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

Refactoring validate() to handle resource data collections #5

Merged
merged 5 commits into from
May 13, 2016

Conversation

grahamu
Copy link
Contributor

@grahamu grahamu commented May 7, 2016

Now validate() yields either a validated resource or a resource generator callable,
depending on the value of new kwarg collection.

Now yields either a validated resource or a resource generator callable,
depending on the value of new kwarg `collection`.
@grahamu grahamu changed the title Refactoring validate() to deal with resource data collections Refactoring validate() to handle resource data collections May 7, 2016
@grahamu
Copy link
Contributor Author

grahamu commented May 7, 2016

Example use for relationship data collections:

        with self.validate(self.resource_class, collection=True) as resource_generator:
            tags = [resource.obj.name for resource in resource_generator()]
            self.object.tags.add(*tags)

@grahamu
Copy link
Contributor Author

grahamu commented May 9, 2016

@brosner sure would be nice to get this merged!

@coveralls
Copy link

coveralls commented May 11, 2016

Coverage Status

Changes Unknown when pulling 29830b2 on grahamu:all-purpose-validate into * on pinax:master*.

@contextlib.contextmanager needed all yield statements inside
a try block in order to properly catch validation errors arising
from resource manipulation. For instance, resource.save() may raise
attribute validation problems that resource.populate() does not.
@coveralls
Copy link

coveralls commented May 12, 2016

Coverage Status

Changes Unknown when pulling 4ba2a87 on grahamu:all-purpose-validate into * on pinax:master*.

@grahamu
Copy link
Contributor Author

grahamu commented May 12, 2016

Needs four types of tests.
Testing with other systems indicate this solution works for valid and invalid single resources, and valid resource collections. Invalid resource collections have not been tested. All four test types need to be added.

for resource_data in data["data"]:
yield self.validate_resource(resource_class, resource_data, obj)

yield gen_func
Copy link
Member

Choose a reason for hiding this comment

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

I think we went the wrong way here. The generator idea was a little ill-conceived on my part. However, I think the fix is very easy and even cleaner. I am changing my mind after seeing the resulting code for the validate caller. Having to call the generator is a bit ugly IMO.

The new idea is to simply change this line to:

yield (self.validate_resource(resource_class, resource_data, obj) for resource_data in data["data"])

The change to the caller is to remove the function call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very nice, much cleaner code both ways. Thanks.

@coveralls
Copy link

coveralls commented May 13, 2016

Coverage Status

Changes Unknown when pulling d1339c1 on grahamu:all-purpose-validate into * on pinax:master*.

@brosner brosner merged commit 8a1a7ce into pinax:master May 13, 2016
@grahamu grahamu deleted the all-purpose-validate branch May 13, 2016 17:46
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.

3 participants