1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- < << << << HEAD
15+
16+ import unittest
17+ from unittest import mock
18+
19+ import pytest
20+
1621from google .cloud import bigquery
1722from google .cloud .bigquery .enums import RoundingMode
1823from google .cloud .bigquery .standard_sql import StandardSqlStructType
2833 _to_schema_fields ,
2934)
3035
31- == == == =
32- import copy
33- > >> >> >> aaf1eb85 (feat : preserve unknown fields from the REST API representation in `SchemaField` (#2097))
34- import unittest
35- from unittest import mock
36-
37- import pytest
38-
39- from google .cloud import bigquery
40- from google .cloud .bigquery .standard_sql import StandardSqlStructType
41- from google .cloud .bigquery .schema import PolicyTagList
42-
4336
4437class TestSchemaField (unittest .TestCase ):
4538 @staticmethod
@@ -858,13 +851,6 @@ def test_schema_fields_sequence(self):
858851
859852 def test_unknown_properties (self ):
860853 schema = [
861- < << << << HEAD
862- {"name" : "full_name" , "type" : "STRING" , "mode" : "REQUIRED" },
863- {"name" : "address" , "invalid_key" : "STRING" , "mode" : "REQUIRED" },
864- ]
865- with pytest .raises (Exception ): # Or a more specific exception if known
866- _to_schema_fields (schema )
867- == == == =
868854 {
869855 "name" : "full_name" ,
870856 "type" : "STRING" ,
@@ -881,15 +867,8 @@ def test_unknown_properties(self):
881867 "anotherNewProperty" : "another-test" ,
882868 },
883869 ]
884-
885- # Make sure the setter doesn't mutate schema.
886- expected_schema = copy .deepcopy (schema )
887-
888- result = self ._call_fut (schema )
889-
890- for api_repr , field in zip (expected_schema , result ):
891- assert field .to_api_repr () == api_repr
892- > >> >> >> aaf1eb85 (feat : preserve unknown fields from the REST API representation in `SchemaField` (#2097))
870+ with pytest .raises (Exception ): # Or a more specific exception if known
871+ _to_schema_fields (schema )
893872
894873 @pytest .mark .parametrize (
895874 "schema, expected_schema" ,
@@ -927,19 +906,8 @@ def test_valid_mapping_representation(self, schema, expected_schema):
927906 result = _to_schema_fields (schema )
928907 assert result == expected_schema
929908
930- def test_valid_schema_object (self ):
931- schema = Schema (
932- fields = [SchemaField ("name" , "STRING" , description = None , policy_tags = None )],
933- foreign_type_info = "TestInfo" ,
934- )
935- result = _to_schema_fields (schema )
936- expected = Schema (
937- [SchemaField ("name" , "STRING" , "NULLABLE" , None , None , (), None )],
938- "TestInfo" ,
939- )
940- assert result .to_api_repr () == expected .to_api_repr ()
941-
942909
910+ # Testing the new Schema Class =================
943911class TestSchemaObject : # New test class for Schema object interactions
944912 def test_schema_object_field_access (self ):
945913 schema = Schema (
@@ -948,10 +916,9 @@ def test_schema_object_field_access(self):
948916 SchemaField ("age" , "INTEGER" ),
949917 ]
950918 )
951-
952919 assert len (schema ) == 2
953- assert schema [0 ][ " name" ] == "name" # Access fields using indexing
954- assert schema [1 ][ "type" ] == "INTEGER"
920+ assert schema [0 ]. name == "name" # Access fields using indexing
921+ assert schema [1 ]. field_type == "INTEGER"
955922
956923 def test_schema_object_foreign_type_info (self ):
957924 schema = Schema (foreign_type_info = "External" )
@@ -963,16 +930,6 @@ def test_schema_object_foreign_type_info(self):
963930 with pytest .raises (TypeError ):
964931 schema .foreign_type_info = 123 # Type check
965932
966- def test_str (self ):
967- schema = Schema (
968- fields = [SchemaField ("name" , "STRING" )],
969- foreign_type_info = "TestInfo" ,
970- )
971- assert (
972- str (schema )
973- == "Schema([{'name': 'name', 'mode': 'NULLABLE', 'type': 'STRING'}], TestInfo)"
974- )
975-
976933 @pytest .mark .parametrize (
977934 "schema, expected_repr" ,
978935 [
@@ -981,12 +938,12 @@ def test_str(self):
981938 fields = [SchemaField ("name" , "STRING" )],
982939 foreign_type_info = "TestInfo" ,
983940 ),
984- "Schema([{ 'name': 'name ', 'mode': ' NULLABLE', 'type': 'STRING'} ], 'TestInfo')" ,
941+ "Schema([SchemaField( 'name', 'STRING ', 'NULLABLE', None, None, (), None) ], 'TestInfo')" ,
985942 id = "repr with foreign type info" ,
986943 ),
987944 pytest .param (
988945 Schema (fields = [SchemaField ("name" , "STRING" )]),
989- "Schema([{ 'name': 'name ', 'mode': ' NULLABLE', 'type': 'STRING'} ], None)" ,
946+ "Schema([SchemaField( 'name', 'STRING ', 'NULLABLE', None, None, (), None) ], None)" ,
990947 id = "repr without foreign type info" ,
991948 ),
992949 ],
@@ -998,7 +955,8 @@ def test_schema_iteration(self):
998955 schema = Schema (
999956 fields = [SchemaField ("name" , "STRING" ), SchemaField ("age" , "INTEGER" )]
1000957 )
1001- field_names = [field ["name" ] for field in schema ]
958+
959+ field_names = [field .name for field in schema ]
1002960 assert field_names == ["name" , "age" ]
1003961
1004962 def test_schema_object_mutability (self ): # Tests __setitem__ and __delitem__
@@ -1041,15 +999,19 @@ def test_schema_extend(self):
1041999 foreign_type_info = "TestInfo" ,
10421000 ),
10431001 {
1044- "fields" : [{"name" : "name" , "mode" : "NULLABLE" , "type" : "STRING" }],
1002+ "_fields" : [
1003+ SchemaField ("name" , "STRING" , "NULLABLE" , None , None , (), None )
1004+ ],
10451005 "foreignTypeInfo" : "TestInfo" ,
10461006 },
10471007 id = "repr with foreign type info" ,
10481008 ),
10491009 pytest .param (
10501010 Schema (fields = [SchemaField ("name" , "STRING" )]),
10511011 {
1052- "fields" : [{"name" : "name" , "mode" : "NULLABLE" , "type" : "STRING" }],
1012+ "_fields" : [
1013+ SchemaField ("name" , "STRING" , "NULLABLE" , None , None , (), None )
1014+ ],
10531015 "foreignTypeInfo" : None ,
10541016 },
10551017 id = "repr without foreign type info" ,
@@ -1064,35 +1026,25 @@ def test_to_api_repr(self, schema, expected_api_repr):
10641026 [
10651027 pytest .param (
10661028 {
1067- "fields " : [
1029+ "_fields " : [
10681030 SchemaField ("name" , "STRING" , "NULLABLE" , None , None , (), None )
10691031 ],
10701032 "foreignTypeInfo" : "TestInfo" ,
10711033 },
10721034 Schema (
1073- fields = [
1074- SchemaField (
1075- "name" , "STRING" , description = None , policy_tags = None
1076- )
1077- ],
1035+ fields = [SchemaField ("name" , "STRING" )],
10781036 foreign_type_info = "TestInfo" ,
10791037 ),
10801038 id = "repr with foreign type info" ,
10811039 ),
10821040 pytest .param (
10831041 {
1084- "fields " : [
1042+ "_fields " : [
10851043 SchemaField ("name" , "STRING" , "NULLABLE" , None , None , (), None )
10861044 ],
10871045 "foreignTypeInfo" : None ,
10881046 },
1089- Schema (
1090- fields = [
1091- SchemaField (
1092- "name" , "STRING" , description = None , policy_tags = None
1093- )
1094- ]
1095- ),
1047+ Schema (fields = [SchemaField ("name" , "STRING" )]),
10961048 id = "repr without foreign type info" ,
10971049 ),
10981050 ],
@@ -1104,11 +1056,13 @@ def test_from_api_repr(self, api_repr, expected):
11041056 THEN it will have the same representation a Schema object created
11051057 directly and displayed as a dict.
11061058 """
1107-
11081059 result = Schema .from_api_repr (api_repr )
11091060 assert result .to_api_repr () == expected .to_api_repr ()
11101061
11111062
1063+ # END PYTEST BASED SCHEMA TESTS ====================
1064+
1065+
11121066class TestPolicyTags (unittest .TestCase ):
11131067 @staticmethod
11141068 def _get_target_class ():
0 commit comments