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_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_doesnt_allow_changing_schema_properties(): schema_dict = { 'foo': 'bar', } schema = Profile(schema_dict) with pytest.raises(AttributeError): schema.foo = 'baz'
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_it_creates_properties_for_every_toplevel_attribute(): schema_dict = { 'foo': 'bar', 'baz': [], } schema = Profile(schema_dict) assert schema.foo == 'bar' assert schema.baz == []
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
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': []}
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)
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
def test_validate(): schema_dict = { 'properties': { 'name': { 'type': 'string', } }, 'required': ['name'], } data = { 'name': 'Sample Package', } schema = Profile(schema_dict) schema.validate(data)
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
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
def test_properties_are_visible_with_dir(): schema_dict = {'foo': {}} schema = Profile(schema_dict) assert 'foo' in dir(schema)
def test_raises_if_trying_to_access_inexistent_attribute(): schema_dict = {} schema = Profile(schema_dict) with pytest.raises(AttributeError): schema.this_doesnt_exist
def test_allow_changing_properties_not_in_schema(): schema_dict = {} schema = Profile(schema_dict) schema.foo = 'bar' assert schema.foo == 'bar'