New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
data type examples #143
data type examples #143
Conversation
Codecov Report
@@ Coverage Diff @@
## v0.2.0-dev #143 +/- ##
==============================================
+ Coverage 95.61% 95.78% +0.16%
==============================================
Files 40 42 +2
Lines 1940 2018 +78
==============================================
+ Hits 1855 1933 +78
Misses 85 85
Continue to review full report at Codecov.
|
1dbce35
to
a4a013d
Compare
ramlfications/models/data_types.py
Outdated
@@ -107,6 +107,23 @@ class RAMLDataType(object): | |||
|
|||
|
|||
@attr.s | |||
class Example(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be moved into ramlfications/models/parameters.py
- the data_types.py
module here is meant for actual Data Types; and parameters.py
is more for both named parameters and "sub" objects like query/uri params/body, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if there should be a ramlfications.models.examples
module for this, then: data types can also have examples, not only parameters.
ramlfications/models/data_types.py
Outdated
display_name = attr.ib(default=None) | ||
|
||
# Not sure when validation of examples should get done; leave for now. | ||
strict = attr.ib(default=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will need annotation_name
too - although they are not implemented yet, if it breaks tests, then either comment it out, or I think it'd work like attr.ib(init=False)
ramlfications/models/data_types.py
Outdated
|
||
""" | ||
value = attr.ib() | ||
name = attr.ib(default=None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think name
should be in repr
but no other attribute.
ramlfications/parser/parameters.py
Outdated
else: | ||
kwargs.update(parse_examples(raml_version, value)) | ||
else: | ||
kwargs.update(parse_examples(raml_version, value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, perhaps this would be cleaner:
if raml_version == "0.8":
kwargs["repeat"] = _get(value, "repeat", False)
if raml_version == "0.8" and isinstance(value, list):
# comment
pass
else:
kwargs.update(parse_examples(raml_version, value))
ramlfications/utils/types.py
Outdated
return data_type_cls(**data) | ||
|
||
|
||
def parse_examples(raml_version, data): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly with the Example model, I think this would be better in ramlfications/parser/parameters.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same response applies here as well, too,
# annotations or other facets of an example other than the value. | ||
# | ||
fp = get_form_param(api, "/with_example_structured") | ||
assert fp.example == {"value": "This whole map is a value."} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also assert that examples
doesn't exist.
|
||
def test_base_uri_param(api): | ||
p = api.base_uri_params[0] | ||
assert p.description.raw == "account name" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need to test other parts that are not a part of the example.
@@ -0,0 +1,78 @@ | |||
# -*- coding: utf-8 -*- | |||
from __future__ import absolute_import, division, print_function | |||
"""\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New line \
not needed
@@ -0,0 +1,121 @@ | |||
# -*- coding: utf-8 -*- | |||
from __future__ import absolute_import, division, print_function | |||
"""\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New line \
not needed
assert ex.description.raw == "Send this for a 500" | ||
assert ex.display_name.raw == "DON'T DO THIS" | ||
assert ex.strict is False | ||
assert ex.value == "breakfast" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test that example
and examples
return a runtime error?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few more nitpicks, but other than that, it looks good!
ramlfications/models/data_types.py
Outdated
|
||
# Not sure when validation of examples should get done; leave for now. | ||
strict = attr.ib(default=True) | ||
strict = attr.ib(default=True, repr=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you see my comment about annotation_name
?
ramlfications/utils/types.py
Outdated
raise RuntimeError("example and examples cannot co-exist") | ||
# TODO: Emit a lint warning during validation. | ||
raise InvalidRAMLStructureError( | ||
"example and examples cannot co-exist") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: can you add single quotes around the properties for readability? e.g. "'example' and 'examples' cannot co-exist"
ramlfications/utils/types.py
Outdated
examples = data["examples"] | ||
# Must be a map: | ||
if not isinstance(examples, dict): | ||
# Need to decide what exception to make this. | ||
raise UnknownDataTypeError | ||
raise InvalidRAMLStructureError("examples must be a map node") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, add '
around examples
ramlfications/utils/types.py
Outdated
if "displayName" in node: | ||
data["display_name"] = node["displayName"] | ||
if "strict" in node: | ||
data["strict"] = node["strict"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think with these things (side-stepping the eventual adding of NOTHING
or some type of Undefined
type), I think it'd be cleaner if it was like this:
data["description"] = BaseContent(node.get("description", ""))
data["display_name"] = node.get("displayName", name)
data["strict"] = node.get("strict", True)
You'd be able to remove the following 2 lines, too.
try: | ||
parse_raml(loaded_raml, config) | ||
except errors.InvalidRAMLStructureError as e: | ||
assert str(e) == 'example and examples cannot co-exist' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way to assert that an exception has been raised is not with a try/except but with a with pytest.raises
:
with pytest.raises(errors.InvalidRAMLStructureError) as e:
parse_raml(loaded_raml, config)
msg = # error message
assert msg in e.value.args[0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and same with the example below.
1d65e3f
to
f8ecf24
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
super tiny nit picks! sorry!
ramlfications/utils/types.py
Outdated
data["strict"] = node["strict"] | ||
if "display_name" not in data: | ||
data["display_name"] = name | ||
if isinstance(node, dict) and "value" in node: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
couldn't you also remove the and "value" in node:
and do similarly `data["value"] = node.get("value")? I know there has to be a value, but that can be caught in validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not in this case. When there's a value
key, the entire interpretation changes. If there isn't a value key, we already have the value, and we don't want to clobber it. Only when we have the value key can the other bits of metadata be present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh okay 👍
parse_raml(loaded_raml, config) | ||
except errors.InvalidRAMLStructureError as e: | ||
assert str(e) == 'example and examples cannot co-exist' | ||
assert str(e.value) == 'example and examples cannot co-exist' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
double quotes please
parse_raml(loaded_raml, config) | ||
except errors.InvalidRAMLStructureError as e: | ||
assert str(e) == 'examples must be a map node' | ||
assert str(e.value) == 'examples must be a map node' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here, double quotes
Ready for review again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just squash commits then I'll merge!
- introduce Example object for RAML 1.0 - use separate test files for 0.8 and 1.0; they will not have anything in common
2861544
to
a10fec3
Compare
Commits have been squashed. |
Awesome! :) |
in common