def test_schema_as_wrong_object_type(self):
        '''Pass schema as not an expected object type (should be string or
        dict).'''

        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(self.dp, schema=123)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0], exceptions.SchemaError)
    def test_schema_as_valid_id(self):
        '''Pass schema as a valid string id to validate(). ID in registry.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        datapackage_validate.validate(self.dp, schema='base')
Exemple #3
0
 def run(self):
     """Run validation.
     """
     try:
         dv.validate(self.descriptor, self.config['schema'])
         self.success = True
     except dv.exceptions.DataPackageValidateException as e:
         self.success = False
         self.error = e.errors
     return self.success
def main(datapackage):
    try:
        datapackage_validate.validate(datapackage)
        # datapackage_validate.validate(datapackage, schema)
    except datapackage_validate.exceptions.DataPackageValidateException as e:
        print "Errors found in datapackage.json:"
        for err in e.errors:
            # only get the actual error msg
            err_msg = err[0]
            print "  - %s" % err_msg
    def test_raises_error_when_registry_raises_error(self, registry_mock):
        '''A 404 while getting the schema raises an error.'''
        registry_excep = datapackage_registry.exceptions
        registry_mock.side_effect = registry_excep.DataPackageRegistryException

        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(self.dp)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0], exceptions.RegistryError)
    def test_validate_empty_data_package(self):
        datapackage = {}
        schema = {
            'required': ['name'],
        }

        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(datapackage, schema)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0], exceptions.ValidationError)
    def test_schema_as_invalid_id(self):
        '''Pass schema as an invalid string id to validate(). ID not in
        registry.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)

        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(self.dp, schema='not-a-valid-id')

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0], exceptions.SchemaError)
 def test_datapackage_files_must_be_valid_fiscal_datapackage_files(self):
     for root, dirs, files in os.walk('.'):
         for file in files:
             if file == 'datapackage.json':
                 with open(os.path.join(root, file), 'r') as f:
                     fdp = json.load(f)
                 try:
                     datapackage_validate.validate(fdp, 'fiscal')
                 except datapackage_validate.exceptions.DataPackageValidateException as e:
                     msg = 'File "{0}" isn\'t a valid Fiscal Data Package: {1}'
                     errs = ','.join([str(err) for err in e.errors])
                     raise ValueError(msg.format(os.path.join(root, file), errs)) from e
    def test_valid_json_string(self):
        '''validate() returns True with no error messages.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        datapackage_json_str = """{
  "name": "basic-data-package",
  "title": "Basic Data Package"
}"""
        datapackage_validate.validate(datapackage_json_str)
    def test_valid_json_obj(self):
        '''Datapackage as valid Python dict.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        datapackage_json_str = """{
  "name": "basic-data-package",
  "title": "Basic Data Package"
}"""
        datapackage_obj = json.loads(datapackage_json_str)
        datapackage_validate.validate(datapackage_obj)
    def test_invalid_json_obj(self):
        '''Datapackage as invalid Python object.

        Datapackage is well-formed JSON, but doesn't validate against schema.
        '''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        datapackage_json_str = """{
  "asdf": "1234",
  "qwer": "abcd"
}"""
        datapackage_obj = json.loads(datapackage_json_str)
        valid, errors = datapackage_validate.validate(datapackage_obj)

        assert_false(valid)
        assert_true(errors)
        # Py2 returns u'name', Py3 doesn't. Test for either.
        assert_true("Schema ValidationError: u'name' is a required property"
                    in errors[0]
                    or
                    "Schema ValidationError: 'name' is a required property"
                    in errors[0])
    def test_schema_as_string(self):
        '''Pass schema as json string to validate()'''

        valid, errors = datapackage_validate.validate(
            self.dp, schema=_get_local_base_datapackage_schema())

        assert_true(valid)
        assert_false(errors)
