|
| 1 | +"""Unit tests for WtSite._replace_jsonld_context_mapping(). |
| 2 | +
|
| 3 | +Regression guard for #177: the method rewrote the context object it was given |
| 4 | +in place, so a caller that still needed its own dictionary got it modified. |
| 5 | +It must now rewrite a copy and leave the argument untouched. An argument of an |
| 6 | +unhandled type must be returned unchanged instead of an implicit None. |
| 7 | +""" |
| 8 | + |
| 9 | +import threading |
| 10 | + |
| 11 | +from osw.wtsite import WtSite |
| 12 | + |
| 13 | + |
| 14 | +class _FakeSite: |
| 15 | + """Stands in for mwclient.Site. Only the host is read.""" |
| 16 | + |
| 17 | + host = "example.org" |
| 18 | + |
| 19 | + |
| 20 | +def _make_fake_wtsite(): |
| 21 | + """A WtSite that performs no network calls.""" |
| 22 | + ws = WtSite.__new__(WtSite) |
| 23 | + ws._session_lock = threading.RLock() |
| 24 | + ws._site = _FakeSite() |
| 25 | + return ws |
| 26 | + |
| 27 | + |
| 28 | +def _params(): |
| 29 | + return WtSite.JsonLdContextLoaderParams(prefer_external_vocal=False) |
| 30 | + |
| 31 | + |
| 32 | +def test_dict_argument_is_not_modified(): |
| 33 | + ws = _make_fake_wtsite() |
| 34 | + given = { |
| 35 | + "label": {"@id": "skos:prefLabel"}, |
| 36 | + "label*": {"@id": "Property:HasLabel"}, |
| 37 | + "wiki": "https://original.example/id/", |
| 38 | + } |
| 39 | + |
| 40 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 41 | + |
| 42 | + assert result is not given |
| 43 | + assert given == { |
| 44 | + "label": {"@id": "skos:prefLabel"}, |
| 45 | + "label*": {"@id": "Property:HasLabel"}, |
| 46 | + "wiki": "https://original.example/id/", |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +def test_dict_argument_is_still_rewritten_in_the_result(): |
| 51 | + ws = _make_fake_wtsite() |
| 52 | + given = { |
| 53 | + "label": {"@id": "skos:prefLabel"}, |
| 54 | + "label*": {"@id": "Property:HasLabel"}, |
| 55 | + "wiki": "https://original.example/id/", |
| 56 | + } |
| 57 | + |
| 58 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 59 | + |
| 60 | + assert result["label"] == {"@id": "Property:HasLabel"} |
| 61 | + assert result["wiki"] == "https://example.org/id/" |
| 62 | + |
| 63 | + |
| 64 | +def test_string_mapping_is_rewritten_without_touching_the_argument(): |
| 65 | + ws = _make_fake_wtsite() |
| 66 | + given = {"label": "skos:prefLabel", "label*": "Property:HasLabel"} |
| 67 | + |
| 68 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 69 | + |
| 70 | + assert result["label"] == "Property:HasLabel" |
| 71 | + assert given["label"] == "skos:prefLabel" |
| 72 | + |
| 73 | + |
| 74 | +def test_nested_values_are_not_shared_with_the_argument(): |
| 75 | + ws = _make_fake_wtsite() |
| 76 | + nested = {"@id": "skos:prefLabel", "@type": "@id"} |
| 77 | + given = {"label": nested} |
| 78 | + |
| 79 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 80 | + |
| 81 | + assert result["label"] is not nested |
| 82 | + result["label"]["@id"] = "changed" |
| 83 | + assert nested["@id"] == "skos:prefLabel" |
| 84 | + |
| 85 | + |
| 86 | +def test_scoped_context_is_not_modified(): |
| 87 | + ws = _make_fake_wtsite() |
| 88 | + scoped = { |
| 89 | + "label": {"@id": "skos:prefLabel"}, |
| 90 | + "label*": {"@id": "Property:HasLabel"}, |
| 91 | + } |
| 92 | + given = {"statements": {"@context": scoped}} |
| 93 | + |
| 94 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 95 | + |
| 96 | + assert result["statements"]["label"] == {"@id": "Property:HasLabel"} |
| 97 | + assert scoped["label"] == {"@id": "skos:prefLabel"} |
| 98 | + |
| 99 | + |
| 100 | +def test_list_elements_are_not_modified(): |
| 101 | + ws = _make_fake_wtsite() |
| 102 | + element = { |
| 103 | + "label": {"@id": "skos:prefLabel"}, |
| 104 | + "label*": {"@id": "Property:HasLabel"}, |
| 105 | + } |
| 106 | + given = ["/wiki/Category:Entity?action=raw&slot=jsonschema", element] |
| 107 | + |
| 108 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 109 | + |
| 110 | + assert result is not given |
| 111 | + assert result[0] == "/wiki/Category:Entity?action=raw&slot=jsonschema" |
| 112 | + assert result[1] is not element |
| 113 | + assert result[1]["label"] == {"@id": "Property:HasLabel"} |
| 114 | + assert element["label"] == {"@id": "skos:prefLabel"} |
| 115 | + |
| 116 | + |
| 117 | +def test_list_value_inside_a_dict_is_not_modified(): |
| 118 | + ws = _make_fake_wtsite() |
| 119 | + element = { |
| 120 | + "label": {"@id": "skos:prefLabel"}, |
| 121 | + "label*": {"@id": "Property:HasLabel"}, |
| 122 | + } |
| 123 | + given = {"statements": [element]} |
| 124 | + |
| 125 | + result = ws._replace_jsonld_context_mapping(given, _params()) |
| 126 | + |
| 127 | + assert result["statements"][0]["label"] == {"@id": "Property:HasLabel"} |
| 128 | + assert element["label"] == {"@id": "skos:prefLabel"} |
| 129 | + |
| 130 | + |
| 131 | +def test_string_argument_is_returned_unchanged(): |
| 132 | + ws = _make_fake_wtsite() |
| 133 | + assert ws._replace_jsonld_context_mapping("schema:name", _params()) == "schema:name" |
| 134 | + |
| 135 | + |
| 136 | +def test_unhandled_type_is_returned_unchanged(): |
| 137 | + ws = _make_fake_wtsite() |
| 138 | + assert ws._replace_jsonld_context_mapping(42, _params()) == 42 |
| 139 | + assert ws._replace_jsonld_context_mapping(None, _params()) is None |
0 commit comments