Mongomallard
This commit is contained in:
parent
ede9fcfb00
commit
dcc5d3c858
24 changed files with 584 additions and 1102 deletions
|
|
@ -56,10 +56,11 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(person.userid, person.userid)
|
||||
self.assertEqual(person.created, person.created)
|
||||
|
||||
self.assertEqual(person._data['name'], person.name)
|
||||
self.assertEqual(person._data['age'], person.age)
|
||||
self.assertEqual(person._data['userid'], person.userid)
|
||||
self.assertEqual(person._data['created'], person.created)
|
||||
data = person.to_dict()
|
||||
self.assertEqual(data['name'], person.name)
|
||||
self.assertEqual(data['age'], person.age)
|
||||
self.assertEqual(data['userid'], person.userid)
|
||||
self.assertEqual(data['created'], person.created)
|
||||
|
||||
# Confirm introspection changes nothing
|
||||
data_to_be_saved = sorted(person.to_mongo().keys())
|
||||
|
|
@ -88,10 +89,11 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(person.userid, person.userid)
|
||||
self.assertEqual(person.created, person.created)
|
||||
|
||||
self.assertEqual(person._data['name'], person.name)
|
||||
self.assertEqual(person._data['age'], person.age)
|
||||
self.assertEqual(person._data['userid'], person.userid)
|
||||
self.assertEqual(person._data['created'], person.created)
|
||||
data = person.to_dict()
|
||||
self.assertEqual(data['name'], person.name)
|
||||
self.assertEqual(data['age'], person.age)
|
||||
self.assertEqual(data['userid'], person.userid)
|
||||
self.assertEqual(data['created'], person.created)
|
||||
|
||||
# Confirm introspection changes nothing
|
||||
data_to_be_saved = sorted(person.to_mongo().keys())
|
||||
|
|
@ -123,10 +125,12 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(person.userid, person.userid)
|
||||
self.assertEqual(person.created, person.created)
|
||||
|
||||
self.assertEqual(person._data['name'], person.name)
|
||||
self.assertEqual(person._data['age'], person.age)
|
||||
self.assertEqual(person._data['userid'], person.userid)
|
||||
self.assertEqual(person._data['created'], person.created)
|
||||
data = person.to_dict()
|
||||
|
||||
self.assertEqual(data['name'], person.name)
|
||||
self.assertEqual(data['age'], person.age)
|
||||
self.assertEqual(data['userid'], person.userid)
|
||||
self.assertEqual(data['created'], person.created)
|
||||
|
||||
# Confirm introspection changes nothing
|
||||
data_to_be_saved = sorted(person.to_mongo().keys())
|
||||
|
|
@ -157,10 +161,12 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(person.userid, person.userid)
|
||||
self.assertEqual(person.created, person.created)
|
||||
|
||||
self.assertEqual(person._data['name'], person.name)
|
||||
self.assertEqual(person._data['age'], person.age)
|
||||
self.assertEqual(person._data['userid'], person.userid)
|
||||
self.assertEqual(person._data['created'], person.created)
|
||||
data = person.to_dict()
|
||||
|
||||
self.assertEqual(data['name'], person.name)
|
||||
self.assertEqual(data['age'], person.age)
|
||||
self.assertEqual(data['userid'], person.userid)
|
||||
self.assertEqual(data['created'], person.created)
|
||||
|
||||
# Confirm introspection changes nothing
|
||||
data_to_be_saved = sorted(person.to_mongo().keys())
|
||||
|
|
@ -266,17 +272,6 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(1, TestDocument.objects(int_fld__ne=None).count())
|
||||
self.assertEqual(1, TestDocument.objects(float_fld__ne=None).count())
|
||||
|
||||
def test_long_ne_operator(self):
|
||||
class TestDocument(Document):
|
||||
long_fld = LongField()
|
||||
|
||||
TestDocument.drop_collection()
|
||||
|
||||
TestDocument(long_fld=None).save()
|
||||
TestDocument(long_fld=1).save()
|
||||
|
||||
self.assertEqual(1, TestDocument.objects(long_fld__ne=None).count())
|
||||
|
||||
def test_object_id_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to string fields.
|
||||
"""
|
||||
|
|
@ -350,23 +345,6 @@ class FieldTest(unittest.TestCase):
|
|||
person.age = 'ten'
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
def test_long_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to long fields.
|
||||
"""
|
||||
class TestDocument(Document):
|
||||
value = LongField(min_value=0, max_value=110)
|
||||
|
||||
doc = TestDocument()
|
||||
doc.value = 50
|
||||
doc.validate()
|
||||
|
||||
doc.value = -1
|
||||
self.assertRaises(ValidationError, doc.validate)
|
||||
doc.age = 120
|
||||
self.assertRaises(ValidationError, doc.validate)
|
||||
doc.age = 'ten'
|
||||
self.assertRaises(ValidationError, doc.validate)
|
||||
|
||||
def test_float_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to float fields.
|
||||
"""
|
||||
|
|
@ -384,69 +362,6 @@ class FieldTest(unittest.TestCase):
|
|||
person.height = 4.0
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
def test_decimal_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to decimal fields.
|
||||
"""
|
||||
class Person(Document):
|
||||
height = DecimalField(min_value=Decimal('0.1'),
|
||||
max_value=Decimal('3.5'))
|
||||
|
||||
Person.drop_collection()
|
||||
|
||||
Person(height=Decimal('1.89')).save()
|
||||
person = Person.objects.first()
|
||||
self.assertEqual(person.height, Decimal('1.89'))
|
||||
|
||||
person.height = '2.0'
|
||||
person.save()
|
||||
person.height = 0.01
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = Decimal('0.01')
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
person.height = Decimal('4.0')
|
||||
self.assertRaises(ValidationError, person.validate)
|
||||
|
||||
Person.drop_collection()
|
||||
|
||||
def test_decimal_comparison(self):
|
||||
|
||||
class Person(Document):
|
||||
money = DecimalField()
|
||||
|
||||
Person.drop_collection()
|
||||
|
||||
Person(money=6).save()
|
||||
Person(money=8).save()
|
||||
Person(money=10).save()
|
||||
|
||||
self.assertEqual(2, Person.objects(money__gt=Decimal("7")).count())
|
||||
self.assertEqual(2, Person.objects(money__gt=7).count())
|
||||
self.assertEqual(2, Person.objects(money__gt="7").count())
|
||||
|
||||
def test_decimal_storage(self):
|
||||
class Person(Document):
|
||||
btc = DecimalField(precision=4)
|
||||
|
||||
Person.drop_collection()
|
||||
Person(btc=10).save()
|
||||
Person(btc=10.1).save()
|
||||
Person(btc=10.11).save()
|
||||
Person(btc="10.111").save()
|
||||
Person(btc=Decimal("10.1111")).save()
|
||||
Person(btc=Decimal("10.11111")).save()
|
||||
|
||||
# How its stored
|
||||
expected = [{'btc': 10.0}, {'btc': 10.1}, {'btc': 10.11},
|
||||
{'btc': 10.111}, {'btc': 10.1111}, {'btc': 10.1111}]
|
||||
actual = list(Person.objects.exclude('id').as_pymongo())
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
# How it comes out locally
|
||||
expected = [Decimal('10.0000'), Decimal('10.1000'), Decimal('10.1100'),
|
||||
Decimal('10.1110'), Decimal('10.1111'), Decimal('10.1111')]
|
||||
actual = list(Person.objects().scalar('btc'))
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_boolean_validation(self):
|
||||
"""Ensure that invalid values cannot be assigned to boolean fields.
|
||||
"""
|
||||
|
|
@ -532,10 +447,10 @@ class FieldTest(unittest.TestCase):
|
|||
log.time = datetime.datetime.now().isoformat('T')
|
||||
log.validate()
|
||||
|
||||
log.time = -1
|
||||
self.assertRaises(ValidationError, log.validate)
|
||||
log.time = 'ABC'
|
||||
self.assertRaises(ValidationError, log.validate)
|
||||
#log.time = -1
|
||||
#self.assertRaises(ValidationError, log.validate)
|
||||
#log.time = 'ABC'
|
||||
#self.assertRaises(ValidationError, log.validate)
|
||||
|
||||
def test_datetime_tz_aware_mark_as_changed(self):
|
||||
from mongoengine import connection
|
||||
|
|
@ -556,7 +471,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
log = LogEntry.objects.first()
|
||||
log.time = datetime.datetime(2013, 1, 1, 0, 0, 0)
|
||||
self.assertEqual(['time'], log._changed_fields)
|
||||
self.assertEqual(set(['time']), log._changed_fields)
|
||||
|
||||
def test_datetime(self):
|
||||
"""Tests showing pymongo datetime fields handling of microseconds.
|
||||
|
|
@ -791,8 +706,8 @@ class FieldTest(unittest.TestCase):
|
|||
post = BlogPost(content='Went for a walk today...')
|
||||
post.validate()
|
||||
|
||||
post.tags = 'fun'
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
#post.tags = 'fun'
|
||||
#self.assertRaises(ValidationError, post.validate)
|
||||
post.tags = [1, 2]
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
|
|
@ -903,11 +818,11 @@ class FieldTest(unittest.TestCase):
|
|||
BlogPost.drop_collection()
|
||||
|
||||
post = BlogPost()
|
||||
post.info = 'my post'
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
#post.info = 'my post'
|
||||
#self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = {'title': 'test'}
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
#post.info = {'title': 'test'}
|
||||
#self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = ['test']
|
||||
post.save()
|
||||
|
|
@ -964,6 +879,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
Simple.drop_collection()
|
||||
|
||||
@unittest.skip("different behavior")
|
||||
def test_list_field_rejects_strings(self):
|
||||
"""Strings aren't valid list field data types"""
|
||||
|
||||
|
|
@ -1008,7 +924,7 @@ class FieldTest(unittest.TestCase):
|
|||
Simple.drop_collection()
|
||||
e = Simple().save()
|
||||
e.mapping = []
|
||||
self.assertEqual([], e._changed_fields)
|
||||
self.assertEqual(set([]), e._changed_fields)
|
||||
|
||||
class Simple(Document):
|
||||
mapping = DictField()
|
||||
|
|
@ -1016,8 +932,9 @@ class FieldTest(unittest.TestCase):
|
|||
Simple.drop_collection()
|
||||
e = Simple().save()
|
||||
e.mapping = {}
|
||||
self.assertEqual([], e._changed_fields)
|
||||
self.assertEqual(set([]), e._changed_fields)
|
||||
|
||||
@unittest.skip("complex types not implemented")
|
||||
def test_list_field_complex(self):
|
||||
"""Ensure that the list fields can handle the complex types."""
|
||||
|
||||
|
|
@ -1074,11 +991,11 @@ class FieldTest(unittest.TestCase):
|
|||
BlogPost.drop_collection()
|
||||
|
||||
post = BlogPost()
|
||||
post.info = 'my post'
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
#post.info = 'my post'
|
||||
#self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = ['test', 'test']
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
#post.info = ['test', 'test']
|
||||
#self.assertRaises(ValidationError, post.validate)
|
||||
|
||||
post.info = {'$title': 'test'}
|
||||
self.assertRaises(ValidationError, post.validate)
|
||||
|
|
@ -1136,6 +1053,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
Simple.drop_collection()
|
||||
|
||||
@unittest.skip("complex types not implemented")
|
||||
def test_dictfield_complex(self):
|
||||
"""Ensure that the dict field can handle the complex types."""
|
||||
|
||||
|
|
@ -1953,6 +1871,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
Shirt.drop_collection()
|
||||
|
||||
@unittest.skip("not implemented")
|
||||
def test_choices_get_field_display(self):
|
||||
"""Test dynamic helper for returning the display value of a choices
|
||||
field.
|
||||
|
|
@ -2005,6 +1924,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
Shirt.drop_collection()
|
||||
|
||||
@unittest.skip("not implemented")
|
||||
def test_simple_choices_get_field_display(self):
|
||||
"""Test dynamic helper for returning the display value of a choices
|
||||
field.
|
||||
|
|
@ -2084,6 +2004,7 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(d2.data, {})
|
||||
self.assertEqual(d2.data2, {})
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_sequence_field(self):
|
||||
class Person(Document):
|
||||
id = SequenceField(primary_key=True)
|
||||
|
|
@ -2109,6 +2030,7 @@ class FieldTest(unittest.TestCase):
|
|||
self.assertEqual(c['next'], 1000)
|
||||
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_sequence_field_get_next_value(self):
|
||||
class Person(Document):
|
||||
id = SequenceField(primary_key=True)
|
||||
|
|
@ -2140,6 +2062,7 @@ class FieldTest(unittest.TestCase):
|
|||
|
||||
self.assertEqual(Person.id.get_next_value(), '1')
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_sequence_field_sequence_name(self):
|
||||
class Person(Document):
|
||||
id = SequenceField(primary_key=True, sequence_name='jelly')
|
||||
|
|
@ -2164,6 +2087,7 @@ class FieldTest(unittest.TestCase):
|
|||
c = self.db['mongoengine.counters'].find_one({'_id': 'jelly.id'})
|
||||
self.assertEqual(c['next'], 1000)
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_multiple_sequence_fields(self):
|
||||
class Person(Document):
|
||||
id = SequenceField(primary_key=True)
|
||||
|
|
@ -2196,6 +2120,7 @@ class FieldTest(unittest.TestCase):
|
|||
c = self.db['mongoengine.counters'].find_one({'_id': 'person.counter'})
|
||||
self.assertEqual(c['next'], 999)
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_sequence_fields_reload(self):
|
||||
class Animal(Document):
|
||||
counter = SequenceField()
|
||||
|
|
@ -2221,6 +2146,7 @@ class FieldTest(unittest.TestCase):
|
|||
a.reload()
|
||||
self.assertEqual(a.counter, 2)
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_multiple_sequence_fields_on_docs(self):
|
||||
|
||||
class Animal(Document):
|
||||
|
|
@ -2255,6 +2181,7 @@ class FieldTest(unittest.TestCase):
|
|||
c = self.db['mongoengine.counters'].find_one({'_id': 'animal.id'})
|
||||
self.assertEqual(c['next'], 10)
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_sequence_field_value_decorator(self):
|
||||
class Person(Document):
|
||||
id = SequenceField(primary_key=True, value_decorator=str)
|
||||
|
|
@ -2276,6 +2203,7 @@ class FieldTest(unittest.TestCase):
|
|||
c = self.db['mongoengine.counters'].find_one({'_id': 'person.id'})
|
||||
self.assertEqual(c['next'], 10)
|
||||
|
||||
@unittest.skip("SequenceField not implemented")
|
||||
def test_embedded_sequence_field(self):
|
||||
class Comment(EmbeddedDocument):
|
||||
id = SequenceField()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue