Ensure that unique_with constraints are applied to fields.
(self)
| 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.""" |
nothing calls this directly
no test coverage detected