|
| 1 | +from copy import deepcopy |
| 2 | + |
1 | 3 | import pytest |
2 | 4 |
|
| 5 | +import frictionless |
3 | 6 | from frictionless import Checklist, Detector, FrictionlessException, Schema, fields |
4 | 7 | from frictionless.resources import TableResource |
5 | 8 |
|
@@ -304,3 +307,69 @@ def test_resource_validate_less_actual_fields_with_required_constraint_issue_950 |
304 | 307 | [3, 3, "constraint-error"], |
305 | 308 | [3, 3, "missing-cell"], |
306 | 309 | ] |
| 310 | + |
| 311 | + |
| 312 | +def test_resource_with_missing_required_header_with_schema_sync_is_true_issue_1611(): |
| 313 | + schema_descriptor_1 = { |
| 314 | + "$schema": "https://frictionlessdata.io/schemas/table-schema.json", |
| 315 | + "fields": [ |
| 316 | + { |
| 317 | + "name": "A", |
| 318 | + "title": "Field A", |
| 319 | + "type": "string", |
| 320 | + "constraints": {"required": True}, |
| 321 | + }, |
| 322 | + {"name": "B", "title": "Field B", "type": "string"}, |
| 323 | + {"name": "C", "title": "Field C", "type": "string"}, |
| 324 | + ], |
| 325 | + } |
| 326 | + |
| 327 | + schema_descriptor_2 = deepcopy(schema_descriptor_1) |
| 328 | + # Add required constraint on "C" field |
| 329 | + schema_descriptor_2["fields"][2]["constraints"] = {"required": True} |
| 330 | + |
| 331 | + test_cases = [ |
| 332 | + { |
| 333 | + "schema": schema_descriptor_1, |
| 334 | + "source": [["B", "C"], ["b", "c"]], |
| 335 | + "expected_flattened_report": [ |
| 336 | + [None, 3, "A", "missing-label"], |
| 337 | + ], |
| 338 | + }, |
| 339 | + { |
| 340 | + "schema": schema_descriptor_2, |
| 341 | + "source": [["B"], ["b"]], |
| 342 | + "expected_flattened_report": [ |
| 343 | + [None, 2, "A", "missing-label"], |
| 344 | + [None, 3, "C", "missing-label"], |
| 345 | + ], |
| 346 | + }, |
| 347 | + { |
| 348 | + "schema": schema_descriptor_2, |
| 349 | + "source": [ |
| 350 | + ["A", "B"], |
| 351 | + ["a", "b"], |
| 352 | + ["a1"], |
| 353 | + ["a2", "b2"], |
| 354 | + [], |
| 355 | + ["a3", "b3", "c3"], |
| 356 | + ], |
| 357 | + "expected_flattened_report": [ |
| 358 | + [None, 3, "C", "missing-label"], |
| 359 | + [3, 2, "B", "missing-cell"], |
| 360 | + [5, None, None, "blank-row"], |
| 361 | + [6, 3, "", "extra-cell"], |
| 362 | + ], |
| 363 | + }, |
| 364 | + ] |
| 365 | + for tc in test_cases: |
| 366 | + schema = Schema.from_descriptor(tc["schema"]) |
| 367 | + resource = TableResource( |
| 368 | + tc["source"], schema=schema, detector=Detector(schema_sync=True) |
| 369 | + ) |
| 370 | + report = frictionless.validate(resource) |
| 371 | + print(report.flatten(["rowNumber", "fieldNumber", "fieldName", "type"])) |
| 372 | + assert ( |
| 373 | + report.flatten(["rowNumber", "fieldNumber", "fieldName", "type"]) |
| 374 | + == tc["expected_flattened_report"] |
| 375 | + ) |
0 commit comments