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

Implements excludes #137

Merged
merged 14 commits into from Aug 27, 2015
Merged

Implements excludes #137

merged 14 commits into from Aug 27, 2015

Conversation

calve
Copy link
Contributor

@calve calve commented Aug 19, 2015

As suggested in #132.

@nicolaiarocci
Copy link
Member

Excellent.

@@ -758,6 +758,17 @@ def _validate_valueschema(self, schema, field, value):
if len(validator.errors):
self._error(field, validator.errors)

def _validate_excludes(self, exclude, field, value):
targets = []
if isinstance(exclude, list):
Copy link
Member

Choose a reason for hiding this comment

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

please test for isinstance(exclude, Sequence) and not isinstance(exclude, _str_types)

@funkyfuture
Copy link
Member

please also add a check to SchemaError.validate for the excludes-rule. please look at #135 for the scheme i used to refactor schema-errors.

otherwise, lgtm. and i'm even baffled that required is dismissed correctly without extra effort. 8-)

... 'that_field': {'type': 'dict',
... 'excludes': 'this_field'}}
>>> v.validate({'this_field': {}, 'that_field': {}}, schema)
... False
Copy link
Member

Choose a reason for hiding this comment

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

that has to be

False

also true for all following output of returned objects. with leading dots it's interpreted as statement, not as expected result.

@calve
Copy link
Contributor Author

calve commented Aug 21, 2015

Thank you very much for the review. I considered your observations and pushed the appropriate changes.

However, I am not quite sure what you mean by

add a check to SchemaError.validate

I added a test for the error string. Please elaborate if this was not what you meant.

@funkyfuture
Copy link
Member

ah, sorry. i meant DefinitionSchema.validate; ensure that the defintion for an excludes-constraint is either a Hashable or a (non-string-)sequence of Hashables to avoid runtime-errors on validation.

@funkyfuture
Copy link
Member

though i don't see a use-case for it, you might add handling of the rule for unknown fields for the sake of completeness. that would let any arbritary field exclude one or more defined fields.

@@ -758,6 +758,16 @@ def _validate_valueschema(self, schema, field, value):
if len(validator.errors):
self._error(field, validator.errors)

def _validate_excludes(self, excludes, field, value):
if isinstance(excludes, _str_type):
Copy link
Member

Choose a reason for hiding this comment

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

should test for Hashable as any hashable object can be a mapping's key.

@nicolaiarocci
Copy link
Member

Could you please rebase this one of top of current master? Thanks!

Also pass flake8 test
They were interpreted as statement, not expected result
Namelly :

  - be consistent : name parameter `excludes` instead of `exclude`
  - if `excludes` is a string, simply wrap it in a list
  - test if `excludes` is a string with `_str_type`
@calve
Copy link
Contributor Author

calve commented Aug 25, 2015

Rebased.

Running tox test suite, those two errors are raised;

Document: usage
---------------
**********************************************************************
File "usage.rst", line 654, in default
Failed example:
    v.validate({'this_field': {}}, schema)
Expected:
    True
Got:
    False
**********************************************************************
File "usage.rst", line 656, in default
Failed example:
    v.validate({'that_field': {}}, schema)
Expected:
    True
Got:
    False
**********************************************************************

which are not consistent with the results expected by the test suite. Any idea how to fix those ?

@funkyfuture
Copy link
Member

in such cases i adapt the doctests as unittests to investigate.

@nicolaiarocci
Copy link
Member

For line 654:

>>> v.errors
{'that_field': 'required field'}

@nicolaiarocci
Copy link
Member

The unittest succeeds because assertSuccess passes update=True to the validate method. If update is False, then it will fail (as it happens to be in the doctest).

@calve
Copy link
Contributor Author

calve commented Aug 25, 2015

It looks like when both fields are required, the validatiion always fails. That makes sence, but the following test case should fails.

def test_required_excludes(self):
    schema = {'this_field': {'type': 'dict',
                             'excludes': 'that_field',
                             'required': True},
              'that_field': {'type': 'dict',
                             'excludes': 'this_field',
                             'required': True}}
    document1 = {'this_field': {}}
    document2 = {'that_field': {}}
    document3 = {'that_field': {}, 'this_field': {}}
    self.assertSuccess(document1, schema)
    self.assertSuccess(document2, schema)
    self.assertFail({}, schema)
    self.assertFail(document3, schema)

in particular the following lines :

    self.assertSuccess(document1, schema)
    self.assertSuccess(document2, schema)

Actually, the test suite still succeed if I changed those assertSuccess for assertFail

@nicolaiarocci
Copy link
Member

Yup see my comment on assertSuccess above

@calve
Copy link
Contributor Author

calve commented Aug 25, 2015

This makes sense. Do you have a suggestion about it ?

I am about to patch the assertSuccess method to accept an update parameter.

@nicolaiarocci
Copy link
Member

Patching assertSuccess is probably a good thing. You will also want to edit/change the usage.rst example.

Save a set of required fields, and ensure at least one of them is
present in the end
@calve calve force-pushed the master branch 2 times, most recently from 0aa7aac to 7504fc8 Compare August 25, 2015 16:19
@calve
Copy link
Contributor Author

calve commented Aug 25, 2015

Please have a look at the latest commit : running assertSuccess with update=False raised errors concerning mutually excluded and required fields (in the test_required_excludes function).

required fields were not properly dismissed, which should be fixed now.

@calve
Copy link
Contributor Author

calve commented Aug 26, 2015

Thanks for the review once again. I applied your suggestions.

@@ -818,6 +830,28 @@ def _validate_valueschema(self, schema, field, value):
if len(validator.errors):
self._error(field, validator.errors)

def _validate_excludes(self, excludes, field, value):
Copy link
Member

Choose a reason for hiding this comment

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

one thing i missed, within their functional grouped sections methods are sorted alphabetically. thus, this should be moved to line 578.

Keep definition validation method sorted alphabetically
@nicolaiarocci nicolaiarocci merged commit fa7b7b4 into pyeve:master Aug 27, 2015
nicolaiarocci added a commit that referenced this pull request Aug 27, 2015
@nicolaiarocci
Copy link
Member

Thanks!

@nicolaiarocci
Copy link
Member

PS: feel free to PR to replace your nickname if so you wish (could not get a hold of your name from the logs). Or I can edit it for you :)

@funkyfuture
Copy link
Member

great, thank you!

@calve
Copy link
Contributor Author

calve commented Aug 28, 2015

Thanks for all the reviews and the approval.

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

3 participants