Skip to content
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

Merged
merged 1 commit into from May 24, 2017
Merged

Conversation

freddrake
Copy link
Collaborator

  • introduce Example object for RAML 1.0
  • use separate test files for 0.8 and 1.0; they will not have anything
    in common

@codecov-io
Copy link

codecov-io commented May 23, 2017

Codecov Report

Merging #143 into v0.2.0-dev will increase coverage by 0.16%.
The diff coverage is 100%.

Impacted file tree graph

@@              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
Impacted Files Coverage Δ
ramlfications/models/examples.py 100% <100%> (ø)
ramlfications/models/base.py 100% <100%> (ø) ⬆️
ramlfications/utils/types.py 93.18% <100%> (+0.32%) ⬆️
ramlfications/errors.py 100% <100%> (ø) ⬆️
ramlfications/utils/examples.py 100% <100%> (ø)
ramlfications/parser/parameters.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1cc8e26...2861544. Read the comment docs.

@@ -107,6 +107,23 @@ class RAMLDataType(object):


@attr.s
class Example(object):
Copy link
Contributor

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

Copy link
Collaborator Author

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.

display_name = attr.ib(default=None)

# Not sure when validation of examples should get done; leave for now.
strict = attr.ib(default=True)
Copy link
Contributor

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)


"""
value = attr.ib()
name = attr.ib(default=None)
Copy link
Contributor

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.

else:
kwargs.update(parse_examples(raml_version, value))
else:
kwargs.update(parse_examples(raml_version, value))
Copy link
Contributor

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))

return data_type_cls(**data)


def parse_examples(raml_version, data):
Copy link
Contributor

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

Copy link
Collaborator Author

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."}
Copy link
Contributor

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"
Copy link
Contributor

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
"""\
Copy link
Contributor

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
"""\
Copy link
Contributor

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"
Copy link
Contributor

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?

Copy link
Contributor

@econchick econchick left a 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!


# Not sure when validation of examples should get done; leave for now.
strict = attr.ib(default=True)
strict = attr.ib(default=True, repr=False)
Copy link
Contributor

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?

raise RuntimeError("example and examples cannot co-exist")
# TODO: Emit a lint warning during validation.
raise InvalidRAMLStructureError(
"example and examples cannot co-exist")
Copy link
Contributor

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"

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")
Copy link
Contributor

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

if "displayName" in node:
data["display_name"] = node["displayName"]
if "strict" in node:
data["strict"] = node["strict"]
Copy link
Contributor

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'
Copy link
Contributor

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]

Copy link
Contributor

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.

Copy link
Contributor

@econchick econchick left a 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!

data["strict"] = node["strict"]
if "display_name" not in data:
data["display_name"] = name
if isinstance(node, dict) and "value" in node:
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

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'
Copy link
Contributor

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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, double quotes

@freddrake
Copy link
Collaborator Author

Ready for review again.

Copy link
Contributor

@econchick econchick left a 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
@freddrake
Copy link
Collaborator Author

Commits have been squashed.

@econchick econchick merged commit 890396e into jdiegodcp:v0.2.0-dev May 24, 2017
@sichvoge
Copy link

Awesome! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants