19
19
import pytest
20
20
21
21
from google .cloud import bigquery
22
+ from google .cloud .bigquery import enums
22
23
from google .cloud .bigquery .standard_sql import StandardSqlStructType
23
24
from google .cloud .bigquery import schema
24
25
from google .cloud .bigquery .schema import PolicyTagList
@@ -49,6 +50,8 @@ def test_constructor_defaults(self):
49
50
self .assertEqual (field .fields , ())
50
51
self .assertIsNone (field .policy_tags )
51
52
self .assertIsNone (field .default_value_expression )
53
+ self .assertEqual (field .rounding_mode , None )
54
+ self .assertEqual (field .foreign_type_definition , None )
52
55
53
56
def test_constructor_explicit (self ):
54
57
FIELD_DEFAULT_VALUE_EXPRESSION = "This is the default value for this field"
@@ -64,6 +67,8 @@ def test_constructor_explicit(self):
64
67
)
65
68
),
66
69
default_value_expression = FIELD_DEFAULT_VALUE_EXPRESSION ,
70
+ rounding_mode = enums .RoundingMode .ROUNDING_MODE_UNSPECIFIED ,
71
+ foreign_type_definition = "INTEGER" ,
67
72
)
68
73
self .assertEqual (field .name , "test" )
69
74
self .assertEqual (field .field_type , "STRING" )
@@ -80,6 +85,8 @@ def test_constructor_explicit(self):
80
85
)
81
86
),
82
87
)
88
+ self .assertEqual (field .rounding_mode , "ROUNDING_MODE_UNSPECIFIED" )
89
+ self .assertEqual (field .foreign_type_definition , "INTEGER" )
83
90
84
91
def test_constructor_explicit_none (self ):
85
92
field = self ._make_one ("test" , "STRING" , description = None , policy_tags = None )
@@ -137,8 +144,16 @@ def test_to_api_repr(self):
137
144
{"names" : ["foo" , "bar" ]},
138
145
)
139
146
147
+ ROUNDINGMODE = enums .RoundingMode .ROUNDING_MODE_UNSPECIFIED
148
+
140
149
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 ,
142
157
)
143
158
self .assertEqual (
144
159
field .to_api_repr (),
@@ -148,6 +163,7 @@ def test_to_api_repr(self):
148
163
"type" : "INTEGER" ,
149
164
"description" : "hello world" ,
150
165
"policyTags" : {"names" : ["foo" , "bar" ]},
166
+ "roundingMode" : "ROUNDING_MODE_UNSPECIFIED" ,
151
167
},
152
168
)
153
169
@@ -181,6 +197,7 @@ def test_from_api_repr(self):
181
197
"description" : "test_description" ,
182
198
"name" : "foo" ,
183
199
"type" : "record" ,
200
+ "roundingMode" : "ROUNDING_MODE_UNSPECIFIED" ,
184
201
}
185
202
)
186
203
self .assertEqual (field .name , "foo" )
@@ -192,6 +209,7 @@ def test_from_api_repr(self):
192
209
self .assertEqual (field .fields [0 ].field_type , "INTEGER" )
193
210
self .assertEqual (field .fields [0 ].mode , "NULLABLE" )
194
211
self .assertEqual (field .range_element_type , None )
212
+ self .assertEqual (field .rounding_mode , "ROUNDING_MODE_UNSPECIFIED" )
195
213
196
214
def test_from_api_repr_policy (self ):
197
215
field = self ._get_target_class ().from_api_repr (
@@ -283,6 +301,28 @@ def test_fields_property(self):
283
301
schema_field = self ._make_one ("boat" , "RECORD" , fields = fields )
284
302
self .assertEqual (schema_field .fields , fields )
285
303
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
+
286
326
def test_to_standard_sql_simple_type (self ):
287
327
examples = (
288
328
# a few legacy types
@@ -457,6 +497,20 @@ def test_to_standard_sql_unknown_type(self):
457
497
bigquery .StandardSqlTypeNames .TYPE_KIND_UNSPECIFIED ,
458
498
)
459
499
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
+
460
514
def test___eq___wrong_type (self ):
461
515
field = self ._make_one ("test" , "STRING" )
462
516
other = object ()
0 commit comments