| 527 | BlogPost.objects.hint(("tags", 1)).count() |
| 528 | |
| 529 | def test_collation(self): |
| 530 | base = {"locale": "en", "strength": 2} |
| 531 | |
| 532 | class BlogPost(Document): |
| 533 | name = StringField() |
| 534 | meta = { |
| 535 | "indexes": [ |
| 536 | {"fields": ["name"], "name": "name_index", "collation": base} |
| 537 | ] |
| 538 | } |
| 539 | |
| 540 | BlogPost.drop_collection() |
| 541 | |
| 542 | names = ["tag1", "Tag2", "tag3", "Tag4", "tag5"] |
| 543 | for name in names: |
| 544 | BlogPost(name=name).save() |
| 545 | |
| 546 | query_result = BlogPost.objects.collation(base).order_by("name") |
| 547 | assert [x.name for x in query_result] == sorted(names, key=lambda x: x.lower()) |
| 548 | assert 5 == query_result.count() |
| 549 | |
| 550 | query_result = BlogPost.objects.collation(Collation(**base)).order_by("name") |
| 551 | assert [x.name for x in query_result] == sorted(names, key=lambda x: x.lower()) |
| 552 | assert 5 == query_result.count() |
| 553 | |
| 554 | incorrect_collation = {"arndom": "wrdo"} |
| 555 | with pytest.raises(OperationFailure) as exc_info: |
| 556 | BlogPost.objects.collation(incorrect_collation).count() |
| 557 | assert "Missing expected field" in str( |
| 558 | exc_info.value |
| 559 | ) or "unknown field" in str(exc_info.value) |
| 560 | |
| 561 | query_result = BlogPost.objects.collation({}).order_by("name") |
| 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.""" |