예제 #1
0
    def test_switch_collection_context_manager(self):
        clear_document_registry()
        connect("mongoenginetest")
        register_connection(alias="testdb-1", db="mongoenginetest2")

        class Group(Document):
            name = StringField()

        Group.drop_collection()  # drops in default

        with switch_collection(Group, "group1") as Group:
            Group.drop_collection()  # drops in group1

        Group(name="hello - group").save()
        assert 1 == Group.objects.count()

        with switch_collection(Group, "group1") as Group:

            assert 0 == Group.objects.count()

            Group(name="hello - group1").save()

            assert 1 == Group.objects.count()

            Group.drop_collection()
            assert 0 == Group.objects.count()

        assert 1 == Group.objects.count()
예제 #2
0
    def test_dynamic_and_embedded(self):
        """Ensure embedded documents play nicely"""
        clear_document_registry()

        class Address(EmbeddedDocument):
            city = StringField()

        class Person(DynamicDocument):
            name = StringField()

        Person.drop_collection()

        Person(name="Ross", address=Address(city="London")).save()

        person = Person.objects.first()
        person.address.city = "Lundenne"
        person.save()

        assert Person.objects.first().address.city == "Lundenne"

        person = Person.objects.first()
        person.address = Address(city="Londinium")
        person.save()

        assert Person.objects.first().address.city == "Londinium"

        person = Person.objects.first()
        person.age = 35
        person.save()
        assert Person.objects.first().age == 35
예제 #3
0
    def circular_reference_deltas(self, DocClass1, DocClass2):
        clear_document_registry()

        class Person(DocClass1):
            name = StringField()
            owns = ListField(ReferenceField("Organization"))

        class Organization(DocClass2):
            name = StringField()
            owner = ReferenceField("Person")

        Person.drop_collection()
        Organization.drop_collection()

        person = Person(name="owner").save()
        organization = Organization(name="company").save()

        person.owns.append(organization)
        organization.owner = person

        person.save()
        organization.save()

        p = Person.objects[0].select_related()
        o = Organization.objects.first()
        assert p.owns[0] == o
        assert o.owner == p
예제 #4
0
    def test_switch_db_context_manager(self):
        clear_document_registry()
        connect("mongoenginetest")
        register_connection("testdb-1", "mongoenginetest2")

        class Group(Document):
            name = StringField()

        Group.drop_collection()

        Group(name="hello - default").save()
        assert 1 == Group.objects.count()

        with switch_db(Group, "testdb-1") as Group:

            assert 0 == Group.objects.count()

            Group(name="hello").save()

            assert 1 == Group.objects.count()

            Group.drop_collection()
            assert 0 == Group.objects.count()

        assert 1 == Group.objects.count()
예제 #5
0
    def test_set_read_write_concern(self):
        clear_document_registry()
        connect("mongoenginetest")

        class User(Document):
            name = StringField()

        collection = User._get_collection()

        original_read_concern = collection.read_concern
        original_write_concern = collection.write_concern

        with set_read_write_concern(
                collection,
            {
                "w": "majority",
                "j": True,
                "wtimeout": 1234
            },
            {"level": "local"},
        ) as update_collection:
            assert update_collection.read_concern.document == {
                "level": "local"
            }
            assert update_collection.write_concern.document == {
                "w": "majority",
                "j": True,
                "wtimeout": 1234,
            }

        assert original_read_concern.document == collection.read_concern.document
        assert original_write_concern.document == collection.write_concern.document
예제 #6
0
    def test_no_subclass_context_manager_does_not_swallow_exception(self):
        clear_document_registry()

        class User(Document):
            name = StringField()

        with pytest.raises(TypeError):
            with no_sub_classes(User):
                raise TypeError()
