Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tests/test_bugfix.py
htmlcov/
venv
venv3
scratchpad
2 changes: 1 addition & 1 deletion mongoengine/queryset/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def create(self, **kwargs):

.. versionadded:: 0.4
"""
return self._document(**kwargs).save()
return self._document(**kwargs).save(force_insert=True)

def first(self):
"""Retrieve the first object matching the query."""
Expand Down
28 changes: 15 additions & 13 deletions tests/document/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@ class MongoUser(User):
User.ensure_indexes()
info = User.objects._collection.index_information()
self.assertEqual(sorted(info.keys()), ['_cls_1_user_guid_1', '_id_'])
User.drop_collection()

def test_embedded_document_index(self):
"""Tests settings an index on an embedded document
Expand All @@ -434,7 +433,6 @@ class BlogPost(Document):

info = BlogPost.objects._collection.index_information()
self.assertEqual(sorted(info.keys()), ['_id_', 'date.yr_-1'])
BlogPost.drop_collection()

def test_list_embedded_document_index(self):
"""Ensure list embedded documents can be indexed
Expand All @@ -461,7 +459,6 @@ class BlogPost(Document):
post1 = BlogPost(title="Embedded Indexes tests in place",
tags=[Tag(name="about"), Tag(name="time")])
post1.save()
BlogPost.drop_collection()

def test_recursive_embedded_objects_dont_break_indexes(self):

Expand Down Expand Up @@ -623,8 +620,6 @@ class BlogPost(Document):
post3 = BlogPost(title='test3', date=Date(year=2010), slug='test')
self.assertRaises(OperationError, post3.save)

BlogPost.drop_collection()

def test_unique_embedded_document(self):
"""Ensure that uniqueness constraints are applied to fields on embedded documents.
"""
Expand Down Expand Up @@ -652,8 +647,6 @@ class BlogPost(Document):
sub=SubDocument(year=2010, slug='test'))
self.assertRaises(NotUniqueError, post3.save)

BlogPost.drop_collection()

def test_unique_embedded_document_in_list(self):
"""
Ensure that the uniqueness constraints are applied to fields in
Expand Down Expand Up @@ -684,8 +677,6 @@ class BlogPost(Document):

self.assertRaises(NotUniqueError, post2.save)

BlogPost.drop_collection()

def test_unique_with_embedded_document_and_embedded_unique(self):
"""Ensure that uniqueness constraints are applied to fields on
embedded documents. And work with unique_with as well.
Expand Down Expand Up @@ -719,8 +710,6 @@ class BlogPost(Document):
sub=SubDocument(year=2009, slug='test-1'))
self.assertRaises(NotUniqueError, post3.save)

BlogPost.drop_collection()

def test_ttl_indexes(self):

class Log(Document):
Expand Down Expand Up @@ -768,13 +757,11 @@ class Customer(Document):
raise AssertionError("We saved a dupe!")
except NotUniqueError:
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()
Expand All @@ -790,8 +777,23 @@ class User(Document):
self.assertEqual(User.objects.count(), 1)
self.assertEqual(User.objects.get().password, 'secret2')

def test_unique_and_primary_create(self):
"""Create a new record with a duplicate primary key
throws an exception
"""
class User(Document):
name = StringField(primary_key=True)
password = StringField()

User.drop_collection()

User.objects.create(name='huangz', password='secret')
with self.assertRaises(NotUniqueError):
User.objects.create(name='huangz', password='secret2')

self.assertEqual(User.objects.count(), 1)
self.assertEqual(User.objects.get().password, 'secret')

def test_index_with_pk(self):
"""Ensure you can use `pk` as part of a query"""

Expand Down