|
14 | 14 |
|
15 | 15 | import base64 |
16 | 16 | import copy |
| 17 | +from typing import Any, Dict, Optional |
17 | 18 | import unittest |
18 | 19 |
|
19 | 20 | from google.cloud.bigquery import external_config |
@@ -979,3 +980,139 @@ def test_from_api_repr(self): |
979 | 980 |
|
980 | 981 | assert isinstance(result, external_config.ExternalCatalogDatasetOptions) |
981 | 982 | assert result._properties == api_repr |
| 983 | + |
| 984 | + |
| 985 | +class TestExternalCatalogTableOptions: |
| 986 | + @staticmethod |
| 987 | + def _get_target_class(): |
| 988 | + from google.cloud.bigquery.external_config import ExternalCatalogTableOptions |
| 989 | + |
| 990 | + return ExternalCatalogTableOptions |
| 991 | + |
| 992 | + def _make_one(self, *args, **kw): |
| 993 | + return self._get_target_class()(*args, **kw) |
| 994 | + |
| 995 | + storage_descriptor_repr = { |
| 996 | + "inputFormat": "testpath.to.OrcInputFormat", |
| 997 | + "locationUri": "gs://test/path/", |
| 998 | + "outputFormat": "testpath.to.OrcOutputFormat", |
| 999 | + "serDeInfo": { |
| 1000 | + "serializationLibrary": "testpath.to.LazySimpleSerDe", |
| 1001 | + "name": "serde_lib_name", |
| 1002 | + "parameters": {"key": "value"}, |
| 1003 | + }, |
| 1004 | + } |
| 1005 | + |
| 1006 | + CONNECTIONID = "connection123" |
| 1007 | + PARAMETERS = {"key": "value"} |
| 1008 | + STORAGEDESCRIPTOR = schema.StorageDescriptor.from_api_repr(storage_descriptor_repr) |
| 1009 | + EXTERNALCATALOGTABLEOPTIONS = { |
| 1010 | + "connectionId": "connection123", |
| 1011 | + "parameters": {"key": "value"}, |
| 1012 | + "storageDescriptor": STORAGEDESCRIPTOR.to_api_repr(), |
| 1013 | + } |
| 1014 | + |
| 1015 | + @pytest.mark.parametrize( |
| 1016 | + "connection_id,parameters,storage_descriptor", |
| 1017 | + [ |
| 1018 | + ( |
| 1019 | + CONNECTIONID, |
| 1020 | + PARAMETERS, |
| 1021 | + STORAGEDESCRIPTOR, |
| 1022 | + ), # set all parameters at once |
| 1023 | + (CONNECTIONID, None, None), # set only one parameter at a time |
| 1024 | + (None, PARAMETERS, None), |
| 1025 | + (None, None, STORAGEDESCRIPTOR), # set storage descriptor using obj |
| 1026 | + (None, None, storage_descriptor_repr), # set storage descriptor using dict |
| 1027 | + (None, None, None), # use default parameters |
| 1028 | + ], |
| 1029 | + ) |
| 1030 | + def test_ctor_initialization( |
| 1031 | + self, |
| 1032 | + connection_id, |
| 1033 | + parameters, |
| 1034 | + storage_descriptor, |
| 1035 | + ): |
| 1036 | + instance = self._make_one( |
| 1037 | + connection_id=connection_id, |
| 1038 | + parameters=parameters, |
| 1039 | + storage_descriptor=storage_descriptor, |
| 1040 | + ) |
| 1041 | + |
| 1042 | + assert instance.connection_id == connection_id |
| 1043 | + assert instance.parameters == parameters |
| 1044 | + |
| 1045 | + if isinstance(storage_descriptor, schema.StorageDescriptor): |
| 1046 | + assert ( |
| 1047 | + instance.storage_descriptor.to_api_repr() |
| 1048 | + == storage_descriptor.to_api_repr() |
| 1049 | + ) |
| 1050 | + elif isinstance(storage_descriptor, dict): |
| 1051 | + assert instance.storage_descriptor.to_api_repr() == storage_descriptor |
| 1052 | + else: |
| 1053 | + assert instance.storage_descriptor is None |
| 1054 | + |
| 1055 | + @pytest.mark.parametrize( |
| 1056 | + "connection_id,parameters,storage_descriptor", |
| 1057 | + [ |
| 1058 | + pytest.param( |
| 1059 | + 123, |
| 1060 | + PARAMETERS, |
| 1061 | + STORAGEDESCRIPTOR, |
| 1062 | + id="connection_id-invalid-type", |
| 1063 | + ), |
| 1064 | + pytest.param( |
| 1065 | + CONNECTIONID, |
| 1066 | + 123, |
| 1067 | + STORAGEDESCRIPTOR, |
| 1068 | + id="parameters-invalid-type", |
| 1069 | + ), |
| 1070 | + pytest.param( |
| 1071 | + CONNECTIONID, |
| 1072 | + PARAMETERS, |
| 1073 | + 123, |
| 1074 | + id="storage_descriptor-invalid-type", |
| 1075 | + ), |
| 1076 | + ], |
| 1077 | + ) |
| 1078 | + def test_ctor_invalid_input( |
| 1079 | + self, |
| 1080 | + connection_id: str, |
| 1081 | + parameters: Dict[str, Any], |
| 1082 | + storage_descriptor: Optional[schema.StorageDescriptor], |
| 1083 | + ): |
| 1084 | + with pytest.raises(TypeError) as e: |
| 1085 | + external_config.ExternalCatalogTableOptions( |
| 1086 | + connection_id=connection_id, |
| 1087 | + parameters=parameters, |
| 1088 | + storage_descriptor=storage_descriptor, |
| 1089 | + ) |
| 1090 | + |
| 1091 | + # Looking for the first word from the string "Pass <variable> as..." |
| 1092 | + assert "Pass " in str(e.value) |
| 1093 | + |
| 1094 | + def test_to_api_repr(self): |
| 1095 | + instance = self._make_one( |
| 1096 | + connection_id=self.CONNECTIONID, |
| 1097 | + parameters=self.PARAMETERS, |
| 1098 | + storage_descriptor=self.STORAGEDESCRIPTOR, |
| 1099 | + ) |
| 1100 | + |
| 1101 | + result = instance.to_api_repr() |
| 1102 | + expected = self.EXTERNALCATALOGTABLEOPTIONS |
| 1103 | + |
| 1104 | + assert result == expected |
| 1105 | + |
| 1106 | + def test_from_api_repr(self): |
| 1107 | + result = self._make_one( |
| 1108 | + connection_id=self.CONNECTIONID, |
| 1109 | + parameters=self.PARAMETERS, |
| 1110 | + storage_descriptor=self.STORAGEDESCRIPTOR, |
| 1111 | + ) |
| 1112 | + |
| 1113 | + instance = self._make_one() |
| 1114 | + api_repr = self.EXTERNALCATALOGTABLEOPTIONS |
| 1115 | + result = instance.from_api_repr(api_repr) |
| 1116 | + |
| 1117 | + assert isinstance(result, external_config.ExternalCatalogTableOptions) |
| 1118 | + assert result._properties == api_repr |
0 commit comments