Added test showing primary=True behaviour.
If you set a field as primary, then unexpected behaviour can occur. You won't create a duplicate but you will update an existing document. Closes #138
This commit is contained in:
parent
fc2aff342b
commit
95c2643f63
@ -380,6 +380,28 @@ class DocumentTest(unittest.TestCase):
|
|||||||
pass
|
pass
|
||||||
Customer.drop_collection()
|
Customer.drop_collection()
|
||||||
|
|
||||||
|
def test_unique_and_primary(self):
|
||||||
|
"""If you set a field as primary, then unexpected behaviour can occur.
|
||||||
|
You won't create a duplicate but you will update an existing document.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class User(Document):
|
||||||
|
name = StringField(primary_key=True, unique=True)
|
||||||
|
password = StringField()
|
||||||
|
|
||||||
|
User.drop_collection()
|
||||||
|
|
||||||
|
user = User(name='huangz', password='secret')
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
user = User(name='huangz', password='secret2')
|
||||||
|
user.save()
|
||||||
|
|
||||||
|
self.assertEqual(User.objects.count(), 1)
|
||||||
|
self.assertEqual(User.objects.get().password, 'secret2')
|
||||||
|
|
||||||
|
User.drop_collection()
|
||||||
|
|
||||||
def test_custom_id_field(self):
|
def test_custom_id_field(self):
|
||||||
"""Ensure that documents may be created with custom primary keys.
|
"""Ensure that documents may be created with custom primary keys.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user