Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/cloud/bigquery/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,6 @@ def from_api_repr(cls, resource: dict) -> SerDeInfo:
Returns:
An instance of the class initialized with data from 'resource'.
"""
config = cls()
config = cls("")
config._properties = copy.deepcopy(resource)
return config
70 changes: 68 additions & 2 deletions tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,23 @@ def test_to_api_repr(self, type_system, expected):
assert result.to_api_repr() == expected


@pytest.fixture
def _make_storage_descriptor():
serdeinfo = SerDeInfo(
serialization_library="testpath.to.LazySimpleSerDe",
name="serde_lib_name",
parameters={"key": "value"},
)

obj = StorageDescriptor(
input_format="testpath.to.OrcInputFormat",
location_uri="gs://test/path/",
output_format="testpath.to.OrcOutputFormat",
serde_info=serdeinfo,
)
return obj


class TestStorageDescriptor:
"""Tests for the StorageDescriptor class."""

Expand Down Expand Up @@ -1251,7 +1268,34 @@ def test_to_api_repr(self):

assert storage_descriptor.to_api_repr() == expected_repr

# TODO: needs a from_api_repr() test.
SERDEINFO = SerDeInfo(
serialization_library="testpath.to.LazySimpleSerDe",
name="serde_lib_name",
parameters={"key": "value"},
)

API_REPR = {
"inputFormat": "testpath.to.OrcInputFormat",
"locationUri": "gs://test/path/",
"outputFormat": "testpath.to.OrcOutputFormat",
"serDeInfo": SERDEINFO.to_api_repr(),
}

def test_from_api_repr(self, _make_storage_descriptor):
"""GIVEN an api representation of a StorageDescriptor (i.e. API_REPR)
WHEN converted into a StorageDescriptor using from_api_repr() and
displayed as a dict
THEN it will have the same representation a StorageDescriptor created
directly (via the fixture) and displayed as a dict.
"""
# generate via fixture
expected = _make_storage_descriptor
resource = self.API_REPR
klass = self._get_target_class()
# generate via API_REPR
result = klass.from_api_repr(resource)

assert result.to_api_repr() == expected.to_api_repr()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels a little strange to have this test, which is intending to test from_api_repr, instead comparing the results of to_api_repr.

Could you write a comparison that checks that the result and expected match more directly?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, as I noted in github.com//pull/2076 since to_api_repr is tested I think this is actually ok.



class TestSerDeInfo:
Expand Down Expand Up @@ -1315,4 +1359,26 @@ def test_to_api_repr(self):
}
assert serde_info.to_api_repr() == expected_repr

# TODO: needs a from_api_repr() test.
def test_from_api_repr(self, _make_storage_descriptor):
"""GIVEN an api representation of a SerDeInfo object (i.e. resource)
WHEN converted into a SerDeInfo using from_api_repr() and
displayed as a dict
THEN it will have the same representation a SerDeInfo object created
directly (via _make_one()) and displayed as a dict.
"""
resource = {
"serializationLibrary": "testpath.to.LazySimpleSerDe",
"name": "serde_name",
"parameters": {"key": "value"},
}

expected = self._make_one(
serialization_library="testpath.to.LazySimpleSerDe",
name="serde_name",
parameters={"key": "value"},
)

klass = self._get_target_class()
result = klass.from_api_repr(resource)

assert result.to_api_repr() == expected.to_api_repr()