Пример #1
0
    def filter(self, *arguments, **kwargs):
        """
        Filters a queryset in order to produce a different set of document from subsequent queries.

        Usage::

            await User.objects.filter(first_name="Bernardo").filter(last_name="Bernardo").find_all()
            # or
            await User.objects.filter(first_name="Bernardo", starting_year__gt=2010).find_all()

        The available filter options are the same as used in MongoEngine.
        """
        from jetengine.query_builder.node import Q, QCombination, QNot
        from jetengine.query_builder.transform import validate_fields

        if arguments and len(arguments) == 1 and isinstance(
                arguments[0], (Q, QNot, QCombination)):
            if self._filters:
                self._filters = self._filters & arguments[0]
            else:
                self._filters = arguments[0]
        else:
            validate_fields(self.__klass__, kwargs)
            if self._filters:
                self._filters = self._filters & Q(**kwargs)
            else:
                if arguments and len(arguments) == 1 and isinstance(
                        arguments[0], dict):
                    self._filters = Q(arguments[0])
                else:
                    self._filters = Q(**kwargs)

        return self
Пример #2
0
    def test_gets_proper_query_when_many_operators_with_and_first(self):
        query = Q(last_name="Whatever") & (
            (Q(embedded__test__lte="Test") & Q(first_name="Someone"))
            | Q(first_name="Else"))
        query_result = query.to_query(User)

        expect(query_result).to_be_like({
            "$and": [
                {
                    "last_name": "Whatever"
                },
                {
                    "$or": [
                        {
                            "embedded_document.other": {
                                "$lte": "Test"
                            },
                            "whatever": "Someone"
                        },
                        {
                            "whatever": "Else"
                        },
                    ]
                },
            ]
        })
Пример #3
0
    def test_gets_proper_query_when_and(self):
        query = Q(embedded__test__lte="Test") & Q(first_name="Someone")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({
            "embedded_document.other": {
                "$lte": "Test"
            },
            "whatever": "Someone"
        })
Пример #4
0
    def test_gets_proper_query_when_or(self):
        query = Q(first_name="Someone") | Q(first_name="Else")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"$or": [{
                "whatever": "Someone"
            }, {
                "whatever": "Else"
            }]})
Пример #5
0
    def test_can_query_using_not_in_with_real_users(self):
        yield from self.create_test_users()

        names = ["Someone", "John"]

        query = ~Q(first_name__in=names) & Q(last_name="Heynemann")

        users = yield from User.objects.filter(query).find_all()

        expect(users).to_length(1)
        expect(users[0].first_name).to_equal("Bernardo")
Пример #6
0
    def test_can_query_using_q_with_is_null(self):
        yield from self.create_test_users()

        users = yield from User.objects.filter(
            Q(nullable__is_null=True)).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(self.user._id)

        users = yield from User.objects.filter(
            Q(nullable__is_null=False)).find_all()
        expect(users).to_length(2)
        expect(users[0]._id).to_equal(self.user2._id)
        expect(users[1]._id).to_equal(self.user3._id)
Пример #7
0
    def test_can_query_using_q(self):
        yield from self.create_test_users()

        users = yield from User.objects.find_all()
        expect(users).to_length(3)

        users = yield from User.objects.filter(
            Q(first_name="Bernardo")).find_all()
        expect(users).to_length(1)

        users = yield from User.objects.filter(
            Q(first_name="Bernardo") | Q(first_name="Someone")).find_all()
        expect(users).to_length(2)
Пример #8
0
    def test_gets_proper_query_when_embedded_document(self):
        query = Q(embedded__test__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"embedded_document.other": {
                "$lte": "Test"
            }})
Пример #9
0
    def test_gets_proper_query_when_embedded_document_in_many_levels(self):
        query = Q(embedded__embedded2__test__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"embedded_document.embedded2.else": {
                "$lte": "Test"
            }})
