def test_proxy_maybe_wrap(): mapping = { "http://somewhere.org/resources/foo": "foo", "http://somewhere.org/resources/bar": "bar", } proxy = ResourceMappingProxy.maybe_wrap(mapping) assert proxy.delegate is mapping assert ResourceMappingProxy.maybe_wrap(proxy) is proxy with pytest.raises(TypeError): ResourceMappingProxy.maybe_wrap([])
def test_proxy_hash_and_eq(): mapping = { "http://somewhere.org/resources/foo": "foo", "http://somewhere.org/resources/bar": "bar", } proxy1 = ResourceMappingProxy(mapping) proxy2 = ResourceMappingProxy(mapping, package_name="foo", package_version="1.2.3") assert proxy1 == proxy2 assert hash(proxy1) == hash(proxy2) assert proxy1 != mapping assert proxy2 != mapping
def test_proxy_properties(): mapping = { "http://somewhere.org/resources/foo": "foo", "http://somewhere.org/resources/bar": "bar", } proxy = ResourceMappingProxy(mapping, package_name="foo", package_version="1.2.3") assert len(proxy) == len(mapping) assert set(proxy.keys()) == set(mapping.keys()) for uri in mapping: assert proxy[uri] is mapping[uri] assert proxy.package_name == "foo" assert proxy.package_version == "1.2.3" assert proxy.class_name.endswith("dict")
def test_proxy_repr(): mapping = { "http://somewhere.org/resources/foo": "foo", "http://somewhere.org/resources/bar": "bar", } proxy = ResourceMappingProxy(mapping, package_name="foo", package_version="1.2.3") assert ".dict" in repr(proxy) assert "package: foo==1.2.3" in repr(proxy) assert "len: 2" in repr(proxy) empty_proxy = ResourceMappingProxy({}) assert ".dict" in repr(empty_proxy) assert "package: (none)" in repr(empty_proxy) assert "len: 0" in repr(empty_proxy)
def test_proxy_is_mapping(): assert isinstance(ResourceMappingProxy({}), Mapping)
def test_resource_mappings(): with asdf.config_context() as config: core_mappings = resource.get_core_resource_mappings() default_mappings = config.resource_mappings assert len(default_mappings) >= len(core_mappings) new_mapping = {"http://somewhere.org/schemas/foo-1.0.0": b"foo"} config.add_resource_mapping(new_mapping) assert len(config.resource_mappings) == len(default_mappings) + 1 assert any(m for m in config.resource_mappings if m.delegate is new_mapping) # Adding a mapping should be idempotent: config.add_resource_mapping(new_mapping) # ... even if wrapped: config.add_resource_mapping(ResourceMappingProxy(new_mapping)) assert len(config.resource_mappings) == len(default_mappings) + 1 # Adding a mapping should place it at the front of the line: front_mapping = {"http://somewhere.org/schemas/baz-1.0.0": b"baz"} config.add_resource_mapping(front_mapping) assert len(config.resource_mappings) == len(default_mappings) + 2 assert config.resource_mappings[0].delegate is front_mapping # ... even if the mapping is already in the list: config.add_resource_mapping(new_mapping) assert len(config.resource_mappings) == len(default_mappings) + 2 assert config.resource_mappings[0].delegate is new_mapping # Reset should get rid of any additions: config.reset_resources() assert len(config.resource_mappings) == len(default_mappings) # Should be able to remove a mapping: config.add_resource_mapping(new_mapping) config.remove_resource_mapping(new_mapping) assert len(config.resource_mappings) == len(default_mappings) # ... even if wrapped: config.add_resource_mapping(new_mapping) config.remove_resource_mapping(ResourceMappingProxy(new_mapping)) assert len(config.resource_mappings) == len(default_mappings) # ... and also by the name of the package the mappings came from: config.add_resource_mapping( ResourceMappingProxy(new_mapping, package_name="foo")) config.add_resource_mapping( ResourceMappingProxy( {"http://somewhere.org/schemas/bar-1.0.0": b"bar"}, package_name="foo")) config.remove_resource_mapping(package="foo") assert len(config.resource_mappings) == len(default_mappings) # Can combine the package and mapping filters when removing: config.add_resource_mapping( ResourceMappingProxy(new_mapping, package_name="foo")) config.remove_resource_mapping(new_mapping, package="foo") assert len(config.resource_mappings) == len(default_mappings) # But not omit both: with pytest.raises(ValueError): config.remove_resource_mapping() # Removing a mapping should be idempotent: config.add_resource_mapping(new_mapping) config.remove_resource_mapping(new_mapping) config.remove_resource_mapping(new_mapping) assert len(config.resource_mappings) == len(default_mappings)