Skip to content

Commit

Permalink
Improve tests readability and harden assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
plamut committed Aug 22, 2019
1 parent 6cc7f58 commit 4ec2fe1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
21 changes: 10 additions & 11 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,15 +939,6 @@ def test_load_table_from_json_basic_use(self):
self.assertEqual(table.num_rows, 2)

def test_load_table_from_json_schema_autodetect(self):
# Use schema with NULLABLE fields, because schema autodetection
# defaults to field mode NULLABLE.
table_schema = (
bigquery.SchemaField("name", "STRING", mode="NULLABLE"),
bigquery.SchemaField("age", "INTEGER", mode="NULLABLE"),
bigquery.SchemaField("birthday", "DATE", mode="NULLABLE"),
bigquery.SchemaField("is_awesome", "BOOLEAN", mode="NULLABLE"),
)

json_rows = [
{"name": "John", "age": 18, "birthday": "2001-10-15", "is_awesome": False},
{"name": "Chuck", "age": 79, "birthday": "1940-03-10", "is_awesome": True},
Expand All @@ -959,13 +950,21 @@ def test_load_table_from_json_schema_autodetect(self):
Config.CLIENT.project, dataset_id
)

# Create the table before loading so that schema mismatch errors are
# identified.
# Use schema with NULLABLE fields, because schema autodetection
# defaults to field mode NULLABLE.
table_schema = (
bigquery.SchemaField("name", "STRING", mode="NULLABLE"),
bigquery.SchemaField("age", "INTEGER", mode="NULLABLE"),
bigquery.SchemaField("birthday", "DATE", mode="NULLABLE"),
bigquery.SchemaField("is_awesome", "BOOLEAN", mode="NULLABLE"),
)
# create the table before loading so that the column order is predictable
table = retry_403(Config.CLIENT.create_table)(
Table(table_id, schema=table_schema)
)
self.to_delete.insert(0, table)

# do not pass an explicit job config to trigger automatic schema detection
load_job = Config.CLIENT.load_table_from_json(json_rows, table_id)
load_job.result()

Expand Down
3 changes: 3 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5635,6 +5635,7 @@ def test_load_table_from_json_non_default_args(self):
SchemaField("adult", "BOOLEAN"),
]
job_config = job.LoadJobConfig(schema=schema)
job_config._properties["load"]["unknown_field"] = "foobar"

load_patch = mock.patch(
"google.cloud.bigquery.client.Client.load_table_from_file", autospec=True
Expand Down Expand Up @@ -5666,6 +5667,8 @@ def test_load_table_from_json_non_default_args(self):
assert sent_config.source_format == job.SourceFormat.NEWLINE_DELIMITED_JSON
assert sent_config.schema == schema
assert not sent_config.autodetect
# all properties should have been cloned and sent to the backend
assert sent_config._properties.get("load", {}).get("unknown_field") == "foobar"

# Low-level tests

Expand Down

0 comments on commit 4ec2fe1

Please sign in to comment.