From 95c2643f63558ccc2a707fa425f670b597f4e2a2 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Wed, 18 May 2011 20:31:28 +0100 Subject: [PATCH] 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 --- tests/document.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/document.py b/tests/document.py index 66efdf98..dee0b712 100644 --- a/tests/document.py +++ b/tests/document.py @@ -380,6 +380,28 @@ class DocumentTest(unittest.TestCase): pass 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): """Ensure that documents may be created with custom primary keys. """