Skip to content

Conversation

@userlocalhost
Copy link
Contributor

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?

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)
@userlocalhost userlocalhost force-pushed the feature/order_guarantee branch 2 times, most recently from e73ae55 to 5957dc7 Compare March 1, 2017 09:39
@wojcikstefan
Copy link
Member

This looks good @userlocalhost! I'd still love to expand the tests to:

  1. See if the document is actually correctly stored in the database. You can do it by using raw PyMongo to query the collection with CodecOptions(document_class=OrderedDict).
  2. See if the order is maintained after doc.reload().

@userlocalhost
Copy link
Contributor Author

userlocalhost commented Mar 7, 2017

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 dict internally after calling doc.reload(). By this transformation, the order collapses.
I think I have to solve it, so please give me a time to do it.

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.
@userlocalhost
Copy link
Contributor Author

I realized that the field type is transformed to the dict internally after calling doc.reload(). By this transformation, the order collapses.

I solved this problem by adding a condition in dereference processing (f2fe58c).
And I added another test for it (9cd3dcd).

Then, I wrote additional tests on the basis of your advice, @wojcikstefan.

@userlocalhost
Copy link
Contributor Author

If there is no problem, could you please merge this?

@userlocalhost userlocalhost changed the title added a feature to save and load object data in order added a feature to save object data in order Mar 24, 2017
@userlocalhost
Copy link
Contributor Author

If some problems existed, I would correct them. So would you please review this, @thedrow?

@userlocalhost
Copy link
Contributor Author

Thank you very much for your review, @thedrow!
I corrected the points that you mentioned.

@thedrow thedrow merged commit 972ac73 into MongoEngine:master Apr 7, 2017
@wojcikstefan
Copy link
Member

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.

@userlocalhost
Copy link
Contributor Author

Thank you for merging, @thedrow!

And thank you for your reply, @wojcikstefan.
If there was something which you're worrying about, I would also correct it.

apolkosnik added a commit to apolkosnik/crits that referenced this pull request Apr 7, 2017
Copy link
Member

@wojcikstefan wojcikstefan left a 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 '
Copy link
Member

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')
Copy link
Member

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):
Copy link
Member

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
Copy link
Member

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):
Copy link
Member

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
Copy link
Member

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.

wojcikstefan added a commit that referenced this pull request May 8, 2017
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.

2 participants