1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- from google .cloud import bigquery
15+ from unittest import mock
16+
17+ import pytest
18+
19+ from google .cloud import bigquery as bq
1620
1721
1822class TestStandardSqlDataType :
@@ -27,7 +31,7 @@ def _make_one(self, *args, **kw):
2731
2832 def test_ctor_default_type_kind (self ):
2933 instance = self ._make_one ()
30- assert instance .type_kind == bigquery .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED
34+ assert instance .type_kind == bq .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED
3135
3236 def test_to_api_repr_no_type_set (self ):
3337 instance = self ._make_one ()
@@ -38,15 +42,15 @@ def test_to_api_repr_no_type_set(self):
3842 assert result == {"typeKind" : "TYPE_KIND_UNSPECIFIED" }
3943
4044 def test_to_api_repr_scalar_type (self ):
41- instance = self ._make_one (bigquery .StandardSqlTypeNames .FLOAT64 )
45+ instance = self ._make_one (bq .StandardSqlTypeNames .FLOAT64 )
4246
4347 result = instance .to_api_repr ()
4448
4549 assert result == {"typeKind" : "FLOAT64" }
4650
4751 def test_to_api_repr_array_type_element_type_missing (self ):
4852 instance = self ._make_one (
49- bigquery .StandardSqlTypeNames .ARRAY , array_element_type = None
53+ bq .StandardSqlTypeNames .ARRAY , array_element_type = None
5054 )
5155
5256 result = instance .to_api_repr ()
@@ -55,11 +59,9 @@ def test_to_api_repr_array_type_element_type_missing(self):
5559 assert result == expected
5660
5761 def test_to_api_repr_array_type_w_element_type (self ):
58- array_element_type = self ._make_one (
59- type_kind = bigquery .StandardSqlTypeNames .BOOL
60- )
62+ array_element_type = self ._make_one (type_kind = bq .StandardSqlTypeNames .BOOL )
6163 instance = self ._make_one (
62- bigquery .StandardSqlTypeNames .ARRAY , array_element_type = array_element_type
64+ bq .StandardSqlTypeNames .ARRAY , array_element_type = array_element_type
6365 )
6466
6567 result = instance .to_api_repr ()
@@ -68,9 +70,7 @@ def test_to_api_repr_array_type_w_element_type(self):
6870 assert result == expected
6971
7072 def test_to_api_repr_struct_type_field_types_missing (self ):
71- instance = self ._make_one (
72- bigquery .StandardSqlTypeNames .STRUCT , struct_type = None
73- )
73+ instance = self ._make_one (bq .StandardSqlTypeNames .STRUCT , struct_type = None )
7474
7575 result = instance .to_api_repr ()
7676
@@ -81,7 +81,7 @@ def test_to_api_repr_struct_type_w_field_types(self):
8181 from google .cloud .bigquery .standard_sql import StandardSqlStructType
8282
8383 StandardSqlDataType = self ._get_target_class ()
84- TypeNames = bigquery .StandardSqlTypeNames
84+ TypeNames = bq .StandardSqlTypeNames
8585
8686 person_type = StandardSqlStructType (
8787 fields = [
@@ -133,7 +133,7 @@ def test_from_api_repr_empty_resource(self):
133133 result = klass .from_api_repr (resource = {})
134134
135135 expected = klass (
136- type_kind = bigquery .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED ,
136+ type_kind = bq .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED ,
137137 array_element_type = None ,
138138 struct_type = None ,
139139 )
@@ -146,7 +146,7 @@ def test_from_api_repr_scalar_type(self):
146146 result = klass .from_api_repr (resource = resource )
147147
148148 expected = klass (
149- type_kind = bigquery .StandardSqlTypeNames .DATE ,
149+ type_kind = bq .StandardSqlTypeNames .DATE ,
150150 array_element_type = None ,
151151 struct_type = None ,
152152 )
@@ -159,8 +159,8 @@ def test_from_api_repr_array_type_full(self):
159159 result = klass .from_api_repr (resource = resource )
160160
161161 expected = klass (
162- type_kind = bigquery .StandardSqlTypeNames .ARRAY ,
163- array_element_type = klass (type_kind = bigquery .StandardSqlTypeNames .BYTES ),
162+ type_kind = bq .StandardSqlTypeNames .ARRAY ,
163+ array_element_type = klass (type_kind = bq .StandardSqlTypeNames .BYTES ),
164164 struct_type = None ,
165165 )
166166 assert result == expected
@@ -172,7 +172,7 @@ def test_from_api_repr_array_type_missing_element_type(self):
172172 result = klass .from_api_repr (resource = resource )
173173
174174 expected = klass (
175- type_kind = bigquery .StandardSqlTypeNames .ARRAY ,
175+ type_kind = bq .StandardSqlTypeNames .ARRAY ,
176176 array_element_type = None ,
177177 struct_type = None ,
178178 )
@@ -183,7 +183,7 @@ def test_from_api_repr_struct_type_nested(self):
183183 from google .cloud .bigquery .standard_sql import StandardSqlStructType
184184
185185 klass = self ._get_target_class ()
186- TypeNames = bigquery .StandardSqlTypeNames
186+ TypeNames = bq .StandardSqlTypeNames
187187
188188 resource = {
189189 "typeKind" : "STRUCT" ,
@@ -239,7 +239,7 @@ def test_from_api_repr_struct_type_missing_struct_info(self):
239239 result = klass .from_api_repr (resource = resource )
240240
241241 expected = klass (
242- type_kind = bigquery .StandardSqlTypeNames .STRUCT ,
242+ type_kind = bq .StandardSqlTypeNames .STRUCT ,
243243 array_element_type = None ,
244244 struct_type = None ,
245245 )
@@ -250,7 +250,7 @@ def test_from_api_repr_struct_type_incomplete_field_info(self):
250250 from google .cloud .bigquery .standard_sql import StandardSqlStructType
251251
252252 klass = self ._get_target_class ()
253- TypeNames = bigquery .StandardSqlTypeNames
253+ TypeNames = bq .StandardSqlTypeNames
254254
255255 resource = {
256256 "typeKind" : "STRUCT" ,
@@ -275,12 +275,101 @@ def test_from_api_repr_struct_type_incomplete_field_info(self):
275275 )
276276 assert result == expected
277277
278+ def test__eq__another_type (self ):
279+ instance = self ._make_one ()
280+
281+ class SqlTypeWannabe :
282+ pass
283+
284+ not_a_type = SqlTypeWannabe ()
285+ not_a_type ._properties = instance ._properties
286+
287+ assert instance != not_a_type # Can't fake it.
288+
289+ def test__eq__delegates_comparison_to_another_type (self ):
290+ instance = self ._make_one ()
291+ assert instance == mock .ANY
292+
293+ def test__eq__similar_instance (self ):
294+ kwargs = {
295+ "type_kind" : bq .StandardSqlTypeNames .GEOGRAPHY ,
296+ "array_element_type" : bq .StandardSqlDataType (
297+ type_kind = bq .StandardSqlTypeNames .INT64
298+ ),
299+ "struct_type" : bq .StandardSqlStructType (fields = []),
300+ }
301+ instance = self ._make_one (** kwargs )
302+ instance2 = self ._make_one (** kwargs )
303+ assert instance == instance2
304+
305+ @pytest .mark .parametrize (
306+ ("attr_name" , "value" , "value2" ),
307+ (
308+ (
309+ "type_kind" ,
310+ bq .StandardSqlTypeNames .INT64 ,
311+ bq .StandardSqlTypeNames .FLOAT64 ,
312+ ),
313+ (
314+ "array_element_type" ,
315+ bq .StandardSqlDataType (type_kind = bq .StandardSqlTypeNames .STRING ),
316+ bq .StandardSqlDataType (type_kind = bq .StandardSqlTypeNames .BOOL ),
317+ ),
318+ (
319+ "struct_type" ,
320+ bq .StandardSqlStructType (fields = [bq .StandardSqlField (name = "foo" )]),
321+ bq .StandardSqlStructType (fields = [bq .StandardSqlField (name = "bar" )]),
322+ ),
323+ ),
324+ )
325+ def test__eq__attribute_differs (self , attr_name , value , value2 ):
326+ instance = self ._make_one (** {attr_name : value })
327+ instance2 = self ._make_one (** {attr_name : value2 })
328+ assert instance != instance2
329+
278330 def test_str (self ):
279- instance = self ._make_one (type_kind = bigquery .StandardSqlTypeNames .BOOL )
280- bool_type_repr = repr (bigquery .StandardSqlTypeNames .BOOL )
331+ instance = self ._make_one (type_kind = bq .StandardSqlTypeNames .BOOL )
332+ bool_type_repr = repr (bq .StandardSqlTypeNames .BOOL )
281333 assert str (instance ) == f"StandardSqlDataType(type_kind={ bool_type_repr } , ...)"
282334
283335
336+ class TestStandardSqlField :
337+ # This class only contains minimum tests to cover what other tests don't
338+
339+ @staticmethod
340+ def _get_target_class ():
341+ from google .cloud .bigquery .standard_sql import StandardSqlField
342+
343+ return StandardSqlField
344+
345+ def _make_one (self , * args , ** kw ):
346+ return self ._get_target_class ()(* args , ** kw )
347+
348+ def test_name (self ):
349+ instance = self ._make_one (name = "foo" )
350+ assert instance .name == "foo"
351+ instance .name = "bar"
352+ assert instance .name == "bar"
353+
354+ def test_type_missing (self ):
355+ instance = self ._make_one (type = None )
356+ assert instance .type is None
357+
358+ def test_type_set_none (self ):
359+ instance = self ._make_one (
360+ type = bq .StandardSqlDataType (type_kind = bq .StandardSqlTypeNames .BOOL )
361+ )
362+ instance .type = None
363+ assert instance .type is None
364+
365+ def test_type_set_not_none (self ):
366+ instance = self ._make_one (type = bq .StandardSqlDataType (type_kind = None ))
367+ instance .type = bq .StandardSqlDataType (type_kind = bq .StandardSqlTypeNames .INT64 )
368+ assert instance .type == bq .StandardSqlDataType (
369+ type_kind = bq .StandardSqlTypeNames .INT64
370+ )
371+
372+
284373class TestStandardSqlTableType :
285374 @staticmethod
286375 def _get_target_class ():
@@ -345,15 +434,14 @@ def test_from_api_repr_with_incomplete_columns(self):
345434 assert len (result .columns ) == 2
346435
347436 expected = StandardSqlField (
348- name = None ,
349- type = StandardSqlDataType (type_kind = bigquery .StandardSqlTypeNames .BOOL ),
437+ name = None , type = StandardSqlDataType (type_kind = bq .StandardSqlTypeNames .BOOL ),
350438 )
351439 assert result .columns [0 ] == expected
352440
353441 expected = StandardSqlField (
354442 name = "bar" ,
355443 type = StandardSqlDataType (
356- type_kind = bigquery .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED
444+ type_kind = bq .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED
357445 ),
358446 )
359447 assert result .columns [1 ] == expected
0 commit comments