Skip to content

Commit

Permalink
test: making code coverage 100 percent
Browse files Browse the repository at this point in the history
  • Loading branch information
HCA97 committed Aug 9, 2023
1 parent 0dfb4b1 commit ca3c283
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ def _make_list_partitons_meta_info(project, dataset_id, table_id, num_rows=0):
}


def _json_serial_date_only(obj):
"""JSON serializer for objects not serializable by default json code
Ref: https://stackoverflow.com/a/22238613
"""

if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))


class TestClient(unittest.TestCase):

PROJECT = "PROJECT"
Expand Down Expand Up @@ -8665,19 +8676,31 @@ def test_load_table_from_dataframe_with_csv_source_format(self):
sent_config = load_table_from_file.mock_calls[0][2]["job_config"]
assert sent_config.source_format == job.SourceFormat.CSV

def test_load_table_from_json_basic_use_with_json_dumps_kwargs(self):
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
from google.cloud.bigquery import job
def test_load_table_from_json_basic_use_with_json_dumps_kwargs_fail(self):
client = self._make_client()

def json_serial(obj):
"""JSON serializer for objects not serializable by default json code
class Fail:
_fail = "This will fail"

Ref: https://stackoverflow.com/a/22238613
"""
json_rows = [
{
"name": "One",
"age": 11,
"birthday": datetime.date(2008, 9, 10),
"adult": Fail(),
}
]

with pytest.raises(TypeError):
client.load_table_from_json(
json_rows,
self.TABLE_REF,
json_dumps_kwargs={"default": _json_serial_date_only},
)

if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
raise TypeError("Type %s not serializable" % type(obj))
def test_load_table_from_json_basic_use_with_json_dumps_kwargs(self):
from google.cloud.bigquery.client import _DEFAULT_NUM_RETRIES
from google.cloud.bigquery import job

client = self._make_client()

Expand All @@ -8702,7 +8725,9 @@ def json_serial(obj):

with load_patch as load_table_from_file:
client.load_table_from_json(
json_rows, self.TABLE_REF, json_dumps_kwargs={"default": json_serial}
json_rows,
self.TABLE_REF,
json_dumps_kwargs={"default": _json_serial_date_only},
)

load_table_from_file.assert_called_once_with(
Expand Down

0 comments on commit ca3c283

Please sign in to comment.