Skip to content

Commit

Permalink
feat(bigquery): check json_rows arg type in insert_rows_json() (#…
Browse files Browse the repository at this point in the history
…10162)

* feat(bigquery): check json_rows arg type in insert_rows_json()

* Spelling
  • Loading branch information
Gurov Ilya authored and tswast committed Jan 17, 2020
1 parent ae7ef87 commit ae54dc3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bigquery/google/cloud/bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,8 @@ def insert_rows_json(
identifies the row, and the "errors" key contains a list of
the mappings describing one or more problems with the row.
"""
if not isinstance(json_rows, collections_abc.Sequence):
raise TypeError("json_rows argument should be a sequence of dicts")
# Convert table to just a reference because unlike insert_rows,
# insert_rows_json doesn't need the table schema. It's not doing any
# type conversions.
Expand Down
25 changes: 25 additions & 0 deletions bigquery/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5384,6 +5384,31 @@ def test_insert_rows_json_w_explicit_none_insert_ids(self):
timeout=None,
)

def test_insert_rows_w_wrong_arg(self):
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.schema import SchemaField
from google.cloud.bigquery.table import Table

PROJECT = "PROJECT"
DS_ID = "DS_ID"
TABLE_ID = "TABLE_ID"
ROW = {"full_name": "Bhettye Rhubble", "age": "27", "joined": None}

creds = _make_credentials()
client = self._make_one(project=PROJECT, credentials=creds, _http=object())
client._connection = make_connection({})

table_ref = DatasetReference(PROJECT, DS_ID).table(TABLE_ID)
schema = [
SchemaField("full_name", "STRING", mode="REQUIRED"),
SchemaField("age", "INTEGER", mode="REQUIRED"),
SchemaField("joined", "TIMESTAMP", mode="NULLABLE"),
]
table = Table(table_ref, schema=schema)

with self.assertRaises(TypeError):
client.insert_rows_json(table, ROW)

def test_list_partitions(self):
from google.cloud.bigquery.table import Table

Expand Down

0 comments on commit ae54dc3

Please sign in to comment.