|
| 1 | +import pytest |
| 2 | +import yaml |
| 3 | + |
| 4 | +from datacontract.data_contract import DataContract |
| 5 | + |
| 6 | + |
| 7 | +def document(**overrides): |
| 8 | + return { |
| 9 | + "apiVersion": "v3.2.0", |
| 10 | + "kind": "DataContract", |
| 11 | + "id": "test", |
| 12 | + "version": "1", |
| 13 | + "status": "active", |
| 14 | + "schema": [{"name": "orders", "properties": [{"name": "a"}, {"name": "b"}]}], |
| 15 | + **overrides, |
| 16 | + } |
| 17 | + |
| 18 | + |
| 19 | +def lint(doc, **kwargs): |
| 20 | + return DataContract(data_contract_str=yaml.safe_dump(doc), **kwargs).lint() |
| 21 | + |
| 22 | + |
| 23 | +def reasons(run): |
| 24 | + return [c.reason for c in run.checks if c.reason] |
| 25 | + |
| 26 | + |
| 27 | +def test_unique_names_are_valid(): |
| 28 | + assert lint(document()).result == "passed" |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize( |
| 32 | + "overrides, expected", |
| 33 | + [ |
| 34 | + ({"schema": [{"name": "orders"}, {"name": "orders"}]}, "schema[1].name duplicates schema[0].name ('orders')"), |
| 35 | + ( |
| 36 | + {"schema": [{"name": "orders", "properties": [{"name": "a"}, {"name": "a"}]}]}, |
| 37 | + "schema[0].properties[1].name duplicates schema[0].properties[0].name ('a')", |
| 38 | + ), |
| 39 | + ( |
| 40 | + {"schema": [{"name": "o", "properties": [{"name": "x", "properties": [{"name": "i"}, {"name": "i"}]}]}]}, |
| 41 | + "schema[0].properties[0].properties[1].name duplicates", |
| 42 | + ), |
| 43 | + ( |
| 44 | + {"schema": [{"name": "o", "properties": [{"name": "x", "items": {"properties": [{"name": "i"}] * 2}}]}]}, |
| 45 | + "schema[0].properties[0].items.properties[1].name duplicates", |
| 46 | + ), |
| 47 | + ( |
| 48 | + { |
| 49 | + "schema": [ |
| 50 | + {"name": "o", "properties": [{"name": "x", "map": {"value": {"properties": [{"name": "i"}] * 2}}}]} |
| 51 | + ] |
| 52 | + }, |
| 53 | + "schema[0].properties[0].map.value.properties[1].name duplicates", |
| 54 | + ), |
| 55 | + ( |
| 56 | + {"servers": [{"server": "prod", "type": "custom"}, {"server": "prod", "type": "custom"}]}, |
| 57 | + "servers[1].server duplicates servers[0].server ('prod')", |
| 58 | + ), |
| 59 | + ({"roles": [{"role": "r"}, {"role": "r"}]}, "roles[1].role duplicates roles[0].role"), |
| 60 | + ( |
| 61 | + {"schema": [{"name": "o", "properties": [{"name": "a", "id": "x"}, {"name": "b", "id": "x"}]}]}, |
| 62 | + "schema[0].properties[1].id duplicates schema[0].properties[0].id ('x')", |
| 63 | + ), |
| 64 | + ( |
| 65 | + {"schema": [{"name": "o", "quality": [{"id": "q", "type": "text"}, {"id": "q", "type": "text"}]}]}, |
| 66 | + "schema[0].quality[1].id duplicates schema[0].quality[0].id ('q')", |
| 67 | + ), |
| 68 | + ], |
| 69 | +) |
| 70 | +def test_duplicates_fail(overrides, expected): |
| 71 | + run = lint(document(**overrides)) |
| 72 | + assert run.result == "failed" |
| 73 | + assert any(expected in r for r in reasons(run)) |
| 74 | + |
| 75 | + |
| 76 | +def test_same_name_in_different_scopes_is_valid(): |
| 77 | + doc = document( |
| 78 | + schema=[ |
| 79 | + {"name": "orders", "properties": [{"name": "id"}, {"name": "o", "properties": [{"name": "id"}]}]}, |
| 80 | + {"name": "items", "properties": [{"name": "id"}]}, |
| 81 | + ] |
| 82 | + ) |
| 83 | + assert lint(doc).result == "passed" |
| 84 | + |
| 85 | + |
| 86 | +def test_repeated_sla_property_is_valid(): |
| 87 | + sla = [ |
| 88 | + {"property": "timeOfAvailability", "value": "09:00-08:00", "element": "orders.a", "driver": "regulatory"}, |
| 89 | + {"property": "timeOfAvailability", "value": "08:00-08:00", "element": "orders.a", "driver": "analytics"}, |
| 90 | + ] |
| 91 | + assert lint(document(slaProperties=sla)).result == "passed" |
| 92 | + |
| 93 | + |
| 94 | +def test_same_id_in_different_arrays_is_valid(): |
| 95 | + doc = document(schema=[{"name": "o", "id": "x", "properties": [{"name": "a", "id": "x"}]}]) |
| 96 | + assert lint(doc).result == "passed" |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "overrides", |
| 101 | + [ |
| 102 | + {"customProperties": [{"property": "related", "value": [{"id": "x", "v": 1}, {"id": "x", "v": 2}]}]}, |
| 103 | + {"schema": [{"name": "o", "properties": [{"name": "a", "examples": [{"id": "x"}, {"id": "x"}]}]}]}, |
| 104 | + ], |
| 105 | +) |
| 106 | +def test_repeated_id_in_free_form_values_is_valid(overrides): |
| 107 | + assert lint(document(**overrides)).result == "passed" |
| 108 | + |
| 109 | + |
| 110 | +def test_names_are_case_sensitive(): |
| 111 | + assert lint(document(schema=[{"name": "Orders"}, {"name": "orders"}])).result == "passed" |
| 112 | + |
| 113 | + |
| 114 | +def test_all_errors_reports_every_duplicate(): |
| 115 | + doc = document(schema=[{"name": "a"}, {"name": "a"}], servers=[{"server": "s"}, {"server": "s"}]) |
| 116 | + run = lint(doc, all_errors=True) |
| 117 | + assert run.result == "failed" |
| 118 | + assert any("schema[1].name" in r for r in reasons(run)) |
| 119 | + assert any("servers[1].server" in r for r in reasons(run)) |
| 120 | + |
| 121 | + |
| 122 | +def test_custom_schema_skips_duplicate_checks(tmp_path): |
| 123 | + custom = tmp_path / "custom.json" |
| 124 | + custom.write_text('{"type":"object"}') |
| 125 | + assert lint(document(), schema_location=str(custom)).result == "passed" |
| 126 | + schema = [{"name": "a", "id": "x"}, {"name": "a", "id": "x"}] |
| 127 | + assert lint(document(schema=schema), schema_location=str(custom)).result == "passed" |
0 commit comments