MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / test_list_field

Method test_list_field

tests/fields/test_fields.py:629–678  ·  view source on GitHub ↗

Ensure that list types work as expected.

(self)

Source from the content-addressed store, hash-verified

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."""

Callers

nothing calls this directly

Calls 8

drop_collectionMethod · 0.80
BlogPostClass · 0.70
validateMethod · 0.45
saveMethod · 0.45
countMethod · 0.45
filterMethod · 0.45
getMethod · 0.45
objectsMethod · 0.45

Tested by

no test coverage detected