Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BigQuery client method (create table) for Tables API #3

Merged
merged 8 commits into from
Apr 2, 2019

Conversation

lbristol88
Copy link

No description provided.

@tswast
Copy link
Owner

tswast commented Mar 29, 2019

@engelke please review

We're working from my fork, as this is dependent on googleapis#7562 which adds the samples directory.

# limitations under the License.


def create_table():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect to see a client argument here (and also the table ID).

Do the tests pass when you run pytest on the samples directory?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Tests have now been updated to pass. 😄

now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
)
table = client.create_table("{}.{}".format(dataset_id, table_id))
yield "{}".format(table.full_table_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use the full_table_id property for this. Use table.project, table.dataset_id, and table.table_id.

The full_table_id property is in legacy SQL format, and also I don't really trust that it's always populated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated with your suggestion

table_id = "example_table_{}_{}".format(
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
)
table = client.create_table("{}.{}".format(dataset_id, table_id))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're creating a table in your sample, you don't actually want to create one in the test fixture.

We might want to rename this to random_table_id to indicate that no table is actually created.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the fixture to random_table_id and removed the creation of the table in the fixture.

create_table.create_table(client, project_id, dataset_id, table_id)
out, err = capsys.readouterr()
assert create_table in out
assert table.table_id == "my_table"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where's the table object coming from? Potentially you could return it from the create_table function, but in this case I think we can get a good enough of a sense that what we wanted to happen actually happened from out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the test to work as you suggested.

from .. import create_table


def test_table_samples(capsys, client, project_id, dataset_id, table_id):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename table_id to random_table_id when you rename the fixture.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated this as suggested.

"""
create_table.create_table(client, project_id, dataset_id, table_id)
out, err = capsys.readouterr()
assert create_table in out
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assert that some string you expect appears in out, not the sample module. In this case, looking for table_id in out would give a good indication that the table was created successfully.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated this as you suggested.

Copy link
Owner

@tswast tswast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much improved, thank you! A few more items to cleanup in the tests.

table = client.create_table("{}.{}".format(dataset_id, table_id))
yield "{}".format(table.full_table_id)
client.delete_table(table, delete_contents=True)
yield "{}.{}".format(dataset_id, random_table_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, since you don't have to do any cleanup, this can now be return instead of yield.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced both table and dataset fixtures to return instead of yield.

def test_table_samples(capsys, client, project_id, dataset_id, table_id):
"""Since creating a table is a long operation, test all table model samples
def test_table_samples(capsys, client, random_table_id):
"""Since creating a table is a long operation, test all table samples
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a table isn't all that long of an operation. You can test each sample separately. This docstring comment can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this docstring.

@@ -15,11 +15,10 @@
from .. import create_table


def test_table_samples(capsys, client, project_id, dataset_id, table_id):
"""Since creating a table is a long operation, test all table model samples
def test_table_samples(capsys, client, random_table_id):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be renamed to test_create_table.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed the name as directed.

@@ -25,6 +25,20 @@ def client():
return bigquery.Client()


@pytest.fixture
def project_id(client):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you don't use the project_id fixture anywhere, this can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed!

@tswast
Copy link
Owner

tswast commented Apr 2, 2019

Oh, and one last thing. Let's remove the bigquery_create_table snippet from docs/snippets.py too.

@lbristol88
Copy link
Author

lbristol88 commented Apr 2, 2019

The test_create_table_sample snippet is now removed from the bigquery collection.

@tswast tswast changed the base branch from bqml to bq-snippets April 2, 2019 20:39
@pytest.fixture
def dataset_id(client):
now = datetime.datetime.now()
dataset_id = "python_samples_{}_{}".format(
now.strftime("%Y%m%d%H%M%S"), uuid.uuid4().hex[:8]
)
dataset = client.create_dataset(dataset_id)
yield "{}.{}".format(dataset.project, dataset.dataset_id)
return "{}.{}".format(dataset.project, dataset.dataset_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this should stay yield, because it needs to clean up the dataset after the test runs.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Copy link
Owner

@tswast tswast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@tswast tswast merged commit 2972c61 into tswast:bq-snippets Apr 2, 2019
@engelke
Copy link

engelke commented Apr 2, 2019

LGTM

tswast pushed a commit that referenced this pull request Jan 15, 2020
* iam proposal #3

maintain compatibility with defaultdict

remove in place

raise KeyError on delete

update deprecation for dict-key access and factory  methods

clean up

maintain compatibility - removing duplicate in __setitems__

check for conditions for dict access

remove empty binding

fix test accessing private var _bindings

fix(tests): change version to make existing tests pass

tests: add tests for getitem, delitem, setitem on v3 and conditions

test policy.bindings property

fixlint

black

sort bindings by role when converting to api repr

add deprecation warning for iam factory methods

update deprecation message for role methods

make Policy#bindings.members a set

update policy docs

fix docs

make docs better

fix: Bigtable policy class to use Policy.bindings

add from_pb with conditions test

add to_pb condition test

blacken

fix policy __delitem__

add docs on dict access

do not modify binding in to_apr_repr

* feat(storage): support requested_policy_version for get_iam_policy

* add system-test

* add ref doc sample to get_iam_policy

* add requested_policy_version to blob

* fix tests

* nit: typo

* blacken

* fix docs build

* format docs

* remove unused variables
tswast pushed a commit that referenced this pull request Jan 15, 2020
…ogleapis#10001)

* iam proposal #3

maintain compatibility with defaultdict

remove in place

raise KeyError on delete

update deprecation for dict-key access and factory  methods

clean up

maintain compatibility - removing duplicate in __setitems__

check for conditions for dict access

remove empty binding

fix test accessing private var _bindings

fix(tests): change version to make existing tests pass

tests: add tests for getitem, delitem, setitem on v3 and conditions

test policy.bindings property

fixlint

black

sort bindings by role when converting to api repr

add deprecation warning for iam factory methods

update deprecation message for role methods

make Policy#bindings.members a set

update policy docs

fix docs

make docs better

fix: Bigtable policy class to use Policy.bindings

add from_pb with conditions test

add to_pb condition test

blacken

fix policy __delitem__

add docs on dict access

do not modify binding in to_apr_repr

* feat(bigtable): support requested_policy_version to instance

* fix passing requested_policy_version to pb2

* add unit test

* add unit test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants