Skip to content

Commit 68d04f0

Browse files
committed
Adds content related to ForeignTypeInfo
1 parent 5fc89ae commit 68d04f0

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

google/cloud/bigquery/schema.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from typing import Any, Dict, Iterable, Optional, Union, cast
2121

2222
from google.cloud.bigquery import standard_sql
23-
from google.cloud.bigquery._helpers import ResourceBase
23+
from google.cloud.bigquery._helpers import ResourceBase, _isinstance_or_raise
2424
from google.cloud.bigquery.enums import StandardSqlTypeNames
2525

2626

@@ -596,16 +596,15 @@ class TableSchema(ResourceBase):
596596
"""Schema of a table
597597
598598
Args:
599-
fields (list): Describes the fields in a table.
600-
foreignTypeInfo: Optional. Specifies metadata of the foreign data type
601-
definition in field schema
602-
(TableFieldSchema.foreign_type_definition).
599+
fields (Optional[list]): Describes the fields in a table.
600+
foreignTypeInfo (Optional[str]): Specifies metadata of the foreign data type
601+
definition in field schema.
603602
"""
604603

605-
def __init__(self, fields: list = None, foreign_type_info: Optional[str] = None):
604+
def __init__(self, fields: Optional[list] = None, foreign_type_info: Optional[str] = None):
606605
self._properties = {}
607-
self._properties["fields"] = fields
608-
self._properties["foreignTypeInfo"] = foreign_type_info
606+
self.fields = fields
607+
self.foreign_type_info = foreign_type_info
609608

610609
@property
611610
def fields(self) -> Any:
@@ -615,8 +614,7 @@ def fields(self) -> Any:
615614

616615
@fields.setter
617616
def fields(self, value: list, dtype: str) -> str:
618-
if not isinstance(value, list):
619-
raise ValueError(f"Pass fields as a '{repr(dtype)}'." f"Got {type(value)}.")
617+
value = _isinstance_or_raise(value, (list, None))
620618
self._properties["fields"] = value
621619

622620
@property
@@ -653,9 +651,9 @@ class ForeignTypeInfo(ResourceBase):
653651
foreign data type.
654652
"""
655653

656-
def __init__(self, type_system_="TYPE_SYSTEM_UNSPECIFIED"):
654+
def __init__(self, type_system="TYPE_SYSTEM_UNSPECIFIED"):
657655
self._properties = {}
658-
self._properties["typeSystem"] = type_system_
656+
self.type_system = type_system
659657

660658
@property
661659
def type_system(self):
@@ -666,8 +664,7 @@ def type_system(self):
666664

667665
@type_system.setter
668666
def type_system(self, value: str):
669-
if not isinstance(value, str) or value is None:
670-
raise ValueError("Pass type_system as a 'str'." f" Got {type(value)}.")
667+
value = _isinstance_or_raise(value, (str, None))
671668
self._properties["typeSystem"] = value
672669

673670
def to_api_repr(self) -> dict:
@@ -678,3 +675,7 @@ def to_api_repr(self) -> dict:
678675
A dictionary in the format used by the BigQuery API.
679676
"""
680677
return copy.deepcopy(self._properties)
678+
679+
680+
681+

tests/unit/test_schema.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,34 +1111,43 @@ def test_to_api_repr_parameterized(field, api):
11111111
assert SchemaField(**field).to_api_repr() == api
11121112

11131113

1114-
class TestForeignTypeSystem:
1114+
class TestForeignTypeInfo:
11151115
"""TODO: add doc string."""
11161116

1117-
def test_foreign_type_system_constructor_valid_type_system(self):
1118-
foreign_type_info = ForeignTypeInfo("my_type_system")
1119-
assert foreign_type_info.type_system == "my_type_system"
1117+
@staticmethod
1118+
def _get_target_class():
1119+
from google.cloud.bigquery.schema import ForeignTypeInfo
1120+
return ForeignTypeInfo
1121+
1122+
def _make_one(self, *args, **kw):
1123+
return self._get_target_class()(*args, **kw)
1124+
1125+
@pytest.mark.parametrize(
1126+
"type_system,expected",
1127+
[
1128+
(None, None),
1129+
("TYPE_SYSTEM_UNSPECIFIED", "TYPE_SYSTEM_UNSPECIFIED"),
1130+
("HIVE", "HIVE"),
1131+
],
1132+
)
1133+
def test_ctor_valid_input(self, type_system, expected):
1134+
result = self._make_one(type_system=type_system)
1135+
1136+
assert result._properties['typeSystem'] == expected
11201137

1121-
@pytest.mark.parametrize("value", [(42), (None)])
1122-
def test_foreign_type_system_constructor_invalid_type_system(self, value):
1123-
foreign_type_info = ForeignTypeInfo("my_type_system")
1124-
with pytest.raises(ValueError) as exc_info:
1125-
foreign_type_info.type_system = value
1126-
assert "Pass type_system as a 'str'." in str(exc_info.value)
1138+
def test_ctor_invalid_input(self):
1139+
with pytest.raises(TypeError) as e:
1140+
result = self._make_one(type_system=123)
1141+
assert result == e
11271142

11281143
@pytest.mark.parametrize(
1129-
"type_system, expected_api_repr",
1144+
"type_system,expected",
11301145
[
11311146
("TYPE_SYSTEM_UNSPECIFIED", {"typeSystem": "TYPE_SYSTEM_UNSPECIFIED"}),
11321147
("HIVE", {"typeSystem": "HIVE"}),
1148+
(None, {"typeSystem": None}),
11331149
],
11341150
)
1135-
def test_to_api_repr(self, type_system, expected_api_repr):
1136-
foreign_type_info = ForeignTypeInfo(type_system)
1137-
actual = foreign_type_info.to_api_repr()
1138-
assert actual == expected_api_repr
1139-
1140-
def test_to_api_repr_type_system_unspecified(self):
1141-
foreign_type_info = ForeignTypeInfo()
1142-
expected_api_repr = {"typeSystem": "TYPE_SYSTEM_UNSPECIFIED"}
1143-
actual = foreign_type_info.to_api_repr()
1144-
assert actual == expected_api_repr
1151+
def test_to_api_repr(self, type_system, expected):
1152+
result = self._make_one(type_system=type_system)
1153+
assert result.to_api_repr() == expected

0 commit comments

Comments
 (0)