MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / test_unique

Method test_unique

tests/document/test_indexes.py:564–585  ·  view source on GitHub ↗

Ensure that uniqueness constraints are applied to fields.

(self)

Source from the content-addressed store, hash-verified

562 assert [x.name for x in query_result] == sorted(names)
563
564 def test_unique(self):
565 """Ensure that uniqueness constraints are applied to fields."""
566
567 class BlogPost(Document):
568 title = StringField()
569 slug = StringField(unique=True)
570
571 BlogPost.drop_collection()
572
573 post1 = BlogPost(title="test1", slug="test")
574 post1.save()
575
576 # Two posts with the same slug is not allowed
577 post2 = BlogPost(title="test2", slug="test")
578 with pytest.raises(NotUniqueError):
579 post2.save()
580 with pytest.raises(NotUniqueError):
581 BlogPost.objects.insert(post2)
582
583 # Ensure backwards compatibility for errors
584 with pytest.raises(OperationError):
585 post2.save()
586
587 def test_primary_key_unique_not_working(self):
588 """Relates to #1445"""

Callers

nothing calls this directly

Calls 4

drop_collectionMethod · 0.80
insertMethod · 0.80
BlogPostClass · 0.70
saveMethod · 0.45

Tested by

no test coverage detected