def test_get_raises_if_profile_isnt_a_json(self): registry_path = test_helpers.fixture_path( 'registry_with_notajson_profile.json') registry = datapackage.registry.Registry(registry_path) with pytest.raises(RegistryError): registry.get('notajson-data-package')
def test_get_memoize_the_profiles(): registry_path = BASE_AND_TABULAR_REGISTRY_PATH registry = datapackage.registry.Registry(registry_path) registry.get('data-package') m = mock.mock_open(read_data='{}') with mock.patch('datapackage.registry.open', m): registry.get('data-package') assert not m.called, '.get() should memoize the profiles'
def test_get_raises_if_remote_profile_file_doesnt_exist(self): registry_url = 'http://example.com/registry.csv' registry_body = ( 'id,title,schema,specification,schema_path\r\n' 'base,Data Package,http://example.com/one.json,http://example.com,base.json' ) httpretty.register_uri(httpretty.GET, registry_url, body=registry_body) profile_url = 'http://example.com/one.json' httpretty.register_uri(httpretty.GET, profile_url, status=404) registry = datapackage.registry.Registry(registry_url) with pytest.raises(RegistryError): registry.get('base')
def test_get_loads_profile_from_disk(): httpretty.HTTPretty.allow_net_connect = False registry_path = BASE_AND_TABULAR_REGISTRY_PATH registry = datapackage.registry.Registry(registry_path) base_profile = registry.get('data-package') assert base_profile is not None assert base_profile['profile'] == 'data-package'
def _load_schema(self, schema, registry): the_schema = schema if isinstance(schema, six.string_types): try: the_schema = registry.get(schema) if not the_schema: if os.path.isfile(schema): with open(schema, 'r') as f: the_schema = json.load(f) else: req = requests.get(schema) req.raise_for_status() the_schema = req.json() except (IOError, ValueError, requests.exceptions.RequestException) as ex: message = 'Unable to load profile at "{0}"' six.raise_from( exceptions.ValidationError(message.format(schema)), ex ) elif isinstance(the_schema, dict): the_schema = copy.deepcopy(the_schema) else: message = 'Schema must be a "dict", but was a "{0}"' raise exceptions.ValidationError(message.format(type(the_schema).__name__)) return the_schema
def test_get_loads_remote_file_if_local_copy_doesnt_exist(self): registry_body = """ [ { "id": "data-package", "title": "Data Package", "schema": "http://example.com/one.json", "schema_path": "data-package.json", "specification": "https://specs.frictionlessdata.io/data-package/" } ] """ profile_url = 'http://example.com/one.json' profile_body = '{ "profile": "data-package" }' httpretty.register_uri(httpretty.GET, profile_url, body=profile_body) with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile: tmpfile.write(registry_body.encode('utf-8')) tmpfile.flush() registry = datapackage.registry.Registry(tmpfile.name) base_profile = registry.get('data-package') assert base_profile is not None assert base_profile == {'profile': 'data-package'}
def test_get_raises_if_local_profile_file_doesnt_exist(self): registry_body = ( 'id,title,schema,specification,schema_path\r\n' 'base,Data Package,http://example.com/one.json,http://example.com,inexistent.json' ) with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile: tmpfile.write(registry_body.encode('utf-8')) tmpfile.flush() registry = datapackage.registry.Registry(tmpfile.name) profile_url = 'http://example.com/one.json' httpretty.register_uri(httpretty.GET, profile_url, status=404) with pytest.raises(RegistryError): registry.get('base')
def _load_schema(self, schema, registry): the_schema = schema # For a reference: # https://frictionlessdata.io/specs/profiles/ if isinstance(schema, six.string_types): try: the_schema = registry.get(schema) if not the_schema: req = requests.get(schema) req.raise_for_status() the_schema = req.json() except (IOError, ValueError, requests.exceptions.RequestException) as ex: message = 'Unable to load profile at "{0}"' six.raise_from( exceptions.ValidationError(message.format(schema)), ex ) elif isinstance(the_schema, dict): the_schema = copy.deepcopy(the_schema) else: message = 'Schema must be a "dict", but was a "{0}"' raise exceptions.ValidationError(message.format(type(the_schema).__name__)) return the_schema
def _load_schema(self, schema, registry): the_schema = schema if isinstance(schema, six.string_types): try: the_schema = registry.get(schema) if not the_schema: if os.path.isfile(schema): with open(schema, 'r') as f: the_schema = json.load(f) else: req = requests.get(schema) req.raise_for_status() the_schema = req.json() except (IOError, ValueError, requests.exceptions.RequestException) as e: msg = 'Unable to load schema at "{0}"' six.raise_from(SchemaError(msg.format(schema)), e) elif isinstance(the_schema, dict): the_schema = copy.deepcopy(the_schema) else: msg = 'Schema must be a "dict", but was a "{0}"' raise SchemaError(msg.format(type(the_schema).__name__)) return the_schema
def test_get_raises_if_remote_profile_file_doesnt_exist(): registry_url = 'http://example.com/registry.json' registry_body = """ [ { "id": "data-package", "title": "Data Package", "schema": "http://example.com/one.json", "schema_path": "data-package.json", "specification": "http://example.com" } ] """ httpretty.register_uri(httpretty.GET, registry_url, body=registry_body) profile_url = 'http://example.com/one.json' httpretty.register_uri(httpretty.GET, profile_url, status=404) registry = datapackage.registry.Registry(registry_url) with pytest.raises(RegistryError): registry.get('data-package')
def test_get_raises_if_local_profile_file_doesnt_exist(): registry_body = """ [ { "id": "data-package", "title": "Data Package", "schema": "http://example.com/one.json", "schema_path": "inexistent.json", "specification": "http://example.com" } ] """ with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile: tmpfile.write(registry_body.encode('utf-8')) tmpfile.flush() registry = datapackage.registry.Registry(tmpfile.name) profile_url = 'http://example.com/one.json' httpretty.register_uri(httpretty.GET, profile_url, status=404) with pytest.raises(RegistryError): registry.get('data-package')
def test_get_loads_remote_file_if_local_copy_doesnt_exist(self): registry_body = ( 'id,title,schema,specification,schema_path\r\n' 'base,Data Package,http://example.com/one.json,http://example.com,inexistent.json' ) profile_url = 'http://example.com/one.json' profile_body = '{ "title": "base_profile" }' httpretty.register_uri(httpretty.GET, profile_url, body=profile_body) with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile: tmpfile.write(registry_body.encode('utf-8')) tmpfile.flush() registry = datapackage.registry.Registry(tmpfile.name) base_profile = registry.get('base') assert base_profile is not None assert base_profile == {'title': 'base_profile'}
def test_get_loads_remote_file_if_local_copy_doesnt_exist(): registry_body = """ [ { "id": "data-package", "title": "Data Package", "schema": "http://example.com/one.json", "schema_path": "data-package.json", "specification": "https://specs.frictionlessdata.io/data-package/" } ] """ profile_url = 'http://example.com/one.json' profile_body = '{ "profile": "data-package" }' httpretty.register_uri(httpretty.GET, profile_url, body=profile_body) with tempfile.NamedTemporaryFile(suffix='.csv') as tmpfile: tmpfile.write(registry_body.encode('utf-8')) tmpfile.flush() registry = datapackage.registry.Registry(tmpfile.name) base_profile = registry.get('data-package') assert base_profile is not None assert base_profile == {'profile': 'data-package'}
def test_get_returns_none_if_profile_doesnt_exist(): registry = datapackage.registry.Registry() assert registry.get('non-existent-profile') is None
def test_get_raises_if_profile_isnt_a_json(): registry_path = fixture_path('registry_with_notajson_profile.json') registry = datapackage.registry.Registry(registry_path) with pytest.raises(RegistryError): registry.get('notajson-data-package')
def test_get_raises_if_profile_isnt_a_json(self): registry_path = test_helpers.fixture_path('registry_with_notajson_profile.csv') registry = datapackage.registry.Registry(registry_path) with pytest.raises(RegistryError): registry.get('notajson')