Skip to content

Commit ae54dc3

Browse files
Gurov Ilyatswast
Gurov Ilya
authored andcommitted
feat(bigquery): check json_rows arg type in insert_rows_json() (#10162)
* feat(bigquery): check json_rows arg type in insert_rows_json() * Spelling
1 parent ae7ef87 commit ae54dc3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

bigquery/google/cloud/bigquery/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,8 @@ def insert_rows_json(
25062506
identifies the row, and the "errors" key contains a list of
25072507
the mappings describing one or more problems with the row.
25082508
"""
2509+
if not isinstance(json_rows, collections_abc.Sequence):
2510+
raise TypeError("json_rows argument should be a sequence of dicts")
25092511
# Convert table to just a reference because unlike insert_rows,
25102512
# insert_rows_json doesn't need the table schema. It's not doing any
25112513
# type conversions.

bigquery/tests/unit/test_client.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5384,6 +5384,31 @@ def test_insert_rows_json_w_explicit_none_insert_ids(self):
53845384
timeout=None,
53855385
)
53865386

5387+
def test_insert_rows_w_wrong_arg(self):
5388+
from google.cloud.bigquery.dataset import DatasetReference
5389+
from google.cloud.bigquery.schema import SchemaField
5390+
from google.cloud.bigquery.table import Table
5391+
5392+
PROJECT = "PROJECT"
5393+
DS_ID = "DS_ID"
5394+
TABLE_ID = "TABLE_ID"
5395+
ROW = {"full_name": "Bhettye Rhubble", "age": "27", "joined": None}
5396+
5397+
creds = _make_credentials()
5398+
client = self._make_one(project=PROJECT, credentials=creds, _http=object())
5399+
client._connection = make_connection({})
5400+
5401+
table_ref = DatasetReference(PROJECT, DS_ID).table(TABLE_ID)
5402+
schema = [
5403+
SchemaField("full_name", "STRING", mode="REQUIRED"),
5404+
SchemaField("age", "INTEGER", mode="REQUIRED"),
5405+
SchemaField("joined", "TIMESTAMP", mode="NULLABLE"),
5406+
]
5407+
table = Table(table_ref, schema=schema)
5408+
5409+
with self.assertRaises(TypeError):
5410+
client.insert_rows_json(table, ROW)
5411+
53875412
def test_list_partitions(self):
53885413
from google.cloud.bigquery.table import Table
53895414

0 commit comments

Comments
 (0)