/
test_schema.py
742 lines (633 loc) · 26.8 KB
/
test_schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# Copyright 2015 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import mock
class TestSchemaField(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.bigquery.schema import SchemaField
return SchemaField
@staticmethod
def _get_standard_sql_data_type_class():
from google.cloud.bigquery_v2 import types
return types.StandardSqlDataType
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_constructor_defaults(self):
field = self._make_one("test", "STRING")
self.assertEqual(field._name, "test")
self.assertEqual(field._field_type, "STRING")
self.assertEqual(field._mode, "NULLABLE")
self.assertIsNone(field._description)
self.assertEqual(field._fields, ())
def test_constructor_explicit(self):
field = self._make_one("test", "STRING", mode="REQUIRED", description="Testing")
self.assertEqual(field._name, "test")
self.assertEqual(field._field_type, "STRING")
self.assertEqual(field._mode, "REQUIRED")
self.assertEqual(field._description, "Testing")
self.assertEqual(field._fields, ())
def test_constructor_subfields(self):
sub_field1 = self._make_one("area_code", "STRING")
sub_field2 = self._make_one("local_number", "STRING")
field = self._make_one(
"phone_number", "RECORD", fields=[sub_field1, sub_field2]
)
self.assertEqual(field._name, "phone_number")
self.assertEqual(field._field_type, "RECORD")
self.assertEqual(field._mode, "NULLABLE")
self.assertIsNone(field._description)
self.assertEqual(len(field._fields), 2)
self.assertIs(field._fields[0], sub_field1)
self.assertIs(field._fields[1], sub_field2)
def test_constructor_with_policy_tags(self):
from google.cloud.bigquery.schema import PolicyTagList
policy = PolicyTagList(names=("foo", "bar"))
field = self._make_one(
"test", "STRING", mode="REQUIRED", description="Testing", policy_tags=policy
)
self.assertEqual(field._name, "test")
self.assertEqual(field._field_type, "STRING")
self.assertEqual(field._mode, "REQUIRED")
self.assertEqual(field._description, "Testing")
self.assertEqual(field._fields, ())
self.assertEqual(field._policy_tags, policy)
def test_to_api_repr(self):
from google.cloud.bigquery.schema import PolicyTagList
policy = PolicyTagList(names=("foo", "bar"))
self.assertEqual(
policy.to_api_repr(), {"names": ["foo", "bar"]},
)
field = self._make_one("foo", "INTEGER", "NULLABLE", policy_tags=policy)
self.assertEqual(
field.to_api_repr(),
{
"mode": "NULLABLE",
"name": "foo",
"type": "INTEGER",
"description": None,
"policyTags": {"names": ["foo", "bar"]},
},
)
def test_to_api_repr_with_subfield(self):
for record_type in ("RECORD", "STRUCT"):
subfield = self._make_one("bar", "INTEGER", "NULLABLE")
field = self._make_one("foo", record_type, "REQUIRED", fields=(subfield,))
self.assertEqual(
field.to_api_repr(),
{
"fields": [
{
"mode": "NULLABLE",
"name": "bar",
"type": "INTEGER",
"description": None,
}
],
"mode": "REQUIRED",
"name": "foo",
"type": record_type,
"description": None,
},
)
def test_from_api_repr(self):
field = self._get_target_class().from_api_repr(
{
"fields": [{"mode": "nullable", "name": "bar", "type": "integer"}],
"mode": "required",
"description": "test_description",
"name": "foo",
"type": "record",
}
)
self.assertEqual(field.name, "foo")
self.assertEqual(field.field_type, "RECORD")
self.assertEqual(field.mode, "REQUIRED")
self.assertEqual(field.description, "test_description")
self.assertEqual(len(field.fields), 1)
self.assertEqual(field.fields[0].name, "bar")
self.assertEqual(field.fields[0].field_type, "INTEGER")
self.assertEqual(field.fields[0].mode, "NULLABLE")
def test_from_api_repr_policy(self):
field = self._get_target_class().from_api_repr(
{
"fields": [{"mode": "nullable", "name": "bar", "type": "integer"}],
"name": "foo",
"type": "record",
"policyTags": {"names": ["one", "two"]},
}
)
self.assertEqual(field.name, "foo")
self.assertEqual(field.field_type, "RECORD")
self.assertEqual(field.policy_tags.names, ("one", "two"))
self.assertEqual(len(field.fields), 1)
self.assertEqual(field.fields[0].name, "bar")
self.assertEqual(field.fields[0].field_type, "INTEGER")
self.assertEqual(field.fields[0].mode, "NULLABLE")
def test_from_api_repr_defaults(self):
field = self._get_target_class().from_api_repr(
{"name": "foo", "type": "record"}
)
self.assertEqual(field.name, "foo")
self.assertEqual(field.field_type, "RECORD")
self.assertEqual(field.mode, "NULLABLE")
self.assertEqual(field.description, None)
self.assertEqual(len(field.fields), 0)
def test_name_property(self):
name = "lemon-ness"
schema_field = self._make_one(name, "INTEGER")
self.assertIs(schema_field.name, name)
def test_field_type_property(self):
field_type = "BOOLEAN"
schema_field = self._make_one("whether", field_type)
self.assertIs(schema_field.field_type, field_type)
def test_mode_property(self):
mode = "REPEATED"
schema_field = self._make_one("again", "FLOAT", mode=mode)
self.assertIs(schema_field.mode, mode)
def test_is_nullable(self):
mode = "NULLABLE"
schema_field = self._make_one("test", "FLOAT", mode=mode)
self.assertTrue(schema_field.is_nullable)
def test_is_not_nullable(self):
mode = "REPEATED"
schema_field = self._make_one("test", "FLOAT", mode=mode)
self.assertFalse(schema_field.is_nullable)
def test_description_property(self):
description = "It holds some data."
schema_field = self._make_one("do", "TIMESTAMP", description=description)
self.assertIs(schema_field.description, description)
def test_fields_property(self):
sub_field1 = self._make_one("one", "STRING")
sub_field2 = self._make_one("fish", "INTEGER")
fields = (sub_field1, sub_field2)
schema_field = self._make_one("boat", "RECORD", fields=fields)
self.assertIs(schema_field.fields, fields)
def test_to_standard_sql_simple_type(self):
sql_type = self._get_standard_sql_data_type_class()
examples = (
# a few legacy types
("INTEGER", sql_type.INT64),
("FLOAT", sql_type.FLOAT64),
("BOOLEAN", sql_type.BOOL),
("DATETIME", sql_type.DATETIME),
# a few standard types
("INT64", sql_type.INT64),
("FLOAT64", sql_type.FLOAT64),
("BOOL", sql_type.BOOL),
("GEOGRAPHY", sql_type.GEOGRAPHY),
)
for legacy_type, standard_type in examples:
field = self._make_one("some_field", legacy_type)
standard_field = field.to_standard_sql()
self.assertEqual(standard_field.name, "some_field")
self.assertEqual(standard_field.type.type_kind, standard_type)
def test_to_standard_sql_struct_type(self):
from google.cloud.bigquery_v2 import types
# Expected result object:
#
# name: "image_usage"
# type {
# type_kind: STRUCT
# struct_type {
# fields {
# name: "image_content"
# type {type_kind: BYTES}
# }
# fields {
# name: "last_used"
# type {
# type_kind: STRUCT
# struct_type {
# fields {
# name: "date_field"
# type {type_kind: DATE}
# }
# fields {
# name: "time_field"
# type {type_kind: TIME}
# }
# }
# }
# }
# }
# }
sql_type = self._get_standard_sql_data_type_class()
# level 2 fields
sub_sub_field_date = types.StandardSqlField(
name="date_field", type=sql_type(type_kind=sql_type.DATE)
)
sub_sub_field_time = types.StandardSqlField(
name="time_field", type=sql_type(type_kind=sql_type.TIME)
)
# level 1 fields
sub_field_struct = types.StandardSqlField(
name="last_used", type=sql_type(type_kind=sql_type.STRUCT)
)
sub_field_struct.type.struct_type.fields.extend(
[sub_sub_field_date, sub_sub_field_time]
)
sub_field_bytes = types.StandardSqlField(
name="image_content", type=sql_type(type_kind=sql_type.BYTES)
)
# level 0 (top level)
expected_result = types.StandardSqlField(
name="image_usage", type=sql_type(type_kind=sql_type.STRUCT)
)
expected_result.type.struct_type.fields.extend(
[sub_field_bytes, sub_field_struct]
)
# construct legacy SchemaField object
sub_sub_field1 = self._make_one("date_field", "DATE")
sub_sub_field2 = self._make_one("time_field", "TIME")
sub_field_record = self._make_one(
"last_used", "RECORD", fields=(sub_sub_field1, sub_sub_field2)
)
sub_field_bytes = self._make_one("image_content", "BYTES")
for type_name in ("RECORD", "STRUCT"):
schema_field = self._make_one(
"image_usage", type_name, fields=(sub_field_bytes, sub_field_record)
)
standard_field = schema_field.to_standard_sql()
self.assertEqual(standard_field, expected_result)
def test_to_standard_sql_array_type_simple(self):
from google.cloud.bigquery_v2 import types
sql_type = self._get_standard_sql_data_type_class()
# construct expected result object
expected_sql_type = sql_type(type_kind=sql_type.ARRAY)
expected_sql_type.array_element_type.type_kind = sql_type.INT64
expected_result = types.StandardSqlField(
name="valid_numbers", type=expected_sql_type
)
# construct "repeated" SchemaField object and convert to standard SQL
schema_field = self._make_one("valid_numbers", "INT64", mode="REPEATED")
standard_field = schema_field.to_standard_sql()
self.assertEqual(standard_field, expected_result)
def test_to_standard_sql_array_type_struct(self):
from google.cloud.bigquery_v2 import types
sql_type = self._get_standard_sql_data_type_class()
# define person STRUCT
name_field = types.StandardSqlField(
name="name", type=sql_type(type_kind=sql_type.STRING)
)
age_field = types.StandardSqlField(
name="age", type=sql_type(type_kind=sql_type.INT64)
)
person_struct = types.StandardSqlField(
name="person_info", type=sql_type(type_kind=sql_type.STRUCT)
)
person_struct.type.struct_type.fields.extend([name_field, age_field])
# define expected result - an ARRAY of person structs
expected_sql_type = sql_type(
type_kind=sql_type.ARRAY, array_element_type=person_struct.type
)
expected_result = types.StandardSqlField(
name="known_people", type=expected_sql_type
)
# construct legacy repeated SchemaField object
sub_field1 = self._make_one("name", "STRING")
sub_field2 = self._make_one("age", "INTEGER")
schema_field = self._make_one(
"known_people", "RECORD", fields=(sub_field1, sub_field2), mode="REPEATED"
)
standard_field = schema_field.to_standard_sql()
self.assertEqual(standard_field, expected_result)
def test_to_standard_sql_unknown_type(self):
sql_type = self._get_standard_sql_data_type_class()
field = self._make_one("weird_field", "TROOLEAN")
standard_field = field.to_standard_sql()
self.assertEqual(standard_field.name, "weird_field")
self.assertEqual(standard_field.type.type_kind, sql_type.TYPE_KIND_UNSPECIFIED)
def test___eq___wrong_type(self):
field = self._make_one("test", "STRING")
other = object()
self.assertNotEqual(field, other)
self.assertEqual(field, mock.ANY)
def test___eq___name_mismatch(self):
field = self._make_one("test", "STRING")
other = self._make_one("other", "STRING")
self.assertNotEqual(field, other)
def test___eq___field_type_mismatch(self):
field = self._make_one("test", "STRING")
other = self._make_one("test", "INTEGER")
self.assertNotEqual(field, other)
def test___eq___mode_mismatch(self):
field = self._make_one("test", "STRING", mode="REQUIRED")
other = self._make_one("test", "STRING", mode="NULLABLE")
self.assertNotEqual(field, other)
def test___eq___description_mismatch(self):
field = self._make_one("test", "STRING", description="Testing")
other = self._make_one("test", "STRING", description="Other")
self.assertNotEqual(field, other)
def test___eq___fields_mismatch(self):
sub1 = self._make_one("sub1", "STRING")
sub2 = self._make_one("sub2", "STRING")
field = self._make_one("test", "RECORD", fields=[sub1])
other = self._make_one("test", "RECORD", fields=[sub2])
self.assertNotEqual(field, other)
def test___eq___hit(self):
field = self._make_one("test", "STRING", mode="REQUIRED", description="Testing")
other = self._make_one("test", "STRING", mode="REQUIRED", description="Testing")
self.assertEqual(field, other)
def test___eq___hit_case_diff_on_type(self):
field = self._make_one("test", "STRING", mode="REQUIRED", description="Testing")
other = self._make_one("test", "string", mode="REQUIRED", description="Testing")
self.assertEqual(field, other)
def test___eq___hit_w_fields(self):
sub1 = self._make_one("sub1", "STRING")
sub2 = self._make_one("sub2", "STRING")
field = self._make_one("test", "RECORD", fields=[sub1, sub2])
other = self._make_one("test", "RECORD", fields=[sub1, sub2])
self.assertEqual(field, other)
def test___ne___wrong_type(self):
field = self._make_one("toast", "INTEGER")
other = object()
self.assertNotEqual(field, other)
self.assertEqual(field, mock.ANY)
def test___ne___same_value(self):
field1 = self._make_one("test", "TIMESTAMP", mode="REPEATED")
field2 = self._make_one("test", "TIMESTAMP", mode="REPEATED")
# unittest ``assertEqual`` uses ``==`` not ``!=``.
comparison_val = field1 != field2
self.assertFalse(comparison_val)
def test___ne___different_values(self):
field1 = self._make_one(
"test1", "FLOAT", mode="REPEATED", description="Not same"
)
field2 = self._make_one(
"test2", "FLOAT", mode="NULLABLE", description="Knot saym"
)
self.assertNotEqual(field1, field2)
def test___hash__set_equality(self):
sub1 = self._make_one("sub1", "STRING")
sub2 = self._make_one("sub2", "STRING")
field1 = self._make_one("test", "RECORD", fields=[sub1])
field2 = self._make_one("test", "RECORD", fields=[sub2])
set_one = {field1, field2}
set_two = {field1, field2}
self.assertEqual(set_one, set_two)
def test___hash__not_equals(self):
sub1 = self._make_one("sub1", "STRING")
sub2 = self._make_one("sub2", "STRING")
field1 = self._make_one("test", "RECORD", fields=[sub1])
field2 = self._make_one("test", "RECORD", fields=[sub2])
set_one = {field1}
set_two = {field2}
self.assertNotEqual(set_one, set_two)
def test___repr__(self):
field1 = self._make_one("field1", "STRING")
expected = "SchemaField('field1', 'STRING', 'NULLABLE', None, (), None)"
self.assertEqual(repr(field1), expected)
# TODO: dedup with the same class in test_table.py.
class _SchemaBase(object):
def _verify_field(self, field, r_field):
self.assertEqual(field.name, r_field["name"])
self.assertEqual(field.field_type, r_field["type"])
self.assertEqual(field.mode, r_field.get("mode", "NULLABLE"))
def _verifySchema(self, schema, resource):
r_fields = resource["schema"]["fields"]
self.assertEqual(len(schema), len(r_fields))
for field, r_field in zip(schema, r_fields):
self._verify_field(field, r_field)
class Test_parse_schema_resource(unittest.TestCase, _SchemaBase):
def _call_fut(self, resource):
from google.cloud.bigquery.schema import _parse_schema_resource
return _parse_schema_resource(resource)
def _make_resource(self):
return {
"schema": {
"fields": [
{"name": "full_name", "type": "STRING", "mode": "REQUIRED"},
{"name": "age", "type": "INTEGER", "mode": "REQUIRED"},
]
}
}
def test__parse_schema_resource_defaults(self):
RESOURCE = self._make_resource()
schema = self._call_fut(RESOURCE["schema"])
self._verifySchema(schema, RESOURCE)
def test__parse_schema_resource_subfields(self):
RESOURCE = self._make_resource()
RESOURCE["schema"]["fields"].append(
{
"name": "phone",
"type": "RECORD",
"mode": "REPEATED",
"fields": [
{"name": "type", "type": "STRING", "mode": "REQUIRED"},
{"name": "number", "type": "STRING", "mode": "REQUIRED"},
],
}
)
schema = self._call_fut(RESOURCE["schema"])
self._verifySchema(schema, RESOURCE)
def test__parse_schema_resource_fields_without_mode(self):
RESOURCE = self._make_resource()
RESOURCE["schema"]["fields"].append({"name": "phone", "type": "STRING"})
schema = self._call_fut(RESOURCE["schema"])
self._verifySchema(schema, RESOURCE)
class Test_build_schema_resource(unittest.TestCase, _SchemaBase):
def _call_fut(self, resource):
from google.cloud.bigquery.schema import _build_schema_resource
return _build_schema_resource(resource)
def test_defaults(self):
from google.cloud.bigquery.schema import SchemaField
full_name = SchemaField("full_name", "STRING", mode="REQUIRED")
age = SchemaField("age", "INTEGER", mode="REQUIRED")
resource = self._call_fut([full_name, age])
self.assertEqual(len(resource), 2)
self.assertEqual(
resource[0],
{
"name": "full_name",
"type": "STRING",
"mode": "REQUIRED",
"description": None,
},
)
self.assertEqual(
resource[1],
{"name": "age", "type": "INTEGER", "mode": "REQUIRED", "description": None},
)
def test_w_description(self):
from google.cloud.bigquery.schema import SchemaField
DESCRIPTION = "DESCRIPTION"
full_name = SchemaField(
"full_name", "STRING", mode="REQUIRED", description=DESCRIPTION
)
age = SchemaField("age", "INTEGER", mode="REQUIRED")
resource = self._call_fut([full_name, age])
self.assertEqual(len(resource), 2)
self.assertEqual(
resource[0],
{
"name": "full_name",
"type": "STRING",
"mode": "REQUIRED",
"description": DESCRIPTION,
},
)
self.assertEqual(
resource[1],
{"name": "age", "type": "INTEGER", "mode": "REQUIRED", "description": None},
)
def test_w_subfields(self):
from google.cloud.bigquery.schema import SchemaField
full_name = SchemaField("full_name", "STRING", mode="REQUIRED")
ph_type = SchemaField("type", "STRING", "REQUIRED")
ph_num = SchemaField("number", "STRING", "REQUIRED")
phone = SchemaField(
"phone", "RECORD", mode="REPEATED", fields=[ph_type, ph_num]
)
resource = self._call_fut([full_name, phone])
self.assertEqual(len(resource), 2)
self.assertEqual(
resource[0],
{
"name": "full_name",
"type": "STRING",
"mode": "REQUIRED",
"description": None,
},
)
self.assertEqual(
resource[1],
{
"name": "phone",
"type": "RECORD",
"mode": "REPEATED",
"description": None,
"fields": [
{
"name": "type",
"type": "STRING",
"mode": "REQUIRED",
"description": None,
},
{
"name": "number",
"type": "STRING",
"mode": "REQUIRED",
"description": None,
},
],
},
)
class Test_to_schema_fields(unittest.TestCase):
@staticmethod
def _call_fut(schema):
from google.cloud.bigquery.schema import _to_schema_fields
return _to_schema_fields(schema)
def test_invalid_type(self):
schema = [
("full_name", "STRING", "REQUIRED"),
("address", "STRING", "REQUIRED"),
]
with self.assertRaises(ValueError):
self._call_fut(schema)
def test_schema_fields_sequence(self):
from google.cloud.bigquery.schema import SchemaField
schema = [
SchemaField("full_name", "STRING", mode="REQUIRED"),
SchemaField("age", "INT64", mode="NULLABLE"),
]
result = self._call_fut(schema)
self.assertEqual(result, schema)
def test_invalid_mapping_representation(self):
schema = [
{"name": "full_name", "type": "STRING", "mode": "REQUIRED"},
{"name": "address", "typeooo": "STRING", "mode": "REQUIRED"},
]
with self.assertRaises(Exception):
self._call_fut(schema)
def test_valid_mapping_representation(self):
from google.cloud.bigquery.schema import SchemaField
schema = [
{"name": "full_name", "type": "STRING", "mode": "REQUIRED"},
{
"name": "residence",
"type": "STRUCT",
"mode": "NULLABLE",
"fields": [
{"name": "foo", "type": "DATE", "mode": "NULLABLE"},
{"name": "bar", "type": "BYTES", "mode": "REQUIRED"},
],
},
]
expected_schema = [
SchemaField("full_name", "STRING", mode="REQUIRED"),
SchemaField(
"residence",
"STRUCT",
mode="NULLABLE",
fields=[
SchemaField("foo", "DATE", mode="NULLABLE"),
SchemaField("bar", "BYTES", mode="REQUIRED"),
],
),
]
result = self._call_fut(schema)
self.assertEqual(result, expected_schema)
class TestPolicyTags(unittest.TestCase):
@staticmethod
def _get_target_class():
from google.cloud.bigquery.schema import PolicyTagList
return PolicyTagList
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def test_constructor(self):
empty_policy_tags = self._make_one()
self.assertIsNotNone(empty_policy_tags.names)
self.assertEqual(len(empty_policy_tags.names), 0)
policy_tags = self._make_one(["foo", "bar"])
self.assertEqual(policy_tags.names, ("foo", "bar"))
def test_from_api_repr(self):
klass = self._get_target_class()
api_repr = {"names": ["foo"]}
policy_tags = klass.from_api_repr(api_repr)
self.assertEqual(policy_tags.to_api_repr(), api_repr)
# Ensure the None case correctly returns None, rather
# than an empty instance.
policy_tags2 = klass.from_api_repr(None)
self.assertIsNone(policy_tags2)
def test_to_api_repr(self):
taglist = self._make_one(names=["foo", "bar"])
self.assertEqual(
taglist.to_api_repr(), {"names": ["foo", "bar"]},
)
taglist2 = self._make_one(names=("foo", "bar"))
self.assertEqual(
taglist2.to_api_repr(), {"names": ["foo", "bar"]},
)
def test___eq___wrong_type(self):
policy = self._make_one(names=["foo"])
other = object()
self.assertNotEqual(policy, other)
self.assertEqual(policy, mock.ANY)
def test___eq___names_mismatch(self):
policy = self._make_one(names=["foo", "bar"])
other = self._make_one(names=["bar", "baz"])
self.assertNotEqual(policy, other)
def test___hash__set_equality(self):
policy1 = self._make_one(["foo", "bar"])
policy2 = self._make_one(["bar", "baz"])
set_one = {policy1, policy2}
set_two = {policy1, policy2}
self.assertEqual(set_one, set_two)
def test___hash__not_equals(self):
policy1 = self._make_one(["foo", "bar"])
policy2 = self._make_one(["bar", "baz"])
set_one = {policy1}
set_two = {policy2}
self.assertNotEqual(set_one, set_two)