@@ -1213,3 +1213,131 @@ def test_from_api_repr(self):
1213
1213
# We convert both to dict format because these classes do not have a
1214
1214
# __eq__() method to facilitate direct equality comparisons.
1215
1215
assert result .to_api_repr () == expected .to_api_repr ()
1216
+
1217
+
1218
+ class TestStorageDescriptor :
1219
+ """Tests for the StorageDescriptor class."""
1220
+
1221
+ @staticmethod
1222
+ def _get_target_class ():
1223
+ return schema .StorageDescriptor
1224
+
1225
+ def _make_one (self , * args , ** kwargs ):
1226
+ return self ._get_target_class ()(* args , ** kwargs )
1227
+
1228
+ serdeinfo_resource = {
1229
+ "serialization_library" : "testpath.to.LazySimpleSerDe" ,
1230
+ "name" : "serde_lib_name" ,
1231
+ "parameters" : {"key" : "value" },
1232
+ }
1233
+
1234
+ SERDEINFO = schema .SerDeInfo ("PLACEHOLDER" ).from_api_repr (serdeinfo_resource )
1235
+
1236
+ STORAGEDESCRIPTOR = {
1237
+ "inputFormat" : "testpath.to.OrcInputFormat" ,
1238
+ "locationUri" : "gs://test/path/" ,
1239
+ "outputFormat" : "testpath.to.OrcOutputFormat" ,
1240
+ "serDeInfo" : SERDEINFO .to_api_repr (),
1241
+ }
1242
+
1243
+ @pytest .mark .parametrize (
1244
+ "input_format,location_uri,output_format,serde_info" ,
1245
+ [
1246
+ (None , None , None , None ),
1247
+ ("testpath.to.OrcInputFormat" , None , None , None ),
1248
+ (None , "gs://test/path/" , None , None ),
1249
+ (None , None , "testpath.to.OrcOutputFormat" , None ),
1250
+ (None , None , None , SERDEINFO ),
1251
+ (
1252
+ "testpath.to.OrcInputFormat" ,
1253
+ "gs://test/path/" ,
1254
+ "testpath.to.OrcOutputFormat" ,
1255
+ SERDEINFO , # uses SERDEINFO class format
1256
+ ),
1257
+ (
1258
+ "testpath.to.OrcInputFormat" ,
1259
+ "gs://test/path/" ,
1260
+ "testpath.to.OrcOutputFormat" ,
1261
+ serdeinfo_resource , # uses api resource format (dict)
1262
+ ),
1263
+ ],
1264
+ )
1265
+ def test_ctor_valid_input (
1266
+ self , input_format , location_uri , output_format , serde_info
1267
+ ):
1268
+ storage_descriptor = self ._make_one (
1269
+ input_format = input_format ,
1270
+ location_uri = location_uri ,
1271
+ output_format = output_format ,
1272
+ serde_info = serde_info ,
1273
+ )
1274
+ assert storage_descriptor .input_format == input_format
1275
+ assert storage_descriptor .location_uri == location_uri
1276
+ assert storage_descriptor .output_format == output_format
1277
+ if isinstance (serde_info , schema .SerDeInfo ):
1278
+ assert (
1279
+ storage_descriptor .serde_info .to_api_repr () == serde_info .to_api_repr ()
1280
+ )
1281
+ elif isinstance (serde_info , dict ):
1282
+ assert storage_descriptor .serde_info .to_api_repr () == serde_info
1283
+ else :
1284
+ assert storage_descriptor .serde_info is None
1285
+
1286
+ @pytest .mark .parametrize (
1287
+ "input_format,location_uri,output_format,serde_info" ,
1288
+ [
1289
+ (123 , None , None , None ),
1290
+ (None , 123 , None , None ),
1291
+ (None , None , 123 , None ),
1292
+ (None , None , None , 123 ),
1293
+ ],
1294
+ )
1295
+ def test_ctor_invalid_input (
1296
+ self , input_format , location_uri , output_format , serde_info
1297
+ ):
1298
+ with pytest .raises (TypeError ) as e :
1299
+ self ._make_one (
1300
+ input_format = input_format ,
1301
+ location_uri = location_uri ,
1302
+ output_format = output_format ,
1303
+ serde_info = serde_info ,
1304
+ )
1305
+
1306
+ # Looking for the first word from the string "Pass <variable> as..."
1307
+ assert "Pass " in str (e .value )
1308
+
1309
+ def test_to_api_repr (self ):
1310
+ storage_descriptor = self ._make_one (
1311
+ input_format = "input_format" ,
1312
+ location_uri = "location_uri" ,
1313
+ output_format = "output_format" ,
1314
+ serde_info = self .SERDEINFO ,
1315
+ )
1316
+ expected_repr = {
1317
+ "inputFormat" : "input_format" ,
1318
+ "locationUri" : "location_uri" ,
1319
+ "outputFormat" : "output_format" ,
1320
+ "serDeInfo" : self .SERDEINFO .to_api_repr (),
1321
+ }
1322
+ assert storage_descriptor .to_api_repr () == expected_repr
1323
+
1324
+ def test_from_api_repr (self ):
1325
+ """GIVEN an api representation of a StorageDescriptor (i.e. STORAGEDESCRIPTOR)
1326
+ WHEN converted into a StorageDescriptor using from_api_repr() and
1327
+ displayed as a dict
1328
+ THEN it will have the same representation a StorageDescriptor created
1329
+ directly (via the _make_one() func) and displayed as a dict.
1330
+ """
1331
+
1332
+ # generate via STORAGEDESCRIPTOR
1333
+ resource = self .STORAGEDESCRIPTOR
1334
+ result = self ._get_target_class ().from_api_repr (resource )
1335
+ # result = klass.from_api_repr(resource)
1336
+
1337
+ expected = self ._make_one (
1338
+ input_format = "testpath.to.OrcInputFormat" ,
1339
+ location_uri = "gs://test/path/" ,
1340
+ output_format = "testpath.to.OrcOutputFormat" ,
1341
+ serde_info = self .SERDEINFO ,
1342
+ )
1343
+ assert result .to_api_repr () == expected .to_api_repr ()
0 commit comments