-
Notifications
You must be signed in to change notification settings - Fork 1.2k
added a feature to save object data in order #1497
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
added a feature to save object data in order #1497
Conversation
…trieving data in order
changed followings. - added optional parameter `container_class` which enables to choose intermediate class at encoding Python data, instead of additional field class. - removed OrderedDocument class because the equivalent feature could be implemented by the outside of Mongoengine.
…e implementation might be in ordered, but other one is not)
e73ae55 to
5957dc7
Compare
|
This looks good @userlocalhost! I'd still love to expand the tests to:
|
|
Thank you again for your review. I got it! But I think it takes a little time to get them done, because I realized that the field type is transformed to the |
Previous dereference implementation re-contains data as `dict` except for the predicted type. But the OrderedDict is not predicted, so the its data would be converted `dict` implicitly. As the result, the order of stored data get wrong. And this patch prevents it.
This checks DBRef conversion using DynamicField with the ordering guarantee.
…Field This tests DynamicField dereference with ordering guarantee.
I solved this problem by adding a condition in dereference processing (f2fe58c). Then, I wrote additional tests on the basis of your advice, @wojcikstefan. |
|
If there is no problem, could you please merge this? |
|
If some problems existed, I would correct them. So would you please review this, @thedrow? |
|
Thank you very much for your review, @thedrow! |
|
Thanks for looking at it @thedrow and sorry for stalling @userlocalhost, have been swamped with other responsibilities lately. I'll double-check this PR soon just in case. |
|
Thank you for merging, @thedrow! And thank you for your reply, @wojcikstefan. |
This thing broke after MongoEngine/mongoengine#1497 was merged.
wojcikstefan
left a comment
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.
This PR was merged prematurely. My bad for not being around and pushing back on both the implementation and the feature itself...
Having thought about it some more, I don't think it makes sense to support ordering in only one particular field type. We should either support it in the entirety of MongoEngine or not at all. I'll revert this PR now. Thanks for all the work anyway @userlocalhost !
I also see that the related PR where you wanted to use ordering (StackStorm/st2#3353 (comment)) hasn't been accepted and the maintainers have other ideas on how to solve the problem (including lists of dicts, which would already work in MongoEngine)
| def __init__(self, container_class=dict, *args, **kwargs): | ||
| self._container_cls = container_class | ||
| if not issubclass(self._container_cls, Mapping): | ||
| self.error('The class that is specified in `container_class` parameter ' |
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.
This shouldn't throw a ValidationError, which is reserved to invalid data, not an invalid field definition.
| else: | ||
| db_doc = pymongo_db.doc.find_one(as_class=OrderedDict) | ||
|
|
||
| self.assertEqual(','.join(doc.ordered_data.keys()), 'd,c,b,a') |
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.
This is an invalid test - it should verify the db_doc here, not the doc.
| self.assertEqual(','.join(doc.ordered_data.keys()), 'd,c,b,a') | ||
|
|
||
| def test_dynamicfield_with_wrong_container_class(self): | ||
| with self.assertRaises(ValidationError): |
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.
Again, ValidationError is a wrong error type.
| # This is because 'codec_options' is supported on pymongo3 or later | ||
| if IS_PYMONGO_3: | ||
| class OrderedDocument(Document): | ||
| my_metaclass = TopLevelDocumentMetaclass |
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.
You shouldn't have to override the metaclass here. I expect you did this because you ran into issues when subclassing the OrderedDocument. That's because the class isn't abstract and it doesn't allow inheritance. These two lines should be replaced with meta = {'abstract': True}.
| class DocWithInvalidField: | ||
| data = DynamicField(container_class=list) | ||
|
|
||
| def test_dynamicfield_with_wrong_container_class_and_reload_docuemnt(self): |
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.
This function name is incorrect - the container class is not wrong in this test.
| if IS_PYMONGO_3: | ||
| class OrderedDocument(Document): | ||
| my_metaclass = TopLevelDocumentMetaclass | ||
| __metaclass__ = TopLevelDocumentMetaclass |
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.
ditto - should be an abstract class instead.
I'm so sorry that I closed #1494 because I remove remote branch of previous PR by mistake.
I corrected the things that you mentioned. Cloud you please review this patch, @wojcikstefan?