Beispiel #1
0
def test_init_loads_schema_from_dict():
    schema_dict = {
        'foo': 'bar'
    }
    schema = Profile(schema_dict)
    assert schema.to_dict().keys() == schema_dict.keys()
    assert schema.to_dict()['foo'] == schema_dict['foo']
def test_init_changing_the_original_schema_dict_doesnt_change_schema():
    schema_dict = {
        'foo': 'bar'
    }
    schema = Profile(schema_dict)
    schema_dict['bar'] = 'baz'
    assert 'bar' not in schema.to_dict()
def test_init_loads_schema_from_dict():
    schema_dict = {
        'foo': 'bar'
    }
    schema = Profile(schema_dict)
    assert schema.to_dict().keys() == schema_dict.keys()
    assert schema.to_dict()['foo'] == schema_dict['foo']
Beispiel #4
0
def test_doesnt_allow_changing_schema_properties():
    schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(schema_dict)
    with pytest.raises(AttributeError):
        schema.foo = 'baz'
Beispiel #5
0
def test_schema_properties_doesnt_linger_in_class():
    foo_schema_dict = {'foo': {}}
    bar_schema_dict = {'bar': {}}
    foo_schema = Profile(foo_schema_dict)
    bar_schema = Profile(bar_schema_dict)
    assert 'bar' not in dir(foo_schema)
    assert 'foo' not in dir(bar_schema)
def test_doesnt_allow_changing_schema_properties():
    schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(schema_dict)
    with pytest.raises(AttributeError):
        schema.foo = 'baz'
Beispiel #7
0
def test_init_changing_the_original_schema_dict_doesnt_change_schema():
    schema_dict = {
        'foo': 'bar'
    }
    schema = Profile(schema_dict)
    schema_dict['bar'] = 'baz'
    assert 'bar' not in schema.to_dict()
def test_to_dict_modifying_the_dict_doesnt_modify_the_schema():
    original_schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(original_schema_dict)
    schema_dict = schema.to_dict()
    schema_dict['bar'] = 'baz'
    assert 'bar' not in schema.to_dict()
Beispiel #9
0
def test_to_dict_modifying_the_dict_doesnt_modify_the_schema():
    original_schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(original_schema_dict)
    schema_dict = schema.to_dict()
    schema_dict['bar'] = 'baz'
    assert 'bar' not in schema.to_dict()
Beispiel #10
0
def test_iter_validation_returns_iter_with_each_validationerror():
    schema_dict = {
        'type': 'array',
        'items': {'enum': [1, 2, 3]},
        'maxItems': 2,
    }
    data_dict = [2, 3, 4]
    expected_errors_validators = ('maxItems', 'enum')
    schema = Profile(schema_dict)
    errors = [error for error in schema.iter_errors(data_dict)]
    assert len(errors) == 2
    for error in errors:
        assert error.validator in expected_errors_validators
Beispiel #11
0
def test_validate_should_raise_when_invalid():
    schema_dict = {
        'properties': {
            'name': {
                'type': 'string',
            }
        },
        'required': ['name'],
    }
    data = {}
    schema = Profile(schema_dict)
    with pytest.raises(exceptions.ValidationError):
        schema.validate(data)
Beispiel #12
0
def test_validate_should_raise_when_invalid():
    schema_dict = {
        'properties': {
            'name': {
                'type': 'string',
            }
        },
        'required': ['name'],
    }
    data = {}
    schema = Profile(schema_dict)
    with pytest.raises(exceptions.ValidationError):
        schema.validate(data)
Beispiel #13
0
def test_iter_validation_returns_iter_with_each_validationerror():
    schema_dict = {
        'type': 'array',
        'items': {'enum': [1, 2, 3]},
        'maxItems': 2,
    }
    data_dict = [2, 3, 4]
    expected_errors_validators = ('maxItems', 'enum')
    schema = Profile(schema_dict)
    errors = [error for error in schema.iter_errors(data_dict)]
    assert len(errors) == 2
    for error in errors:
        assert error.validator in expected_errors_validators
Beispiel #14
0
def test_validate():
    schema_dict = {
        'properties': {
            'name': {
                'type': 'string',
            }
        },
        'required': ['name'],
    }
    data = {
        'name': 'Sample Package',
    }
    schema = Profile(schema_dict)
    schema.validate(data)
Beispiel #15
0
def test_validate():
    schema_dict = {
        'properties': {
            'name': {
                'type': 'string',
            }
        },
        'required': ['name'],
    }
    data = {
        'name': 'Sample Package',
    }
    schema = Profile(schema_dict)
    schema.validate(data)
Beispiel #16
0
def test_it_creates_properties_for_every_toplevel_attribute():
    schema_dict = {
        'foo': 'bar',
        'baz': [],
    }
    schema = Profile(schema_dict)
    assert schema.foo == 'bar'
    assert schema.baz == []
