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