|
21 | 21 | HasColumnTests, |
22 | 22 | UnparsedColumn, |
23 | 23 | UnparsedConversionTypeParams, |
| 24 | + UnparsedDerivedDimensionV2, |
24 | 25 | UnparsedDocumentationFile, |
25 | 26 | UnparsedExposure, |
26 | 27 | UnparsedMacro, |
@@ -1140,7 +1141,8 @@ def test_column_parse(): |
1140 | 1141 |
|
1141 | 1142 |
|
1142 | 1143 | class TestUnparsedColumnTimeDimensionGranularityValidation(ContractTestCase): |
1143 | | - """Test validation that SL YAML V2 column with time dimension must specify granularity.""" |
| 1144 | + """Test validation that SL YAML V2 column with time dimension must specify granularity, |
| 1145 | + and that dimension validity_params require granularity.""" |
1144 | 1146 |
|
1145 | 1147 | ContractType = UnparsedColumn |
1146 | 1148 |
|
@@ -1189,3 +1191,80 @@ def test_non_time_dimension_string_passes_without_granularity(self): |
1189 | 1191 | col = self.ContractType.from_dict(column_dict) |
1190 | 1192 | self.assertEqual(col.name, "category") |
1191 | 1193 | self.assertIsNone(col.granularity) |
| 1194 | + |
| 1195 | + def test_dimension_with_validity_params_without_granularity_fails_validation(self): |
| 1196 | + """Dimension (dict) with validity_params must have column granularity.""" |
| 1197 | + column_dict = { |
| 1198 | + "name": "valid_from", |
| 1199 | + "dimension": { |
| 1200 | + "type": "time", |
| 1201 | + "name": "valid_from_dim", |
| 1202 | + "validity_params": {"is_start": True, "is_end": False}, |
| 1203 | + }, |
| 1204 | + } |
| 1205 | + self.assert_fails_validation(column_dict) |
| 1206 | + |
| 1207 | + def test_dimension_with_validity_params_with_granularity_passes_validation(self): |
| 1208 | + """Dimension (dict) with validity_params and granularity passes.""" |
| 1209 | + column_dict = { |
| 1210 | + "name": "valid_from", |
| 1211 | + "granularity": "day", |
| 1212 | + "dimension": { |
| 1213 | + "type": "time", |
| 1214 | + "name": "valid_from_dim", |
| 1215 | + "validity_params": {"is_start": True, "is_end": True}, |
| 1216 | + }, |
| 1217 | + } |
| 1218 | + col = self.ContractType.from_dict(column_dict) |
| 1219 | + self.assertEqual(col.granularity, "day") |
| 1220 | + self.assertEqual(col.name, "valid_from") |
| 1221 | + |
| 1222 | + def test_dimension_without_validity_params_passes_without_granularity_when_not_time(self): |
| 1223 | + """Dimension (dict) without validity_params and not time does not require granularity.""" |
| 1224 | + column_dict = { |
| 1225 | + "name": "category", |
| 1226 | + "dimension": {"type": "categorical", "name": "category_dim"}, |
| 1227 | + } |
| 1228 | + col = self.ContractType.from_dict(column_dict) |
| 1229 | + self.assertEqual(col.name, "category") |
| 1230 | + self.assertIsNone(col.granularity) |
| 1231 | + |
| 1232 | + |
| 1233 | +class TestUnparsedDerivedDimensionV2ValidityParamsValidation(ContractTestCase): |
| 1234 | + """Test validation that UnparsedDerivedDimensionV2 only has validity_params when it has granularity.""" |
| 1235 | + |
| 1236 | + ContractType = UnparsedDerivedDimensionV2 |
| 1237 | + |
| 1238 | + def test_derived_dimension_with_validity_params_without_granularity_fails_validation(self): |
| 1239 | + """Derived dimension with validity_params must have granularity.""" |
| 1240 | + dimension_dict = { |
| 1241 | + "type": "time", |
| 1242 | + "name": "valid_from_dim", |
| 1243 | + "expr": "valid_from", |
| 1244 | + "validity_params": {"is_start": True, "is_end": False}, |
| 1245 | + } |
| 1246 | + self.assert_fails_validation(dimension_dict) |
| 1247 | + |
| 1248 | + def test_derived_dimension_with_validity_params_with_granularity_passes_validation(self): |
| 1249 | + """Derived dimension with validity_params and granularity passes.""" |
| 1250 | + dimension_dict = { |
| 1251 | + "type": "time", |
| 1252 | + "name": "valid_from_dim", |
| 1253 | + "expr": "valid_from", |
| 1254 | + "granularity": "day", |
| 1255 | + "validity_params": {"is_start": True, "is_end": True}, |
| 1256 | + } |
| 1257 | + dim = self.ContractType.from_dict(dimension_dict) |
| 1258 | + self.assertEqual(dim.granularity, "day") |
| 1259 | + self.assertEqual(dim.name, "valid_from_dim") |
| 1260 | + |
| 1261 | + def test_derived_dimension_without_validity_params_passes_without_granularity(self): |
| 1262 | + """Derived dimension without validity_params does not require granularity.""" |
| 1263 | + dimension_dict = { |
| 1264 | + "type": "time", |
| 1265 | + "name": "some_dim", |
| 1266 | + "expr": "some_column", |
| 1267 | + } |
| 1268 | + dim = self.ContractType.from_dict(dimension_dict) |
| 1269 | + self.assertEqual(dim.name, "some_dim") |
| 1270 | + self.assertIsNone(dim.granularity) |
0 commit comments