MCPcopy Create free account
hub / github.com/MongoEngine/mongoengine / test_fields

Method test_fields

tests/queryset/test_queryset.py:4835–4901  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4833 assert expected == "%s" % sorted(names)
4834
4835 def test_fields(self):
4836 class Bar(EmbeddedDocument):
4837 v = StringField()
4838 z = StringField()
4839
4840 class Foo(Document):
4841 x = StringField()
4842 y = IntField()
4843 items = EmbeddedDocumentListField(Bar)
4844
4845 Foo.drop_collection()
4846
4847 Foo(x="foo1", y=1).save()
4848 Foo(x="foo2", y=2, items=[]).save()
4849 Foo(x="foo3", y=3, items=[Bar(z="a", v="V")]).save()
4850 Foo(
4851 x="foo4",
4852 y=4,
4853 items=[
4854 Bar(z="a", v="V"),
4855 Bar(z="b", v="W"),
4856 Bar(z="b", v="X"),
4857 Bar(z="c", v="V"),
4858 ],
4859 ).save()
4860 Foo(
4861 x="foo5",
4862 y=5,
4863 items=[
4864 Bar(z="b", v="X"),
4865 Bar(z="c", v="V"),
4866 Bar(z="d", v="V"),
4867 Bar(z="e", v="V"),
4868 ],
4869 ).save()
4870
4871 foos_with_x = list(Foo.objects.order_by("y").fields(x=1))
4872
4873 assert all(o.x is not None for o in foos_with_x)
4874
4875 foos_without_y = list(Foo.objects.order_by("y").fields(y=0))
4876
4877 assert all(o.y is None for o in foos_without_y)
4878
4879 foos_with_sliced_items = list(Foo.objects.order_by("y").fields(slice__items=1))
4880
4881 assert foos_with_sliced_items[0].items == []
4882 assert foos_with_sliced_items[1].items == []
4883 assert len(foos_with_sliced_items[2].items) == 1
4884 assert foos_with_sliced_items[2].items[0].z == "a"
4885 assert len(foos_with_sliced_items[3].items) == 1
4886 assert foos_with_sliced_items[3].items[0].z == "a"
4887 assert len(foos_with_sliced_items[4].items) == 1
4888 assert foos_with_sliced_items[4].items[0].z == "b"
4889
4890 foos_with_elem_match_items = list(
4891 Foo.objects.order_by("y").fields(elemMatch__items={"z": "b"})
4892 )

Callers

nothing calls this directly

Calls 6

drop_collectionMethod · 0.80
fieldsMethod · 0.80
order_byMethod · 0.80
FooClass · 0.70
BarClass · 0.70
saveMethod · 0.45

Tested by

no test coverage detected