Ensure that list types work as expected.
(self)
| 627 | assert catlist.categories[2].name == cat1.name |
| 628 | |
| 629 | def test_list_field(self): |
| 630 | """Ensure that list types work as expected.""" |
| 631 | |
| 632 | class BlogPost(Document): |
| 633 | info = ListField() |
| 634 | |
| 635 | BlogPost.drop_collection() |
| 636 | |
| 637 | post = BlogPost() |
| 638 | post.info = "my post" |
| 639 | with pytest.raises(ValidationError): |
| 640 | post.validate() |
| 641 | |
| 642 | post.info = {"title": "test"} |
| 643 | with pytest.raises(ValidationError): |
| 644 | post.validate() |
| 645 | |
| 646 | post.info = ["test"] |
| 647 | post.save() |
| 648 | |
| 649 | post = BlogPost() |
| 650 | post.info = [{"test": "test"}] |
| 651 | post.save() |
| 652 | |
| 653 | post = BlogPost() |
| 654 | post.info = [{"test": 3}] |
| 655 | post.save() |
| 656 | |
| 657 | assert BlogPost.objects.count() == 3 |
| 658 | assert BlogPost.objects.filter(info__exact="test").count() == 1 |
| 659 | assert BlogPost.objects.filter(info__0__test="test").count() == 1 |
| 660 | |
| 661 | # Confirm handles non strings or non existing keys |
| 662 | assert BlogPost.objects.filter(info__0__test__exact="5").count() == 0 |
| 663 | assert BlogPost.objects.filter(info__100__test__exact="test").count() == 0 |
| 664 | |
| 665 | # test queries by list |
| 666 | post = BlogPost() |
| 667 | post.info = ["1", "2"] |
| 668 | post.save() |
| 669 | post = BlogPost.objects(info=["1", "2"]).get() |
| 670 | post.info += ["3", "4"] |
| 671 | post.save() |
| 672 | assert BlogPost.objects(info=["1", "2", "3", "4"]).count() == 1 |
| 673 | post = BlogPost.objects(info=["1", "2", "3", "4"]).get() |
| 674 | post.info *= 2 |
| 675 | post.save() |
| 676 | assert ( |
| 677 | BlogPost.objects(info=["1", "2", "3", "4", "1", "2", "3", "4"]).count() == 1 |
| 678 | ) |
| 679 | |
| 680 | def test_list_field_manipulative_operators(self): |
| 681 | """Ensure that ListField works with standard list operators that manipulate the list.""" |