|
| 1 | +from functools import lru_cache |
| 2 | +import json |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from referencing import Registry, Resource, exceptions |
| 7 | +from referencing.jsonschema import DRAFT202012 |
| 8 | +from referencing.retrieval import to_cached_resource |
| 9 | + |
| 10 | + |
| 11 | +class TestToCachedResource: |
| 12 | + def test_it_caches_retrieved_resources(self): |
| 13 | + contents = {"$schema": "https://json-schema.org/draft/2020-12/schema"} |
| 14 | + stack = [json.dumps(contents)] |
| 15 | + |
| 16 | + @to_cached_resource() |
| 17 | + def retrieve(uri): |
| 18 | + return stack.pop() |
| 19 | + |
| 20 | + registry = Registry(retrieve=retrieve) |
| 21 | + |
| 22 | + expected = Resource.from_contents(contents) |
| 23 | + |
| 24 | + got = registry.get_or_retrieve("urn:example:schema") |
| 25 | + assert got.value == expected |
| 26 | + |
| 27 | + # And a second time we get the same value. |
| 28 | + again = registry.get_or_retrieve("urn:example:schema") |
| 29 | + assert again.value is got.value |
| 30 | + |
| 31 | + def test_custom_loader(self): |
| 32 | + contents = {"$schema": "https://json-schema.org/draft/2020-12/schema"} |
| 33 | + stack = [json.dumps(contents)[::-1]] |
| 34 | + |
| 35 | + @to_cached_resource(loads=lambda s: json.loads(s[::-1])) |
| 36 | + def retrieve(uri): |
| 37 | + return stack.pop() |
| 38 | + |
| 39 | + registry = Registry(retrieve=retrieve) |
| 40 | + |
| 41 | + expected = Resource.from_contents(contents) |
| 42 | + |
| 43 | + got = registry.get_or_retrieve("urn:example:schema") |
| 44 | + assert got.value == expected |
| 45 | + |
| 46 | + # And a second time we get the same value. |
| 47 | + again = registry.get_or_retrieve("urn:example:schema") |
| 48 | + assert again.value is got.value |
| 49 | + |
| 50 | + def test_custom_from_contents(self): |
| 51 | + contents = {} |
| 52 | + stack = [json.dumps(contents)] |
| 53 | + |
| 54 | + @to_cached_resource(from_contents=DRAFT202012.create_resource) |
| 55 | + def retrieve(uri): |
| 56 | + return stack.pop() |
| 57 | + |
| 58 | + registry = Registry(retrieve=retrieve) |
| 59 | + |
| 60 | + expected = DRAFT202012.create_resource(contents) |
| 61 | + |
| 62 | + got = registry.get_or_retrieve("urn:example:schema") |
| 63 | + assert got.value == expected |
| 64 | + |
| 65 | + # And a second time we get the same value. |
| 66 | + again = registry.get_or_retrieve("urn:example:schema") |
| 67 | + assert again.value is got.value |
| 68 | + |
| 69 | + def test_custom_cache(self): |
| 70 | + schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"} |
| 71 | + mapping = { |
| 72 | + "urn:example:1": dict(schema, foo=1), |
| 73 | + "urn:example:2": dict(schema, foo=2), |
| 74 | + "urn:example:3": dict(schema, foo=3), |
| 75 | + } |
| 76 | + |
| 77 | + resources = { |
| 78 | + uri: Resource.from_contents(contents) |
| 79 | + for uri, contents in mapping.items() |
| 80 | + } |
| 81 | + |
| 82 | + @to_cached_resource(cache=lru_cache(maxsize=2)) |
| 83 | + def retrieve(uri): |
| 84 | + return json.dumps(mapping.pop(uri)) |
| 85 | + |
| 86 | + registry = Registry(retrieve=retrieve) |
| 87 | + |
| 88 | + got = registry.get_or_retrieve("urn:example:1") |
| 89 | + assert got.value == resources["urn:example:1"] |
| 90 | + assert registry.get_or_retrieve("urn:example:1").value is got.value |
| 91 | + assert registry.get_or_retrieve("urn:example:1").value is got.value |
| 92 | + |
| 93 | + got = registry.get_or_retrieve("urn:example:2") |
| 94 | + assert got.value == resources["urn:example:2"] |
| 95 | + assert registry.get_or_retrieve("urn:example:2").value is got.value |
| 96 | + assert registry.get_or_retrieve("urn:example:2").value is got.value |
| 97 | + |
| 98 | + # This still succeeds, but evicts the first URI |
| 99 | + got = registry.get_or_retrieve("urn:example:3") |
| 100 | + assert got.value == resources["urn:example:3"] |
| 101 | + assert registry.get_or_retrieve("urn:example:3").value is got.value |
| 102 | + assert registry.get_or_retrieve("urn:example:3").value is got.value |
| 103 | + |
| 104 | + # And now this fails (as we popped the value out of `mapping`) |
| 105 | + with pytest.raises(exceptions.Unretrievable): |
| 106 | + registry.get_or_retrieve("urn:example:1") |
0 commit comments