@@ -871,6 +871,54 @@ def test__build_resource_w_custom_field_not_in__properties(self):
871
871
with self .assertRaises (ValueError ):
872
872
table ._build_resource (["bad" ])
873
873
874
+ def test_time_partitioning_getter (self ):
875
+ from google .cloud .bigquery .table import TimePartitioning
876
+ from google .cloud .bigquery .table import TimePartitioningType
877
+
878
+ dataset = DatasetReference (self .PROJECT , self .DS_ID )
879
+ table_ref = dataset .table (self .TABLE_NAME )
880
+ table = self ._make_one (table_ref )
881
+
882
+ table ._properties ["timePartitioning" ] = {
883
+ "type" : "DAY" ,
884
+ "field" : "col1" ,
885
+ "expirationMs" : "123456" ,
886
+ "requirePartitionFilter" : False ,
887
+ }
888
+ self .assertIsInstance (table .time_partitioning , TimePartitioning )
889
+ self .assertEqual (table .time_partitioning .type_ , TimePartitioningType .DAY )
890
+ self .assertEqual (table .time_partitioning .field , "col1" )
891
+ self .assertEqual (table .time_partitioning .expiration_ms , 123456 )
892
+ self .assertFalse (table .time_partitioning .require_partition_filter )
893
+
894
+ def test_time_partitioning_getter_w_none (self ):
895
+ dataset = DatasetReference (self .PROJECT , self .DS_ID )
896
+ table_ref = dataset .table (self .TABLE_NAME )
897
+ table = self ._make_one (table_ref )
898
+
899
+ table ._properties ["timePartitioning" ] = None
900
+ self .assertIsNone (table .time_partitioning )
901
+
902
+ del table ._properties ["timePartitioning" ]
903
+ self .assertIsNone (table .time_partitioning )
904
+
905
+ def test_time_partitioning_getter_w_empty (self ):
906
+ from google .cloud .bigquery .table import TimePartitioning
907
+
908
+ dataset = DatasetReference (self .PROJECT , self .DS_ID )
909
+ table_ref = dataset .table (self .TABLE_NAME )
910
+ table = self ._make_one (table_ref )
911
+
912
+ # Even though there are required properties according to the API
913
+ # specification, sometimes time partitioning is populated as an empty
914
+ # object. See internal bug 131167013.
915
+ table ._properties ["timePartitioning" ] = {}
916
+ self .assertIsInstance (table .time_partitioning , TimePartitioning )
917
+ self .assertIsNone (table .time_partitioning .type_ )
918
+ self .assertIsNone (table .time_partitioning .field )
919
+ self .assertIsNone (table .time_partitioning .expiration_ms )
920
+ self .assertIsNone (table .time_partitioning .require_partition_filter )
921
+
874
922
def test_time_partitioning_setter (self ):
875
923
from google .cloud .bigquery .table import TimePartitioning
876
924
from google .cloud .bigquery .table import TimePartitioningType
@@ -2211,6 +2259,20 @@ def test_constructor_explicit(self):
2211
2259
self .assertEqual (time_partitioning .expiration_ms , 10000 )
2212
2260
self .assertTrue (time_partitioning .require_partition_filter )
2213
2261
2262
+ def test_from_api_repr_empty (self ):
2263
+ klass = self ._get_target_class ()
2264
+
2265
+ # Even though there are required properties according to the API
2266
+ # specification, sometimes time partitioning is populated as an empty
2267
+ # object. See internal bug 131167013.
2268
+ api_repr = {}
2269
+ time_partitioning = klass .from_api_repr (api_repr )
2270
+
2271
+ self .assertIsNone (time_partitioning .type_ )
2272
+ self .assertIsNone (time_partitioning .field )
2273
+ self .assertIsNone (time_partitioning .expiration_ms )
2274
+ self .assertIsNone (time_partitioning .require_partition_filter )
2275
+
2214
2276
def test_from_api_repr_minimal (self ):
2215
2277
from google .cloud .bigquery .table import TimePartitioningType
2216
2278
@@ -2223,6 +2285,12 @@ def test_from_api_repr_minimal(self):
2223
2285
self .assertIsNone (time_partitioning .expiration_ms )
2224
2286
self .assertIsNone (time_partitioning .require_partition_filter )
2225
2287
2288
+ def test_from_api_repr_doesnt_override_type (self ):
2289
+ klass = self ._get_target_class ()
2290
+ api_repr = {"type" : "HOUR" }
2291
+ time_partitioning = klass .from_api_repr (api_repr )
2292
+ self .assertEqual (time_partitioning .type_ , "HOUR" )
2293
+
2226
2294
def test_from_api_repr_explicit (self ):
2227
2295
from google .cloud .bigquery .table import TimePartitioningType
2228
2296
0 commit comments