def test_get_core_resource_mappings(uri): mappings = get_core_resource_mappings() mapping = next(m for m in mappings if uri in m) assert mapping is not None assert uri.encode("utf-8") in mapping[uri]
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 new_mapping in config.resource_mappings config.reset_resources() assert len(config.resource_mappings) == len(default_mappings)
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)