예제 #1
0
    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')
예제 #2
0
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'
예제 #3
0
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'
예제 #4
0
    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')
예제 #5
0
    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')
예제 #6
0
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'
예제 #7
0
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'
예제 #8
0
    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
예제 #9
0
    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'}
예제 #10
0
    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')
예제 #11
0
    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
예제 #12
0
    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')
예제 #13
0
    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
예제 #14
0
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')
예제 #15
0
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')
예제 #16
0
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')
예제 #17
0
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')
예제 #18
0
    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'}
예제 #19
0
    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'}
예제 #20
0
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'}
예제 #21
0
def test_get_returns_none_if_profile_doesnt_exist():
    registry = datapackage.registry.Registry()
    assert registry.get('non-existent-profile') is None
예제 #22
0
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')
예제 #23
0
    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')
예제 #24
0
def test_get_returns_none_if_profile_doesnt_exist():
    registry = datapackage.registry.Registry()
    assert registry.get('non-existent-profile') is None