예제 #7
0
    def test_no_sub_classes(self):
        clear_document_registry()

        class A(Document):
            x = IntField()
            meta = {"allow_inheritance": True}

        class B(A):
            z = IntField()

        class C(B):
            zz = IntField()

        A.drop_collection()

        A(x=10).save()
        A(x=15).save()
        B(x=20).save()
        B(x=30).save()
        C(x=40).save()

        assert A.objects.count() == 5
        assert B.objects.count() == 3
        assert C.objects.count() == 1

        with no_sub_classes(A):
            assert A.objects.count() == 2

            for obj in A.objects:
                assert obj.__class__ == A

        with no_sub_classes(B):
            assert B.objects.count() == 2

            for obj in B.objects:
                assert obj.__class__ == B

        with no_sub_classes(C):
            assert C.objects.count() == 1

            for obj in C.objects:
                assert obj.__class__ == C

        # Confirm context manager exit correctly
        assert A.objects.count() == 5
        assert B.objects.count() == 3
        assert C.objects.count() == 1
예제 #8
0
    def test_query_counter_alias(self):
        """query_counter works properly with db aliases?"""
        clear_document_registry()
        # Register a connection with db_alias testdb-1
        register_connection("testdb-1", "mongoenginetest2")

        class A(Document):
            """Uses default db_alias"""

            name = StringField()

        class B(Document):
            """Uses testdb-1 db_alias"""

            name = StringField()
            meta = {"db_alias": "testdb-1"}

        A.drop_collection()
        B.drop_collection()

        with query_counter() as q:
            assert q == 0
            A.objects.create(name="A")
            assert q == 1
            a = A.objects.first()
            assert q == 2
            a.name = "Test A"
            a.save()
            assert q == 3
            # querying the other db should'nt alter the counter
            B.objects().first()
            assert q == 3

        with query_counter(alias="testdb-1") as q:
            assert q == 0
            B.objects.create(name="B")
            assert q == 1
            b = B.objects.first()
            assert q == 2
            b.name = "Test B"
            b.save()
            assert b.name == "Test B"
            assert q == 3
            # querying the other db should'nt alter the counter
            A.objects().first()
            assert q == 3
예제 #9
0
    def test_no_sub_classes_modification_to_document_class_are_temporary(self):
        clear_document_registry()

        class A(Document):
            x = IntField()
            meta = {"allow_inheritance": True}

        class B(A):
            z = IntField()

        assert A._subclasses == ("A", "A.B")
        with no_sub_classes(A):
            assert A._subclasses == ("A", )
        assert A._subclasses == ("A", "A.B")

        assert B._subclasses == ("A.B", )
        with no_sub_classes(B):
            assert B._subclasses == ("A.B", )
        assert B._subclasses == ("A.B", )
예제 #10
0
    def test_dynamic_embedded_works_with_only(self):
        """Ensure custom fieldnames on a dynamic embedded document are found by qs.only()"""
        clear_document_registry()

        class Address(DynamicEmbeddedDocument):
            city = StringField()

        class Person(DynamicDocument):
            address = EmbeddedDocumentField(Address)

        Person.drop_collection()

        Person(
            name="Eric", address=Address(city="San Francisco", street_number="1337")
        ).save()

        assert Person.objects.first().address.street_number == "1337"
        assert (
            Person.objects.only("address__street_number").first().address.street_number
            == "1337"
        )
예제 #11
0
    def test_delta_for_dynamic_documents(self):
        clear_document_registry()

        class Person(DynamicDocument):
            name = StringField()
            meta = {"allow_inheritance": True}

        Person.drop_collection()

        p = Person(name="James", age=34)
        assert p._delta() == (
            SON([("_cls", "Person"), ("name", "James"), ("age", 34)]),
            {},
        )

        p.doc = 123
        del p.doc
        assert p._delta() == (
            SON([("_cls", "Person"), ("name", "James"), ("age", 34)]),
            {},
        )

        p = Person()
        p.name = "Dean"
        p.age = 22
        p.save()

        p.age = 24
        assert p.age == 24
        assert p._get_changed_fields() == ["age"]
        assert p._delta() == ({"age": 24}, {})

        p = Person.objects(age=22).get()
        p.age = 24
        assert p.age == 24
        assert p._get_changed_fields() == ["age"]
        assert p._delta() == ({"age": 24}, {})

        p.save()
        assert 1 == Person.objects(age=24).count()