def main(datapackage):
    # is it a dir or the json file?
    if os.path.isdir(datapackage):
        json_path = os.path.join(datapackage, "datapackage.json")
    elif os.path.exists(datapackage):
        json_path = datapackage
    json_contents = codecs.open(json_path, 'r', 'utf-8').read()

    try:
        datapackage_validate.validate(json_contents)
        # datapackage_validate.validate(json_contents, schema)
    except datapackage_validate.exceptions.DataPackageValidateException as e:
        click.echo("Errors found in datapackage.json:")
        for err in e.errors:
            # only get the actual error msg
            err_msg = err[0]
            click.echo("  - %s" % err_msg)
    def test_invalid_json_not_string(self):
        '''Datapackage isn't a JSON string. Return False with expected error
        message.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        # not a string
        invalid_datapackage_json_str = 123
        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(invalid_datapackage_json_str)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0],
                          exceptions.DataPackageValidateException)
    def test_schema_as_dict(self):
        '''Pass schema as python dict to validate()'''

        schema_dict = json.loads(_get_local_base_datapackage_schema())

        valid, errors = datapackage_validate.validate(self.dp,
                                                      schema=schema_dict)

        assert_true(valid)
        assert_false(errors)
    def test_invalid_json_string(self):
        '''Datapackage JSON string is not well formed. Retrn False with
        expected error message.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        # missing closing bracket
        invalid_datapackage_json_str = """{
  "name": "basic-data-package",
  "title": "Basic Data Package"
"""
        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(invalid_datapackage_json_str)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0],
                          exceptions.DataPackageValidateException)
    def test_schema_as_wrong_object_type(self):
        '''Pass schema as not an expected object type (should be string or
        dict).'''

        valid, errors = datapackage_validate.validate(
            self.dp, schema=123)

        assert_false(valid)
        assert_true(errors)
        assert_true('Invalid Schema: not a string or object'
                    in errors[0])
    def test_schema_as_invalid_id(self):
        '''Pass schema as an invalid string id to validate(). ID not in
        registry.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        valid, errors = datapackage_validate.validate(self.dp,
                                                      schema='not-a-valid-id')

        assert_false(valid)
        assert_true(errors)
        assert_true('Registry Error: no schema with id "not-a-valid-id"'
                    in errors[0])
    def test_registry_404_raises_error(self):
        '''A 404 while getting the registry raises an error.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body="404", status=404)

        valid, errors = datapackage_validate.validate(self.dp)

        assert_false(valid)
        assert_true(errors)
        assert_true('Registry Error: 404 Client Error: Not Found for url: '
                    'https://rawgit.com/dataprotocols/registry/master/registry.csv'
                    in errors[0])
    def test_invalid_json_obj(self):
        '''Datapackage as invalid Python dict.

        Datapackage is well-formed JSON, but doesn't validate against schema.
        '''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        datapackage_json_str = """{
  "asdf": "1234",
  "qwer": "abcd"
}"""
        datapackage_obj = json.loads(datapackage_json_str)
        with pytest.raises(exceptions.DataPackageValidateException) as excinfo:
            datapackage_validate.validate(datapackage_obj)

        assert excinfo.value.errors
        assert isinstance(excinfo.value.errors[0], exceptions.ValidationError)
        assert "'name' is a required property" in str(excinfo.value.errors[0])
    def test_registry_500_raises_error(self):
        '''A 500 while getting the registry raises an error.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body="500", status=500)

        valid, errors = datapackage_validate.validate(self.dp)

        assert_false(valid)
        assert_true(errors)
        assert_true('Registry Error: 500 Server Error: '
                    'Internal Server Error for url: '
                    'https://rawgit.com/dataprotocols/registry/master/registry.csv'
                    in errors[0])
    def test_schema_404_raises_error(self):
        '''A 404 while getting the schema raises an error.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body="404", status=404)

        valid, errors = datapackage_validate.validate(self.dp)

        assert_false(valid)
        assert_true(errors)
        assert_true('Registry Error: 404 Client Error: '
                    'Not Found for url: https://example.com/base_schema.json'
                    in errors[0])
    def test_schema_500_raises_error(self):
        '''A 500 while getting the schema raises an error.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body="500", status=500)

        valid, errors = datapackage_validate.validate(self.dp)

        assert_false(valid)
        assert_true(errors)
        assert_true('Registry Error: 500 Server Error: '
                    'Internal Server Error for url: '
                    'https://example.com/base_schema.json'
                    in errors[0])
    def test_invalid_json_not_string(self):
        '''Datapackage isn't a JSON string. Return False with expected error
        message.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        # not a string
        invalid_datapackage_json_str = 123
        valid, errors = datapackage_validate.validate(
            invalid_datapackage_json_str)

        assert_false(valid)
        assert_true(errors)
        assert_true('Invalid Data Package: not a string or object'
                    in errors[0])
    def test_invalid_json_string(self):
        '''Datapackage JSON string is not well formed. Retrn False with
        expected error message.'''
        httpretty.register_uri(httpretty.GET, REGISTRY_BACKEND_URL,
                               body=REGISTRY_BODY)
        httpretty.register_uri(httpretty.GET,
                               'https://example.com/base_schema.json',
                               body=_get_local_base_datapackage_schema())

        # missing closing bracket
        invalid_datapackage_json_str = """{
  "name": "basic-data-package",
  "title": "Basic Data Package"
"""
        valid, errors = datapackage_validate.validate(
            invalid_datapackage_json_str)

        assert_false(valid)
        assert_true(errors)
        assert_true('Invalid JSON:' in errors[0])
 def test_datapackage_json_validates(self):
     """datapackage.json: Test that datapackage.json validates"""
     for root, filename in self.find_datapackage_files():
         with open(os.path.join(root, filename)) as f:
             datapackagejson = json.load(f)
             datapackage_validate.validate(datapackagejson)
Exemple #27
0
 def test_datapackage_json_validates(self):
     """datapackage.json: Test that datapackage.json validates"""
     for root, filename in self.find_datapackage_files():
         with open(os.path.join(root, filename)) as f:
             datapackagejson = json.load(f)
             datapackage_validate.validate(datapackagejson)
    def test_schema_as_dict(self):
        '''Pass schema as python dict to validate()'''

        schema_dict = json.loads(_get_local_base_datapackage_schema())
        datapackage_validate.validate(self.dp, schema=schema_dict)
    def test_schema_as_string(self):
        '''Pass schema as json string to validate()'''

        schema_dict = _get_local_base_datapackage_schema()
        datapackage_validate.validate(self.dp, schema_dict)