|
20 | 20 |
|
21 | 21 | from google.cloud import bigquery
|
22 | 22 | from google.cloud.bigquery.standard_sql import StandardSqlStructType
|
| 23 | +from google.cloud.bigquery import schema |
23 | 24 | from google.cloud.bigquery.schema import PolicyTagList
|
24 | 25 |
|
25 | 26 |
|
@@ -130,8 +131,6 @@ def test_constructor_range_str(self):
|
130 | 131 | self.assertEqual(field.range_element_type.element_type, "DATETIME")
|
131 | 132 |
|
132 | 133 | def test_to_api_repr(self):
|
133 |
| - from google.cloud.bigquery.schema import PolicyTagList |
134 |
| - |
135 | 134 | policy = PolicyTagList(names=("foo", "bar"))
|
136 | 135 | self.assertEqual(
|
137 | 136 | policy.to_api_repr(),
|
@@ -886,8 +885,6 @@ def test_valid_mapping_representation(self):
|
886 | 885 | class TestPolicyTags(unittest.TestCase):
|
887 | 886 | @staticmethod
|
888 | 887 | def _get_target_class():
|
889 |
| - from google.cloud.bigquery.schema import PolicyTagList |
890 |
| - |
891 | 888 | return PolicyTagList
|
892 | 889 |
|
893 | 890 | def _make_one(self, *args, **kw):
|
@@ -1129,3 +1126,90 @@ def test_to_api_repr_parameterized(field, api):
|
1129 | 1126 | from google.cloud.bigquery.schema import SchemaField
|
1130 | 1127 |
|
1131 | 1128 | assert SchemaField(**field).to_api_repr() == api
|
| 1129 | + |
| 1130 | + |
| 1131 | +class TestSerDeInfo: |
| 1132 | + """Tests for the SerDeInfo class.""" |
| 1133 | + |
| 1134 | + @staticmethod |
| 1135 | + def _get_target_class(): |
| 1136 | + return schema.SerDeInfo |
| 1137 | + |
| 1138 | + def _make_one(self, *args, **kwargs): |
| 1139 | + return self._get_target_class()(*args, **kwargs) |
| 1140 | + |
| 1141 | + @pytest.mark.parametrize( |
| 1142 | + "serialization_library,name,parameters", |
| 1143 | + [ |
| 1144 | + ("testpath.to.LazySimpleSerDe", None, None), |
| 1145 | + ("testpath.to.LazySimpleSerDe", "serde_name", None), |
| 1146 | + ("testpath.to.LazySimpleSerDe", None, {"key": "value"}), |
| 1147 | + ("testpath.to.LazySimpleSerDe", "serde_name", {"key": "value"}), |
| 1148 | + ], |
| 1149 | + ) |
| 1150 | + def test_ctor_valid_input(self, serialization_library, name, parameters): |
| 1151 | + serde_info = self._make_one( |
| 1152 | + serialization_library=serialization_library, |
| 1153 | + name=name, |
| 1154 | + parameters=parameters, |
| 1155 | + ) |
| 1156 | + assert serde_info.serialization_library == serialization_library |
| 1157 | + assert serde_info.name == name |
| 1158 | + assert serde_info.parameters == parameters |
| 1159 | + |
| 1160 | + @pytest.mark.parametrize( |
| 1161 | + "serialization_library,name,parameters", |
| 1162 | + [ |
| 1163 | + (123, None, None), |
| 1164 | + ("testpath.to.LazySimpleSerDe", 123, None), |
| 1165 | + ("testpath.to.LazySimpleSerDe", None, ["test", "list"]), |
| 1166 | + ("testpath.to.LazySimpleSerDe", None, 123), |
| 1167 | + ], |
| 1168 | + ) |
| 1169 | + def test_ctor_invalid_input(self, serialization_library, name, parameters): |
| 1170 | + with pytest.raises(TypeError) as e: |
| 1171 | + self._make_one( |
| 1172 | + serialization_library=serialization_library, |
| 1173 | + name=name, |
| 1174 | + parameters=parameters, |
| 1175 | + ) |
| 1176 | + # Looking for the first word from the string "Pass <variable> as..." |
| 1177 | + assert "Pass " in str(e.value) |
| 1178 | + |
| 1179 | + def test_to_api_repr(self): |
| 1180 | + serde_info = self._make_one( |
| 1181 | + serialization_library="testpath.to.LazySimpleSerDe", |
| 1182 | + name="serde_name", |
| 1183 | + parameters={"key": "value"}, |
| 1184 | + ) |
| 1185 | + expected_repr = { |
| 1186 | + "serializationLibrary": "testpath.to.LazySimpleSerDe", |
| 1187 | + "name": "serde_name", |
| 1188 | + "parameters": {"key": "value"}, |
| 1189 | + } |
| 1190 | + assert serde_info.to_api_repr() == expected_repr |
| 1191 | + |
| 1192 | + def test_from_api_repr(self): |
| 1193 | + """GIVEN an api representation of a SerDeInfo object (i.e. resource) |
| 1194 | + WHEN converted into a SerDeInfo object using from_api_repr() |
| 1195 | + THEN it will have the representation in dict format as a SerDeInfo |
| 1196 | + object made directly (via _make_one()) and represented in dict format. |
| 1197 | + """ |
| 1198 | + api_repr = { |
| 1199 | + "serializationLibrary": "testpath.to.LazySimpleSerDe", |
| 1200 | + "name": "serde_name", |
| 1201 | + "parameters": {"key": "value"}, |
| 1202 | + } |
| 1203 | + |
| 1204 | + expected = self._make_one( |
| 1205 | + serialization_library="testpath.to.LazySimpleSerDe", |
| 1206 | + name="serde_name", |
| 1207 | + parameters={"key": "value"}, |
| 1208 | + ) |
| 1209 | + |
| 1210 | + klass = self._get_target_class() |
| 1211 | + result = klass.from_api_repr(api_repr) |
| 1212 | + |
| 1213 | + # We convert both to dict format because these classes do not have a |
| 1214 | + # __eq__() method to facilitate direct equality comparisons. |
| 1215 | + assert result.to_api_repr() == expected.to_api_repr() |
0 commit comments