Compare commits
6 Commits
queryset-c
...
fix-strict
Author | SHA1 | Date | |
---|---|---|---|
|
c78e5079d4 | ||
|
ab69e50361 | ||
|
eb743beaa3 | ||
|
0007535a46 | ||
|
8391af026c | ||
|
800f656dcf |
@@ -438,7 +438,7 @@ class StrictDict(object):
|
|||||||
__slots__ = allowed_keys_tuple
|
__slots__ = allowed_keys_tuple
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in self.iterkeys())
|
return "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in self.items())
|
||||||
|
|
||||||
cls._classes[allowed_keys] = SpecificStrictDict
|
cls._classes[allowed_keys] = SpecificStrictDict
|
||||||
return cls._classes[allowed_keys]
|
return cls._classes[allowed_keys]
|
||||||
|
@@ -121,7 +121,7 @@ class BaseDocument(object):
|
|||||||
else:
|
else:
|
||||||
self._data[key] = value
|
self._data[key] = value
|
||||||
|
|
||||||
# Set any get_fieldname_display methods
|
# Set any get_<field>_display methods
|
||||||
self.__set_field_display()
|
self.__set_field_display()
|
||||||
|
|
||||||
if self._dynamic:
|
if self._dynamic:
|
||||||
@@ -1005,19 +1005,18 @@ class BaseDocument(object):
|
|||||||
return '.'.join(parts)
|
return '.'.join(parts)
|
||||||
|
|
||||||
def __set_field_display(self):
|
def __set_field_display(self):
|
||||||
"""Dynamically set the display value for a field with choices"""
|
"""For each field that specifies choices, create a
|
||||||
for attr_name, field in self._fields.items():
|
get_<field>_display method.
|
||||||
if field.choices:
|
"""
|
||||||
if self._dynamic:
|
fields_with_choices = [(n, f) for n, f in self._fields.items()
|
||||||
obj = self
|
if f.choices]
|
||||||
else:
|
for attr_name, field in fields_with_choices:
|
||||||
obj = type(self)
|
setattr(self,
|
||||||
setattr(obj,
|
'get_%s_display' % attr_name,
|
||||||
'get_%s_display' % attr_name,
|
partial(self.__get_field_display, field=field))
|
||||||
partial(self.__get_field_display, field=field))
|
|
||||||
|
|
||||||
def __get_field_display(self, field):
|
def __get_field_display(self, field):
|
||||||
"""Returns the display value for a choice field"""
|
"""Return the display value for a choice field"""
|
||||||
value = getattr(self, field.name)
|
value = getattr(self, field.name)
|
||||||
if field.choices and isinstance(field.choices[0], (list, tuple)):
|
if field.choices and isinstance(field.choices[0], (list, tuple)):
|
||||||
return dict(field.choices).get(value, value)
|
return dict(field.choices).get(value, value)
|
||||||
|
@@ -577,7 +577,7 @@ class EmbeddedDocumentField(BaseField):
|
|||||||
return self.document_type._fields.get(member_name)
|
return self.document_type._fields.get(member_name)
|
||||||
|
|
||||||
def prepare_query_value(self, op, value):
|
def prepare_query_value(self, op, value):
|
||||||
if not isinstance(value, self.document_type):
|
if value is not None and not isinstance(value, self.document_type):
|
||||||
value = self.document_type._from_son(value)
|
value = self.document_type._from_son(value)
|
||||||
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
|
super(EmbeddedDocumentField, self).prepare_query_value(op, value)
|
||||||
return self.to_mongo(value)
|
return self.to_mongo(value)
|
||||||
|
@@ -2,10 +2,8 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
sys.path[0:0] = [""]
|
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
from random import randint
|
|
||||||
|
|
||||||
from nose.plugins.skip import SkipTest
|
from nose.plugins.skip import SkipTest
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
@@ -17,11 +15,9 @@ __all__ = ("IndexesTest", )
|
|||||||
|
|
||||||
|
|
||||||
class IndexesTest(unittest.TestCase):
|
class IndexesTest(unittest.TestCase):
|
||||||
_MAX_RAND = 10 ** 10
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.db_name = 'mongoenginetest_IndexesTest_' + str(randint(0, self._MAX_RAND))
|
self.connection = connect(db='mongoenginetest')
|
||||||
self.connection = connect(db=self.db_name)
|
|
||||||
self.db = get_db()
|
self.db = get_db()
|
||||||
|
|
||||||
class Person(Document):
|
class Person(Document):
|
||||||
|
@@ -3001,28 +3001,32 @@ class FieldTest(unittest.TestCase):
|
|||||||
('S', 'Small'), ('M', 'Medium'), ('L', 'Large'),
|
('S', 'Small'), ('M', 'Medium'), ('L', 'Large'),
|
||||||
('XL', 'Extra Large'), ('XXL', 'Extra Extra Large')))
|
('XL', 'Extra Large'), ('XXL', 'Extra Extra Large')))
|
||||||
style = StringField(max_length=3, choices=(
|
style = StringField(max_length=3, choices=(
|
||||||
('S', 'Small'), ('B', 'Baggy'), ('W', 'wide')), default='S')
|
('S', 'Small'), ('B', 'Baggy'), ('W', 'Wide')), default='W')
|
||||||
|
|
||||||
Shirt.drop_collection()
|
Shirt.drop_collection()
|
||||||
|
|
||||||
shirt = Shirt()
|
shirt1 = Shirt()
|
||||||
|
shirt2 = Shirt()
|
||||||
|
|
||||||
self.assertEqual(shirt.get_size_display(), None)
|
# Make sure get_<field>_display returns the default value (or None)
|
||||||
self.assertEqual(shirt.get_style_display(), 'Small')
|
self.assertEqual(shirt1.get_size_display(), None)
|
||||||
|
self.assertEqual(shirt1.get_style_display(), 'Wide')
|
||||||
|
|
||||||
shirt.size = "XXL"
|
shirt1.size = 'XXL'
|
||||||
shirt.style = "B"
|
shirt1.style = 'B'
|
||||||
self.assertEqual(shirt.get_size_display(), 'Extra Extra Large')
|
shirt2.size = 'M'
|
||||||
self.assertEqual(shirt.get_style_display(), 'Baggy')
|
shirt2.style = 'S'
|
||||||
|
self.assertEqual(shirt1.get_size_display(), 'Extra Extra Large')
|
||||||
|
self.assertEqual(shirt1.get_style_display(), 'Baggy')
|
||||||
|
self.assertEqual(shirt2.get_size_display(), 'Medium')
|
||||||
|
self.assertEqual(shirt2.get_style_display(), 'Small')
|
||||||
|
|
||||||
# Set as Z - an invalid choice
|
# Set as Z - an invalid choice
|
||||||
shirt.size = "Z"
|
shirt1.size = 'Z'
|
||||||
shirt.style = "Z"
|
shirt1.style = 'Z'
|
||||||
self.assertEqual(shirt.get_size_display(), 'Z')
|
self.assertEqual(shirt1.get_size_display(), 'Z')
|
||||||
self.assertEqual(shirt.get_style_display(), 'Z')
|
self.assertEqual(shirt1.get_style_display(), 'Z')
|
||||||
self.assertRaises(ValidationError, shirt.validate)
|
self.assertRaises(ValidationError, shirt1.validate)
|
||||||
|
|
||||||
Shirt.drop_collection()
|
|
||||||
|
|
||||||
def test_simple_choices_validation(self):
|
def test_simple_choices_validation(self):
|
||||||
"""Ensure that value is in a container of allowed values.
|
"""Ensure that value is in a container of allowed values.
|
||||||
|
@@ -1238,7 +1238,8 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
self.assertFalse('$orderby' in q.get_ops()[0]['query'])
|
self.assertFalse('$orderby' in q.get_ops()[0]['query'])
|
||||||
|
|
||||||
def test_find_embedded(self):
|
def test_find_embedded(self):
|
||||||
"""Ensure that an embedded document is properly returned from a query.
|
"""Ensure that an embedded document is properly returned from
|
||||||
|
a query.
|
||||||
"""
|
"""
|
||||||
class User(EmbeddedDocument):
|
class User(EmbeddedDocument):
|
||||||
name = StringField()
|
name = StringField()
|
||||||
@@ -1249,16 +1250,31 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
post = BlogPost(content='Had a good coffee today...')
|
BlogPost.objects.create(
|
||||||
post.author = User(name='Test User')
|
author=User(name='Test User'),
|
||||||
post.save()
|
content='Had a good coffee today...'
|
||||||
|
)
|
||||||
|
|
||||||
result = BlogPost.objects.first()
|
result = BlogPost.objects.first()
|
||||||
self.assertTrue(isinstance(result.author, User))
|
self.assertTrue(isinstance(result.author, User))
|
||||||
self.assertEqual(result.author.name, 'Test User')
|
self.assertEqual(result.author.name, 'Test User')
|
||||||
|
|
||||||
|
def test_find_empty_embedded(self):
|
||||||
|
"""Ensure that you can save and find an empty embedded document."""
|
||||||
|
class User(EmbeddedDocument):
|
||||||
|
name = StringField()
|
||||||
|
|
||||||
|
class BlogPost(Document):
|
||||||
|
content = StringField()
|
||||||
|
author = EmbeddedDocumentField(User)
|
||||||
|
|
||||||
BlogPost.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
|
BlogPost.objects.create(content='Anonymous post...')
|
||||||
|
|
||||||
|
result = BlogPost.objects.get(author=None)
|
||||||
|
self.assertEqual(result.author, None)
|
||||||
|
|
||||||
def test_find_dict_item(self):
|
def test_find_dict_item(self):
|
||||||
"""Ensure that DictField items may be found.
|
"""Ensure that DictField items may be found.
|
||||||
"""
|
"""
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
|
||||||
|
|
||||||
|
|
||||||
@@ -13,6 +14,14 @@ class TestStrictDict(unittest.TestCase):
|
|||||||
d = self.dtype(a=1, b=1, c=1)
|
d = self.dtype(a=1, b=1, c=1)
|
||||||
self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
|
self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
|
||||||
|
|
||||||
|
def test_repr(self):
|
||||||
|
d = self.dtype(a=1, b=2, c=3)
|
||||||
|
self.assertEqual(repr(d), '{"a": 1, "b": 2, "c": 3}')
|
||||||
|
|
||||||
|
# make sure quotes are escaped properly
|
||||||
|
d = self.dtype(a='"', b="'", c="")
|
||||||
|
self.assertEqual(repr(d), '{"a": \'"\', "b": "\'", "c": \'\'}')
|
||||||
|
|
||||||
def test_init_fails_on_nonexisting_attrs(self):
|
def test_init_fails_on_nonexisting_attrs(self):
|
||||||
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))
|
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user