MCPcopy Create free account
hub / github.com/MongoEngine/mongoengine / test_unique_with

Method test_unique_with

tests/document/test_indexes.py:612–635  ·  view source on GitHub ↗

Ensure that unique_with constraints are applied to fields.

(self)

Source from the content-addressed store, hash-verified

610 )
611
612 def test_unique_with(self):
613 """Ensure that unique_with constraints are applied to fields."""
614
615 class Date(EmbeddedDocument):
616 year = IntField(db_field="yr")
617
618 class BlogPost(Document):
619 title = StringField()
620 date = EmbeddedDocumentField(Date)
621 slug = StringField(unique_with="date.year")
622
623 BlogPost.drop_collection()
624
625 post1 = BlogPost(title="test1", date=Date(year=2009), slug="test")
626 post1.save()
627
628 # day is different so won't raise exception
629 post2 = BlogPost(title="test2", date=Date(year=2010), slug="test")
630 post2.save()
631
632 # Now there will be two docs with the same slug and the same day: fail
633 post3 = BlogPost(title="test3", date=Date(year=2010), slug="test")
634 with pytest.raises(OperationError):
635 post3.save()
636
637 def test_unique_embedded_document(self):
638 """Ensure that uniqueness constraints are applied to fields on embedded documents."""

Callers

nothing calls this directly

Calls 4

DateClass · 0.85
drop_collectionMethod · 0.80
BlogPostClass · 0.70
saveMethod · 0.45

Tested by

no test coverage detected