예제 #12
0
    def circular_reference_deltas_2(self, DocClass1, DocClass2, dbref=True):
        clear_document_registry()

        class Person(DocClass1):
            name = StringField()
            owns = ListField(ReferenceField("Organization", dbref=dbref))
            employer = ReferenceField("Organization", dbref=dbref)

        class Organization(DocClass2):
            name = StringField()
            owner = ReferenceField("Person", dbref=dbref)
            employees = ListField(ReferenceField("Person", dbref=dbref))

        Person.drop_collection()
        Organization.drop_collection()

        person = Person(name="owner").save()
        employee = Person(name="employee").save()
        organization = Organization(name="company").save()

        person.owns.append(organization)
        organization.owner = person

        organization.employees.append(employee)
        employee.employer = organization

        person.save()
        organization.save()
        employee.save()

        p = Person.objects.get(name="owner")
        e = Person.objects.get(name="employee")
        o = Organization.objects.first()

        assert p.owns[0] == o
        assert o.owner == p
        assert e.employer == o

        return person, organization, employee
예제 #13
0
    def test_complex_dynamic_document_queries(self):
        clear_document_registry()

        class Person(DynamicDocument):
            name = StringField()

        Person.drop_collection()

        p = Person(name="test")
        p.age = "ten"
        p.save()

        p1 = Person(name="test1")
        p1.age = "less then ten and a half"
        p1.save()

        p2 = Person(name="test2")
        p2.age = 10
        p2.save()

        assert Person.objects(age__icontains="ten").count() == 2
        assert Person.objects(age__gte=10).count() == 1
예제 #14
0
    def test_no_dereference_context_manager_object_id(self):
        """Ensure that DBRef items in ListFields aren't dereferenced.
        """
        clear_document_registry()
        connect("mongoenginetest")

        class User(Document):
            name = StringField()

        class Group(Document):
            ref = ReferenceField(User, dbref=False)
            generic = GenericReferenceField()
            members = ListField(ReferenceField(User, dbref=False))

        User.drop_collection()
        Group.drop_collection()

        for i in range(1, 51):
            User(name="user %s" % i).save()

        user = User.objects.first()
        Group(ref=user, members=User.objects, generic=user).save()

        with no_dereference(Group) as NoDeRefGroup:
            assert Group._fields["members"]._auto_dereference
            assert not NoDeRefGroup._fields["members"]._auto_dereference

        with no_dereference(Group) as Group:
            group = Group.objects.first()
            for m in group.members:
                assert not isinstance(m, User)
            assert not isinstance(group.ref, User)
            assert not isinstance(group.generic, User)

        for m in group.members:
            assert isinstance(m, User)
        assert isinstance(group.ref, User)
        assert isinstance(group.generic, User)
예제 #15
0
    def test_dynamic_and_embedded_dict_access(self):
        """Ensure embedded dynamic documents work with dict[] style access"""
        clear_document_registry()

        class Address(EmbeddedDocument):
            city = StringField()

        class Person(DynamicDocument):
            name = StringField()

        Person.drop_collection()

        Person(name="Ross", address=Address(city="London")).save()

        person = Person.objects.first()
        person.attrval = "This works"

        person["phone"] = "555-1212"  # but this should too

        # Same thing two levels deep
        person["address"]["city"] = "Lundenne"
        person.save()

        assert Person.objects.first().address.city == "Lundenne"

        assert Person.objects.first().phone == "555-1212"

        person = Person.objects.first()
        person.address = Address(city="Londinium")
        person.save()

        assert Person.objects.first().address.city == "Londinium"

        person = Person.objects.first()
        person["age"] = 35
        person.save()
        assert Person.objects.first().age == 35
예제 #16
0
 def tearDown(self):
     clear_document_registry()
예제 #17
0
 def setUp(self):
     clear_document_registry()