Skip to content

Commit 8162a2c

Browse files
feat: adds ForeignTypeInfo test (#2076)
* likely gonna delete this branch cause TestTableSchema may not be necessary * Adds ForeignTypeInfo test_from_api_repr * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fixes merge conflict --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 9fd8854 commit 8162a2c

File tree

2 files changed

+33
-61
lines changed

2 files changed

+33
-61
lines changed

google/cloud/bigquery/schema.py

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from google.cloud.bigquery import standard_sql
2525
from google.cloud.bigquery._helpers import (
2626
_isinstance_or_raise,
27-
_from_api_repr,
2827
_get_sub_prop,
2928
)
3029
from google.cloud.bigquery.enums import StandardSqlTypeNames
@@ -501,6 +500,7 @@ def _to_schema_fields(schema):
501500
sequence is not a :class:`~google.cloud.bigquery.schema.SchemaField`
502501
instance or a compatible mapping representation of the field.
503502
"""
503+
504504
for field in schema:
505505
if not isinstance(field, (SchemaField, collections.abc.Mapping)):
506506
raise ValueError(
@@ -598,61 +598,6 @@ def to_api_repr(self) -> dict:
598598
return answer
599599

600600

601-
class TableSchema:
602-
"""Schema of a table
603-
604-
Args:
605-
fields (Optional[list]): Describes the fields in a table.
606-
foreignTypeInfo (Optional[str]): Specifies metadata of the foreign data type
607-
definition in field schema.
608-
"""
609-
610-
def __init__(
611-
self, fields: Optional[list] = None, foreign_type_info: Optional[str] = None
612-
):
613-
self._properties: Dict[str, Any] = {}
614-
self.fields = fields
615-
self.foreign_type_info = foreign_type_info
616-
617-
@property
618-
def fields(self) -> Any:
619-
"""Describes the fields in a table."""
620-
621-
return self._properties.get("fields")
622-
623-
@fields.setter
624-
def fields(self, value: list, dtype: str) -> None:
625-
value = _isinstance_or_raise(value, list, none_allowed=True)
626-
self._properties["fields"] = value
627-
628-
@property
629-
def foreign_type_info(self) -> Any:
630-
"""Optional. Specifies metadata of the foreign data type definition in
631-
field schema (TableFieldSchema.foreign_type_definition)."""
632-
633-
return self._properties.get("foreignTypeInfo")
634-
635-
@foreign_type_info.setter
636-
def foreign_type_info(self, value: str, dtype: str) -> None:
637-
if not isinstance(value, str):
638-
raise ValueError(
639-
f"Pass {value} as a '{repr(dtype)}'." f"Got {type(value)}."
640-
)
641-
self._properties["foreignTypeInfo"] = value
642-
643-
def to_api_repr(self) -> dict:
644-
"""Build an API representation of this object.
645-
646-
Returns:
647-
Dict[str, Any]:
648-
A dictionary in the format used by the BigQuery API.
649-
"""
650-
return copy.deepcopy(self._properties)
651-
652-
def from_api_repr(self, resource):
653-
return _from_api_repr(self, resource)
654-
655-
656601
class ForeignTypeInfo:
657602
"""Metadata about the foreign data type definition such as the system in which the
658603
type is defined.
@@ -687,8 +632,21 @@ def to_api_repr(self) -> dict:
687632
"""
688633
return copy.deepcopy(self._properties)
689634

690-
def from_api_repr(self, resource):
691-
return _from_api_repr(self, resource)
635+
@classmethod
636+
def from_api_repr(cls, resource):
637+
"""Factory: constructs an instance of the class (cls)
638+
given its API representation.
639+
640+
Args:
641+
resource (Dict[str, Any]):
642+
API representation of the object to be instantiated.
643+
644+
Returns:
645+
An instance of the class initialized with data from 'resource'.
646+
"""
647+
config = cls()
648+
config._properties = copy.deepcopy(resource)
649+
return config
692650

693651

694652
class StorageDescriptor:

tests/unit/test_schema.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from google.cloud.bigquery.standard_sql import StandardSqlStructType
1717
from google.cloud.bigquery.schema import (
1818
PolicyTagList,
19-
# ForeignTypeInfo,
19+
ForeignTypeInfo,
2020
StorageDescriptor,
2121
SerDeInfo,
2222
)
@@ -1121,8 +1121,6 @@ class TestForeignTypeInfo:
11211121

11221122
@staticmethod
11231123
def _get_target_class():
1124-
from google.cloud.bigquery.schema import ForeignTypeInfo
1125-
11261124
return ForeignTypeInfo
11271125

11281126
def _make_one(self, *args, **kw):
@@ -1160,6 +1158,22 @@ def test_to_api_repr(self, type_system, expected):
11601158
result = self._make_one(type_system=type_system)
11611159
assert result.to_api_repr() == expected
11621160

1161+
def test_from_api_repr(self):
1162+
"""GIVEN an api representation of a ForeignTypeInfo object (i.e. resource)
1163+
WHEN converted into a ForeignTypeInfo object using from_api_repr() and
1164+
displayed as a dict
1165+
THEN it will have the same representation a ForeignTypeInfo object created
1166+
directly (via _make_one()) and displayed as a dict.
1167+
"""
1168+
resource = {"typeSystem": "TYPE_SYSTEM_UNSPECIFIED"}
1169+
1170+
expected = self._make_one(type_system="TYPE_SYSTEM_UNSPECIFIED")
1171+
1172+
klass = self._get_target_class()
1173+
result = klass.from_api_repr(resource)
1174+
1175+
assert result.to_api_repr() == expected.to_api_repr()
1176+
11631177

11641178
@pytest.fixture
11651179
def _make_storage_descriptor():

0 commit comments

Comments
 (0)