Skip to content

Commit

Permalink
Add repr method better diagnostics (#23)
Browse files Browse the repository at this point in the history
* Add repr method better diagnostics

* Add schema validation error messages

* Bump requirements and related fixes
  • Loading branch information
mbachry committed Nov 7, 2018
1 parent 056664f commit 8c192f1
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 80 deletions.
3 changes: 3 additions & 0 deletions hedwig/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,6 @@ def as_dict(self) -> dict:
'metadata': self.metadata.as_dict(),
'data': self.data,
}

def __repr__(self) -> str:
return f'<Message type={self.type.value}/{self.format_version}>'
8 changes: 4 additions & 4 deletions hedwig/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

class MessageValidator(Draft4Validator):
# uuid separated by hyphens:
_human_uuid_re = re.compile("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
_human_uuid_re = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")

_version_pattern_re = re.compile("^[0-9]+\.\*$")
_version_pattern_re = re.compile(r"^[0-9]+\.\*$")

checker = FormatChecker()
"""
Expand Down Expand Up @@ -44,7 +44,7 @@ def validate(self, message) -> None:
Validates a message using JSON Schema
"""
if not message.schema.startswith(self.schema_root):
raise ValidationError
raise ValidationError(f'message schema must start with "{self.schema_root}"')

full_version = StrictVersion(message.schema.split('/')[-1])
major_version = full_version.version[0]
Expand All @@ -53,7 +53,7 @@ def validate(self, message) -> None:
try:
_, schema = self.resolver.resolve(schema_ptr)
except RefResolutionError:
raise ValidationError
raise ValidationError('definition not found in schema')

errors = list(self.iter_errors(message.data, schema))
if errors:
Expand Down
40 changes: 19 additions & 21 deletions requirements/dev-3.6-dev.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
alabaster==0.7.12 # via sphinx
appdirs==1.4.3 # via black
appnope==0.1.0 # via ipython
atomicwrites==1.2.1 # via pytest
attrs==18.2.0 # via black, pytest
babel==2.6.0 # via sphinx
backcall==0.1.0 # via ipython
black==18.9b0
boto3==1.9.16
botocore==1.12.16 # via boto3, s3transfer
certifi==2018.8.24 # via requests
boto3==1.9.39
botocore==1.12.39 # via boto3, s3transfer
certifi==2018.10.15 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black
coverage==4.5.1 # via coveralls, pytest-cov
Expand All @@ -17,45 +16,44 @@ decorator==4.3.0 # via ipython, traitlets
docopt==0.6.2 # via coveralls
docutils==0.14 # via botocore, sphinx
factory-boy==2.11.1
faker==0.9.1 # via factory-boy
flake8==3.5.0
faker==0.9.2 # via factory-boy
flake8==3.6.0
funcy==1.11
idna==2.7 # via requests
imagesize==1.1.0 # via sphinx
ipdb==0.11
ipython-genutils==0.2.0 # via traitlets
ipython==7.0.1 # via ipdb
ipython==7.1.1 # via ipdb
jedi==0.13.1 # via ipython
jinja2==2.10 # via sphinx
jmespath==0.9.3 # via boto3, botocore
jsonpointer==2.0
jsonschema==2.6.0
markupsafe==1.0 # via jinja2
markupsafe==1.1.0 # via jinja2
mccabe==0.6.1 # via flake8
more-itertools==4.3.0 # via pytest
mypy-extensions==0.4.1 # via mypy
mypy==0.630
mypy==0.641
packaging==18.0 # via sphinx
parso==0.3.1 # via jedi
pexpect==4.6.0 # via ipython
pickleshare==0.7.5 # via ipython
pluggy==0.7.1 # via pytest
prompt-toolkit==2.0.5 # via ipython
pluggy==0.8.0 # via pytest
prompt-toolkit==2.0.7 # via ipython
ptyprocess==0.6.0 # via pexpect
py==1.6.0 # via pytest
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
py==1.7.0 # via pytest
pycodestyle==2.4.0 # via flake8
pyflakes==2.0.0 # via flake8
pygments==2.2.0 # via ipython, sphinx
pyparsing==2.2.2 # via packaging
pyparsing==2.3.0 # via packaging
pytest-cov==2.6.0
pytest-env==0.6.2
pytest==3.8.2 # via pytest-cov, pytest-env
python-dateutil==2.7.3 # via botocore, faker
pytz==2018.5 # via babel
requests==2.19.1 # via coveralls, sphinx
pytest==3.10.0 # via pytest-cov, pytest-env
python-dateutil==2.7.5 # via botocore, faker
pytz==2018.7 # via babel
requests==2.20.0 # via coveralls, sphinx
retrying==1.3.3
s3transfer==0.1.13 # via boto3
simplegeneric==0.8.1 # via ipython
six==1.11.0 # via faker, more-itertools, packaging, prompt-toolkit, pytest, python-dateutil, retrying, sphinx, traitlets
snowballstemmer==1.2.1 # via sphinx
sphinx-autodoc-typehints==1.3.0
Expand All @@ -65,5 +63,5 @@ text-unidecode==1.2 # via faker
toml==0.10.0 # via black
traitlets==4.3.2 # via ipython
typed-ast==1.1.0 # via mypy
urllib3==1.23 # via botocore, requests
urllib3==1.24.1 # via botocore, requests
wcwidth==0.1.7 # via prompt-toolkit
40 changes: 19 additions & 21 deletions requirements/dev-3.6.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
alabaster==0.7.12 # via sphinx
appdirs==1.4.3 # via black
appnope==0.1.0 # via ipython
atomicwrites==1.2.1 # via pytest
attrs==18.2.0 # via black, pytest
babel==2.6.0 # via sphinx
backcall==0.1.0 # via ipython
black==18.9b0
boto3==1.9.16
botocore==1.12.16 # via boto3, s3transfer
certifi==2018.8.24 # via requests
boto3==1.9.39
botocore==1.12.39 # via boto3, s3transfer
certifi==2018.10.15 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black
coverage==4.5.1 # via coveralls, pytest-cov
Expand All @@ -17,45 +16,44 @@ decorator==4.3.0 # via ipython, traitlets
docopt==0.6.2 # via coveralls
docutils==0.14 # via botocore, sphinx
factory-boy==2.11.1
faker==0.9.1 # via factory-boy
flake8==3.5.0
faker==0.9.2 # via factory-boy
flake8==3.6.0
funcy==1.11
idna==2.7 # via requests
imagesize==1.1.0 # via sphinx
ipdb==0.11
ipython-genutils==0.2.0 # via traitlets
ipython==7.0.1 # via ipdb
ipython==7.1.1 # via ipdb
jedi==0.13.1 # via ipython
jinja2==2.10 # via sphinx
jmespath==0.9.3 # via boto3, botocore
jsonpointer==2.0
jsonschema==2.6.0
markupsafe==1.0 # via jinja2
markupsafe==1.1.0 # via jinja2
mccabe==0.6.1 # via flake8
more-itertools==4.3.0 # via pytest
mypy-extensions==0.4.1 # via mypy
mypy==0.630
mypy==0.641
packaging==18.0 # via sphinx
parso==0.3.1 # via jedi
pexpect==4.6.0 # via ipython
pickleshare==0.7.5 # via ipython
pluggy==0.7.1 # via pytest
prompt-toolkit==2.0.5 # via ipython
pluggy==0.8.0 # via pytest
prompt-toolkit==2.0.7 # via ipython
ptyprocess==0.6.0 # via pexpect
py==1.6.0 # via pytest
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
py==1.7.0 # via pytest
pycodestyle==2.4.0 # via flake8
pyflakes==2.0.0 # via flake8
pygments==2.2.0 # via ipython, sphinx
pyparsing==2.2.2 # via packaging
pyparsing==2.3.0 # via packaging
pytest-cov==2.6.0
pytest-env==0.6.2
pytest==3.8.2 # via pytest-cov, pytest-env
python-dateutil==2.7.3 # via botocore, faker
pytz==2018.5 # via babel
requests==2.19.1 # via coveralls, sphinx
pytest==3.10.0 # via pytest-cov, pytest-env
python-dateutil==2.7.5 # via botocore, faker
pytz==2018.7 # via babel
requests==2.20.0 # via coveralls, sphinx
retrying==1.3.3
s3transfer==0.1.13 # via boto3
simplegeneric==0.8.1 # via ipython
six==1.11.0 # via faker, more-itertools, packaging, prompt-toolkit, pytest, python-dateutil, retrying, sphinx, traitlets
snowballstemmer==1.2.1 # via sphinx
sphinx-autodoc-typehints==1.3.0
Expand All @@ -65,5 +63,5 @@ text-unidecode==1.2 # via faker
toml==0.10.0 # via black
traitlets==4.3.2 # via ipython
typed-ast==1.1.0 # via mypy
urllib3==1.23 # via botocore, requests
urllib3==1.24.1 # via botocore, requests
wcwidth==0.1.7 # via prompt-toolkit
40 changes: 19 additions & 21 deletions requirements/dev-3.7.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
alabaster==0.7.12 # via sphinx
appdirs==1.4.3 # via black
appnope==0.1.0 # via ipython
atomicwrites==1.2.1 # via pytest
attrs==18.2.0 # via black, pytest
babel==2.6.0 # via sphinx
backcall==0.1.0 # via ipython
black==18.9b0
boto3==1.9.16
botocore==1.12.16 # via boto3, s3transfer
certifi==2018.8.24 # via requests
boto3==1.9.39
botocore==1.12.39 # via boto3, s3transfer
certifi==2018.10.15 # via requests
chardet==3.0.4 # via requests
click==7.0 # via black
coverage==4.5.1 # via coveralls, pytest-cov
Expand All @@ -17,45 +16,44 @@ decorator==4.3.0 # via ipython, traitlets
docopt==0.6.2 # via coveralls
docutils==0.14 # via botocore, sphinx
factory-boy==2.11.1
faker==0.9.1 # via factory-boy
flake8==3.5.0
faker==0.9.2 # via factory-boy
flake8==3.6.0
funcy==1.11
idna==2.7 # via requests
imagesize==1.1.0 # via sphinx
ipdb==0.11
ipython-genutils==0.2.0 # via traitlets
ipython==7.0.1 # via ipdb
ipython==7.1.1 # via ipdb
jedi==0.13.1 # via ipython
jinja2==2.10 # via sphinx
jmespath==0.9.3 # via boto3, botocore
jsonpointer==2.0
jsonschema==2.6.0
markupsafe==1.0 # via jinja2
markupsafe==1.1.0 # via jinja2
mccabe==0.6.1 # via flake8
more-itertools==4.3.0 # via pytest
mypy-extensions==0.4.1 # via mypy
mypy==0.630
mypy==0.641
packaging==18.0 # via sphinx
parso==0.3.1 # via jedi
pexpect==4.6.0 # via ipython
pickleshare==0.7.5 # via ipython
pluggy==0.7.1 # via pytest
prompt-toolkit==2.0.5 # via ipython
pluggy==0.8.0 # via pytest
prompt-toolkit==2.0.7 # via ipython
ptyprocess==0.6.0 # via pexpect
py==1.6.0 # via pytest
pycodestyle==2.3.1 # via flake8
pyflakes==1.6.0 # via flake8
py==1.7.0 # via pytest
pycodestyle==2.4.0 # via flake8
pyflakes==2.0.0 # via flake8
pygments==2.2.0 # via ipython, sphinx
pyparsing==2.2.2 # via packaging
pyparsing==2.3.0 # via packaging
pytest-cov==2.6.0
pytest-env==0.6.2
pytest==3.8.2 # via pytest-cov, pytest-env
python-dateutil==2.7.3 # via botocore, faker
pytz==2018.5 # via babel
requests==2.19.1 # via coveralls, sphinx
pytest==3.10.0 # via pytest-cov, pytest-env
python-dateutil==2.7.5 # via botocore, faker
pytz==2018.7 # via babel
requests==2.20.0 # via coveralls, sphinx
retrying==1.3.3
s3transfer==0.1.13 # via boto3
simplegeneric==0.8.1 # via ipython
six==1.11.0 # via faker, more-itertools, packaging, prompt-toolkit, pytest, python-dateutil, retrying, sphinx, traitlets
snowballstemmer==1.2.1 # via sphinx
sphinx-autodoc-typehints==1.3.0
Expand All @@ -65,5 +63,5 @@ text-unidecode==1.2 # via faker
toml==0.10.0 # via black
traitlets==4.3.2 # via ipython
typed-ast==1.1.0 # via mypy
urllib3==1.23 # via botocore, requests
urllib3==1.24.1 # via botocore, requests
wcwidth==0.1.7 # via prompt-toolkit
18 changes: 7 additions & 11 deletions requirements/publish.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
bleach==3.0.0 # via readme-renderer
bleach==3.0.2 # via readme-renderer
bumpversion==0.5.3
certifi==2018.8.24 # via requests
cffi==1.11.5 # via cmarkgfm
certifi==2018.10.15 # via requests
chardet==3.0.4 # via requests
cmarkgfm==0.4.2 # via readme-renderer
docutils==0.14 # via readme-renderer
future==0.16.0 # via readme-renderer
idna==2.7 # via requests
pkginfo==1.4.2 # via twine
pycparser==2.19 # via cffi
pygments==2.2.0 # via readme-renderer
readme-renderer==22.0 # via twine
readme-renderer==24.0 # via twine
requests-toolbelt==0.8.0 # via twine
requests==2.19.1 # via requests-toolbelt, twine
requests==2.20.0 # via requests-toolbelt, twine
six==1.11.0 # via bleach, readme-renderer
tqdm==4.26.0 # via twine
tqdm==4.28.1 # via twine
twine==1.12.1
urllib3==1.23 # via requests
urllib3==1.24.1 # via requests
webencodings==0.5.1 # via bleach
wheel==0.32.1
wheel==0.32.2
3 changes: 3 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ def test_getter_timestamp(self, message):
def test_getter_publisher(self, message):
assert message.publisher == message.metadata.publisher

def test_repr(self, message):
assert repr(message) == f'<Message type={message.type.value}/{message.format_version}>'


@mock.patch('hedwig.models.boto3.client')
def test_get_sqs_client(mock_boto3_client):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ def test_validate(self):
self.validator.validate(message)

def test_validate_raises_error_invalid_schema(self):
with pytest.raises(ValidationError):
with pytest.raises(ValidationError) as e:
MessageFactory(schema='https://wrong.host/schema#/schemas/trip_created/1.0')
assert e.value.args[0] == 'message schema must start with "https://hedwig.automatic.com/schema"'

with pytest.raises(ValidationError):
with pytest.raises(ValidationError) as e:
MessageFactory(schema='https://hedwig.automatic.com/schema#/schemas/trip_created/9.0')
assert e.value.args[0] == 'definition not found in schema'

def test_validate_raises_errors(self):
with pytest.raises(ValidationError):
Expand Down

0 comments on commit 8c192f1

Please sign in to comment.