Пример #10
0
    def test_can_find_with_multiple_filters(self):
        yield from User.objects.create(email="*****@*****.**",
                                       first_name="Bernardo",
                                       last_name="Heynemann")

        yield from User.objects.create(email="*****@*****.**",
                                       first_name="Someone",
                                       last_name="Else")

        user = yield from User.objects.create(email="*****@*****.**",
                                              first_name="Bernardo",
                                              last_name="Heynemann")

        last_user = yield from User.objects.create(email="*****@*****.**",
                                                   first_name="Bernardo",
                                                   last_name="Silva")

        # filter and filter not
        users = yield from User.objects.filter(
            email="*****@*****.**").filter_not(
                first_name="Someone").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(user._id)

        # filter and filter not for Q
        from jetengine import Q

        users = yield from User.objects.filter(
            email="*****@*****.**").filter_not(
                Q(first_name="Someone")).find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(user._id)

        # filter not and filter not
        users = yield from User.objects.filter_not(
            last_name="Heynemann").filter_not(first_name="Someone").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)

        first_user = users[0]
        expect(first_user._id).to_equal(last_user._id)

        # filter and filter
        users = yield from User.objects.filter(last_name="Silva").filter(
            first_name="Bernardo").find_all()

        expect(users).to_be_instance_of(list)
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(last_user._id)
Пример #11
0
    def test_can_query_using_q_for_list(self):
        yield from User.objects.create(
            email="*****@*****.**",
            first_name="Bernardo",
            last_name="Heynemann",
            embedded=EmbeddedDocument(test="test"),
            numbers=[1, 2, 3],
        )

        user2 = yield from User.objects.create(
            email="*****@*****.**",
            first_name="Someone",
            last_name="Else",
            embedded=EmbeddedDocument(test="test2"),
            numbers=[4, 5, 6],
        )

        yield from User.objects.create(
            email="*****@*****.**",
            first_name="John",
            last_name="Doe",
            embedded=EmbeddedDocument(test="test3"),
            numbers=[7, 8, 9],
        )

        users = yield from User.objects.filter(Q(numbers=[4])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield from User.objects.filter(Q(numbers=[5])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield from User.objects.filter(Q(numbers=[6])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield from User.objects.filter(Q(numbers=[4, 5, 6])).find_all()
        expect(users).to_length(1)
        expect(users[0]._id).to_equal(user2._id)

        users = yield from User.objects.filter(Q(numbers=[20])).find_all()
        expect(users).to_length(0)
Пример #12
0
    def to_query(self):
        from jetengine import Q

        match_obj = {"$match": {}}

        query = self.aggregation.queryset.get_query_from_filters(
            Q(**self.filters))

        update(match_obj["$match"], query)

        return match_obj
Пример #13
0
    def test_can_query_using_not_in(self):
        names = ["Someone", "John"]

        query = ~Q(first_name__in=names)
        query_result = query.to_query(User)

        expect(query_result).to_be_like(
            {"whatever": {
                "$not": {
                    "$in": ["Someone", "John"]
                }
            }})
Пример #14
0
    def filter_not(self, *arguments, **kwargs):
        """
        Filters a queryset to negate all the filters passed in subsequent queries.

        Usage::

            await User.objects.filter_not(first_name="Bernardo").filter_not(last_name="Bernardo").find_all()
            # or
            await User.objects.filter_not(first_name="Bernardo", starting_year__gt=2010).find_all()

        The available filter options are the same as used in MongoEngine.
        """
        from jetengine.query_builder.node import Q, QCombination, QNot

        if arguments and len(arguments) == 1 and isinstance(
                arguments[0], (Q, QCombination)):
            self.filter(QNot(arguments[0]))
        else:
            self.filter(QNot(Q(**kwargs)))

        return self
Пример #15
0
    async def get(self, id=None, alias=None, **kwargs):
        """
        Gets a single item of the current queryset collection using it's id.

        In order to query a different database, please specify the `alias` of the database to query.
        """

        from jetengine import Q

        if id is None and not kwargs:
            raise RuntimeError(
                "Either an id or a filter must be provided to get")

        if id is not None:
            if not isinstance(id, ObjectId):
                id = ObjectId(id)

            filters = {"_id": id}
        else:
            filters = Q(**kwargs)
            filters = self.get_query_from_filters(filters)

        instance = await self.coll(alias).find_one(
            filters, projection=self._loaded_fields.to_query(self.__klass__))
        if instance is None:
            return None
        else:
            doc = self.__klass__.from_son(
                instance,
                # if _loaded_fields is not empty then
                # document is partly loaded
                _is_partly_loaded=bool(self._loaded_fields),
                # set projections for references (if any)
                _reference_loaded_fields=self._reference_loaded_fields,
            )
            if self.is_lazy:
                return doc
            else:
                await doc.load_references()
                return doc
Пример #16
0
    def test_gets_proper_query_when_query_operator_used(self):
        query = Q(first_name__lte="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"whatever": {"$lte": "Test"}})
Пример #17
0
    def test_gets_proper_query_when_list_field(self):
        query = Q(numbers=[10])
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"numbers": {"$all": [10]}})
Пример #18
0
    def test_gets_proper_type(self):
        query = Q(first_name="Test")
        expect(query).to_be_instance_of(Q)

        query = Q(first_name="Test") | Q(first_name="Else")
        expect(query).to_be_instance_of(QCombination)
Пример #19
0
    def test_gets_proper_query(self):
        query = Q(first_name="Test")
        query_result = query.to_query(User)

        expect(query_result).to_be_like({"whatever": "Test"})