-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix for issue #904, also includes a test which checks the error case … #1248
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import sys | ||
| sys.path[0:0] = [""] | ||
| import unittest | ||
|
|
||
| from bson import SON | ||
| from mongoengine import * | ||
|
|
||
| __all__ = ("DbFieldParameterTest", ) | ||
|
|
||
|
|
||
| class DbFieldParameterTest(unittest.TestCase): | ||
|
|
||
| def setUp(self): | ||
| # testcase from https://github.com/MongoEngine/mongoengine/issues/904 | ||
| self.data = {'b': {'c': [{'x': 1.0, 'y': 2.0}]}} | ||
| self.reference_son = SON([('fb', SON([('fc', [SON([('fx', 1.0), ('fy', 2.0)])])]))]) | ||
|
|
||
| class C(EmbeddedDocument): | ||
| x = FloatField(db_field='fx') | ||
| y = FloatField(db_field='fy') | ||
|
|
||
| class B(EmbeddedDocument): | ||
| c = ListField(EmbeddedDocumentField(C), db_field='fc') | ||
|
|
||
| class A(Document): | ||
| b = EmbeddedDocumentField(B, db_field='fb') | ||
|
|
||
| self.A = A | ||
|
|
||
| def test_document_db_field_validation(self): | ||
| a = self.A(**self.data) | ||
| try: | ||
| a.validate() | ||
| except ValidationError as e: | ||
| self.fail("ValidationError raised: %s" % e.message) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to have try-except here. Can be replaced with |
||
|
|
||
| def test_document_fieldname_to_db_field_name(self): | ||
| a = self.A(**self.data) | ||
| self.assertEqual(a.to_mongo(), self.reference_son) | ||
|
|
||
| def test_document_db_fieldname_to_fieldname(self): | ||
| a = self.A._from_son(self.reference_son) | ||
| self.assertEqual(a.b.c[0].x, 1.0) | ||
| self.assertEqual(a.b.c[0].y, 2.0) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It would be better to add a docstring to the class above describing exactly what this set of tests ensures. Otherwise, we're forcing every dev to look up a (somewhat poorly worded) GitHub ticket in order to learn what this is about.