test_retrieval.py 3.6 KB

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