1919import pytest
2020
2121from google .cloud import bigquery
22+ from google .cloud .bigquery import enums
2223from google .cloud .bigquery .standard_sql import StandardSqlStructType
2324from google .cloud .bigquery import schema
2425from google .cloud .bigquery .schema import PolicyTagList
@@ -49,6 +50,8 @@ def test_constructor_defaults(self):
4950 self .assertEqual (field .fields , ())
5051 self .assertIsNone (field .policy_tags )
5152 self .assertIsNone (field .default_value_expression )
53+ self .assertEqual (field .rounding_mode , None )
54+ self .assertEqual (field .foreign_type_definition , None )
5255
5356 def test_constructor_explicit (self ):
5457 FIELD_DEFAULT_VALUE_EXPRESSION = "This is the default value for this field"
@@ -64,6 +67,8 @@ def test_constructor_explicit(self):
6467 )
6568 ),
6669 default_value_expression = FIELD_DEFAULT_VALUE_EXPRESSION ,
70+ rounding_mode = enums .RoundingMode .ROUNDING_MODE_UNSPECIFIED ,
71+ foreign_type_definition = "INTEGER" ,
6772 )
6873 self .assertEqual (field .name , "test" )
6974 self .assertEqual (field .field_type , "STRING" )
@@ -80,6 +85,8 @@ def test_constructor_explicit(self):
8085 )
8186 ),
8287 )
88+ self .assertEqual (field .rounding_mode , "ROUNDING_MODE_UNSPECIFIED" )
89+ self .assertEqual (field .foreign_type_definition , "INTEGER" )
8390
8491 def test_constructor_explicit_none (self ):
8592 field = self ._make_one ("test" , "STRING" , description = None , policy_tags = None )
@@ -137,8 +144,16 @@ def test_to_api_repr(self):
137144 {"names" : ["foo" , "bar" ]},
138145 )
139146
147+ ROUNDINGMODE = enums .RoundingMode .ROUNDING_MODE_UNSPECIFIED
148+
140149 field = self ._make_one (
141- "foo" , "INTEGER" , "NULLABLE" , description = "hello world" , policy_tags = policy
150+ "foo" ,
151+ "INTEGER" ,
152+ "NULLABLE" ,
153+ description = "hello world" ,
154+ policy_tags = policy ,
155+ rounding_mode = ROUNDINGMODE ,
156+ foreign_type_definition = None ,
142157 )
143158 self .assertEqual (
144159 field .to_api_repr (),
@@ -148,6 +163,7 @@ def test_to_api_repr(self):
148163 "type" : "INTEGER" ,
149164 "description" : "hello world" ,
150165 "policyTags" : {"names" : ["foo" , "bar" ]},
166+ "roundingMode" : "ROUNDING_MODE_UNSPECIFIED" ,
151167 },
152168 )
153169
@@ -181,6 +197,7 @@ def test_from_api_repr(self):
181197 "description" : "test_description" ,
182198 "name" : "foo" ,
183199 "type" : "record" ,
200+ "roundingMode" : "ROUNDING_MODE_UNSPECIFIED" ,
184201 }
185202 )
186203 self .assertEqual (field .name , "foo" )
@@ -192,6 +209,7 @@ def test_from_api_repr(self):
192209 self .assertEqual (field .fields [0 ].field_type , "INTEGER" )
193210 self .assertEqual (field .fields [0 ].mode , "NULLABLE" )
194211 self .assertEqual (field .range_element_type , None )
212+ self .assertEqual (field .rounding_mode , "ROUNDING_MODE_UNSPECIFIED" )
195213
196214 def test_from_api_repr_policy (self ):
197215 field = self ._get_target_class ().from_api_repr (
@@ -283,6 +301,28 @@ def test_fields_property(self):
283301 schema_field = self ._make_one ("boat" , "RECORD" , fields = fields )
284302 self .assertEqual (schema_field .fields , fields )
285303
304+ def test_roundingmode_property_str (self ):
305+ ROUNDINGMODE = "ROUND_HALF_AWAY_FROM_ZERO"
306+ schema_field = self ._make_one ("test" , "STRING" , rounding_mode = ROUNDINGMODE )
307+ self .assertEqual (schema_field .rounding_mode , ROUNDINGMODE )
308+
309+ del schema_field
310+ schema_field = self ._make_one ("test" , "STRING" )
311+ schema_field ._properties ["roundingMode" ] = ROUNDINGMODE
312+ self .assertEqual (schema_field .rounding_mode , ROUNDINGMODE )
313+
314+ def test_foreign_type_definition_property_str (self ):
315+ FOREIGN_TYPE_DEFINITION = "INTEGER"
316+ schema_field = self ._make_one (
317+ "test" , "STRING" , foreign_type_definition = FOREIGN_TYPE_DEFINITION
318+ )
319+ self .assertEqual (schema_field .foreign_type_definition , FOREIGN_TYPE_DEFINITION )
320+
321+ del schema_field
322+ schema_field = self ._make_one ("test" , "STRING" )
323+ schema_field ._properties ["foreignTypeDefinition" ] = FOREIGN_TYPE_DEFINITION
324+ self .assertEqual (schema_field .foreign_type_definition , FOREIGN_TYPE_DEFINITION )
325+
286326 def test_to_standard_sql_simple_type (self ):
287327 examples = (
288328 # a few legacy types
@@ -457,6 +497,20 @@ def test_to_standard_sql_unknown_type(self):
457497 bigquery .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED ,
458498 )
459499
500+ def test_to_standard_sql_foreign_type_valid (self ):
501+ legacy_type = "FOREIGN"
502+ standard_type = bigquery .StandardSqlTypeNames .FOREIGN
503+ foreign_type_definition = "INTEGER"
504+
505+ field = self ._make_one (
506+ "some_field" ,
507+ field_type = legacy_type ,
508+ foreign_type_definition = foreign_type_definition ,
509+ )
510+ standard_field = field .to_standard_sql ()
511+ self .assertEqual (standard_field .name , "some_field" )
512+ self .assertEqual (standard_field .type .type_kind , standard_type )
513+
460514 def test___eq___wrong_type (self ):
461515 field = self ._make_one ("test" , "STRING" )
462516 other = object ()
0 commit comments