Beispiel #17
0
def test_init_loads_schema_from_url():
    schema = {
        'foo': 'bar',
    }
    url = 'http://someplace.com/schema.json'
    body = json.dumps(schema)
    httpretty.register_uri(httpretty.GET, url, body=body, content_type='application/json')
    assert Profile(url).to_dict() == schema
Beispiel #18
0
def test_changing_properties_doesnt_change_the_originals():
    schema_dict = {
        'foo': {
            'bar': [],
        }
    }
    schema = Profile(schema_dict)
    schema.foo['bar'].append('baz')
    assert schema.foo == {'bar': []}
Beispiel #19
0
    def validate(self, raise_on_error=True, schema_only=True):
        """Validate a datapackage schema.

        By default, only the data-package schema is validated. To validate the
        data files too, set the `data` flag to `True`. The method fails if an
        error is found, unless the `raise_error` flag is explicitely set to
        `False`.

        :param raise_on_error: raise error on failure or not (default: True)
        :param schema_only: only validate the schema (default: True)
        :raise: :class:`ValidationError` if the schema is invalid

        :return A list of error messages or an empty list.
        """
        messages = []
        profile = Profile('fiscal-data-package')

        if raise_on_error:
            profile.validate(self.descriptor)
        else:
            try:
                profile.validate(self.descriptor)
                message = '%s (%s) is a valid fiscal data-package schema'
                log.info(message, self.path, self)
            except ValidationError as e:
                for error in e.errors:
                    message = 'SCHEMA ERROR in %s: %s'
                    args = self.path, error
                    messages.append(message % args)
                    log.warn(message, *args)

        if messages:
            messages.append('Aborting data validation due to invalid schema')
            return messages

        if not schema_only:
            return self._validate_data(raise_on_error)
        else:
            return messages
Beispiel #20
0
def test_init_raises_if_schema_is_invalid():
    invalid_schema = {
        'required': 51,
    }
    with pytest.raises(exceptions.ValidationError):
        Profile(invalid_schema)
Beispiel #21
0
def test_init_raises_if_schema_isnt_string_nor_dict():
    invalid_schema = []
    with pytest.raises(exceptions.ValidationError):
        Profile(invalid_schema)
Beispiel #22
0
def test_init_raises_if_url_doesnt_exist():
    url = 'https://inexistent-url.com/data-package.json'
    httpretty.register_uri(httpretty.GET, url, status=404)
    with pytest.raises(exceptions.ValidationError):
        Profile(url).to_dict()
Beispiel #23
0
def test_init_raises_if_url_isnt_a_json():
    url = 'https://someplace.com/data-package.csv'
    body = 'not a json'
    httpretty.register_uri(httpretty.GET, url, body=body)
    with pytest.raises(exceptions.ValidationError):
        Profile(url).to_dict()
Beispiel #24
0
def test_init_raises_if_path_isnt_a_json():
    with pytest.raises(exceptions.ValidationError):
        Profile('data/not_a_json')
Beispiel #25
0
def test_init_raises_if_path_doesnt_exist():
    with pytest.raises(exceptions.ValidationError):
        Profile('inexistent_schema.json')
Beispiel #26
0
def test_iter_validation_returns_no_errors_if_data_is_valid():
    schema_dict = {}
    data_dict = ''
    schema = Profile(schema_dict)
    errors = [error for error in schema.iter_errors(data_dict)]
    assert len(errors) == 0
Beispiel #27
0
def test_allow_changing_properties_not_in_schema():
    schema_dict = {}
    schema = Profile(schema_dict)
    schema.foo = 'bar'
    assert schema.foo == 'bar'
Beispiel #28
0
def test_properties_are_visible_with_dir():
    schema_dict = {'foo': {}}
    schema = Profile(schema_dict)
    assert 'foo' in dir(schema)
Beispiel #29
0
def test_raises_if_trying_to_access_inexistent_attribute():
    schema_dict = {}
    schema = Profile(schema_dict)
    with pytest.raises(AttributeError):
        schema.this_doesnt_exist
Beispiel #30
0
def test_allow_changing_properties_not_in_schema():
    schema_dict = {}
    schema = Profile(schema_dict)
    schema.foo = 'bar'
    assert schema.foo == 'bar'
Beispiel #31
0
def test_to_dict_converts_schema_to_dict():
    original_schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(original_schema_dict)
    assert schema.to_dict() == original_schema_dict
Beispiel #32
0
def test_init_loads_schema_from_path():
    assert Profile('data/empty_schema.json').to_dict() == {}
Beispiel #33
0
def test_iter_validation_returns_no_errors_if_data_is_valid():
    schema_dict = {}
    data_dict = ''
    schema = Profile(schema_dict)
    errors = [error for error in schema.iter_errors(data_dict)]
    assert len(errors) == 0
Beispiel #34
0
def test_to_dict_converts_schema_to_dict():
    original_schema_dict = {
        'foo': 'bar',
    }
    schema = Profile(original_schema_dict)
    assert schema.to_dict() == original_schema_dict