Esempio n. 1
0
    def test_query_construnction_with_multiple_Q_objects_input_to_filter_method(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(Q(last_name='John'),
                                                      Q(age=3))

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert str(
            decon_args
        ) == "(<Q: (AND: ('last_name', 'John'))>, <Q: (AND: ('age', 3))>)"
        assert decon_kwargs == {}
        assert q1._criteria.children[0].connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 2
0
    def test_query_construction_with_multiple_OR_criteria(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(
            Q(last_name='John') | Q(age=3) | Q(name='Jean'))

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert str(decon_args) == ("(<Q: (OR: "
                                   "('last_name', 'John'), "
                                   "('age', 3), "
                                   "('name', 'Jean'))>,)")
        assert decon_kwargs == {}
        assert q1._criteria.children[0].connector == Q.OR
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 3
0
    def test_query_construnction_with_multiple_Q_objects_input_to_exclude_method(
        self, test_domain
    ):
        q1 = test_domain.get_dao(Person).query.exclude(Q(last_name="John"), Q(age=3))

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert (
            str(decon_args)
            == "(<Q: (AND: ('last_name', 'John'))>, <Q: (AND: ('age', 3))>)"
        )
        assert decon_kwargs == {"_negated": True}
        assert q1._criteria.children[0].connector == Q.AND
        assert q1._criteria.negated is True
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 4
0
    def test_query_construction_with_multiple_AND_criteria(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.repository_for(Person)._dao.query.filter(
            Q(last_name="John") & Q(age=3) & Q(name="Jean"))

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert str(decon_args) == ("(<Q: (AND: "
                                   "('last_name', 'John'), "
                                   "('age', 3), "
                                   "('name', 'Jean'))>,)")
        assert decon_kwargs == {}
        assert q1._criteria.children[0].connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 5
0
    def __init__(
        self,
        owner_dao,
        domain,
        entity_cls,
        criteria=None,
        offset: int = 0,
        # Aggregates should be loaded in entirety
        # FIXME Should this limit be removed entirely?
        limit: int = 1000,
        order_by: list = None,
    ):
        """Initialize either with empty preferences (when invoked on an Entity)
        or carry forward filters and preferences when chained
        """
        self._owner_dao = owner_dao
        self._domain = domain
        self._entity_cls = entity_cls
        self._criteria = criteria or Q()
        self._result_cache = None
        self._offset = offset or 0
        self._limit = limit or 10

        # `order_by` could be empty, or a string or a list.
        #   Initialize empty list if `order_by` is None
        #   Convert string to list if `order_by` is a String
        #   Safe-cast list to a list if `order_by` is already a list
        if order_by:
            self._order_by = [order_by] if isinstance(order_by,
                                                      str) else order_by
        else:
            self._order_by = []
    def test_combination_of_OR_followed_by_AND(self, test_domain, create_5_people):
        q1 = test_domain.get_dao(Person).query.filter(
            (Q(last_name='John') | Q(age=7)) & (Q(last_name='Carrie') | Q(age=6)))
        assert q1.total == 0

        q2 = test_domain.get_dao(Person).query.filter(
            (Q(last_name='John') | Q(age__gte=3)) &
            (Q(first_name='Jean') | Q(first_name='Murdock')))
        assert q2.total == 2
Esempio n. 7
0
    def test_combination_of_OR_followed_by_AND(self, test_domain,
                                               create_5_people):
        q1 = test_domain.repository_for(
            Person)._dao.query.filter((Q(last_name="John") | Q(age=7))
                                      & (Q(last_name="Carrie") | Q(age=6)))
        assert q1.total == 0

        q2 = test_domain.repository_for(Person)._dao.query.filter(
            (Q(last_name="John") | Q(age__gte=3))
            & (Q(first_name="Jean") | Q(first_name="Murdock")))
        assert q2.total == 2
Esempio n. 8
0
    def test_query_construction_with_multiple_criteria_in_filter(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(last_name="John", age=3)
        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert decon_args == (("age", 3), ("last_name", "John"))
        assert decon_kwargs == {}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
    def test_combination_of_AND_followed_by_OR(self, test_domain, create_5_people):
        q1 = test_domain.get_dao(Person).query.filter(
            Q(last_name='John', first_name='Jean')
            | Q(age=6))
        assert q1.total == 4

        q2 = test_domain.get_dao(Person).query.filter(Q(last_name='John') | Q(age=6))
        assert q2.total == 5

        q3 = test_domain.get_dao(Person).query.filter(
            (Q(last_name='John') & Q(age=7)) |
            (Q(last_name='Carrie') & Q(age=6)))
        assert q3.total == 3
Esempio n. 10
0
    def test_query_construction_with_simple_exclude_kwargs(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.exclude(last_name="John")

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert decon_args == ()
        assert decon_kwargs == {"last_name": "John", "_negated": True}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is True
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 11
0
    def test_query_construction_with_multiple_criteria_in_exclude(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.exclude(last_name='John', age=3)

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert decon_args == (('age', 3), ('last_name', 'John'))
        assert decon_kwargs == {'_negated': True}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is True
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 12
0
    def test_query_construction_with_simple_filter_kwargs(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(last_name="John")
        assert q1._criteria is not None

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert decon_args == ()
        assert decon_kwargs == {"last_name": "John"}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 13
0
    def test_combination_of_AND_followed_by_OR(self, test_domain,
                                               create_5_people):
        q1 = test_domain.repository_for(Person)._dao.query.filter(
            Q(last_name="John", first_name="Jean") | Q(age=6))
        assert q1.total == 4

        q2 = test_domain.repository_for(Person)._dao.query.filter(
            Q(last_name="John") | Q(age=6))
        assert q2.total == 5

        q3 = test_domain.repository_for(
            Person)._dao.query.filter((Q(last_name="John") & Q(age=7))
                                      | (Q(last_name="Carrie") & Q(age=6)))
        assert q3.total == 3
Esempio n. 14
0
    def test_query_construction_with_multiple_filter_criteria(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(last_name='John')
        q2 = q1.filter(age=3)

        _, decon_args, decon_kwargs = q2._criteria.deconstruct()

        assert decon_args == (('last_name', 'John'), ('age', 3))
        assert decon_kwargs == {}
        assert q1._criteria.connector == Q.AND
        assert q2._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q2._criteria
Esempio n. 15
0
    def test_query_construction_with_multiple_criteria_in_exclude(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.repository_for(Person)._dao.query.exclude(
            last_name="John", age=3)

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert decon_args == (("age", 3), ("last_name", "John"))
        assert decon_kwargs == {"_negated": True}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is True
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 16
0
    def test_query_construction_with_combined_filter_and_exclude_with_exclude_coming_first(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.exclude(age=3).filter(
            last_name='John')

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert str(decon_args
                   ) == "(<Q: (NOT (AND: ('age', 3)))>, ('last_name', 'John'))"
        assert decon_kwargs == {}
        assert q1._criteria.children[0].connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 17
0
    def test_query_construction_with_combined_filter_and_exclude_with_filter_coming_first(
            self, test_domain):
        # Filter by the last_name
        q1 = (test_domain.repository_for(Person)._dao.query.filter(
            last_name="John").exclude(age=3))

        _, decon_args, decon_kwargs = q1._criteria.deconstruct()

        assert (str(decon_args) ==
                "(('last_name', 'John'), <Q: (NOT (AND: ('age', 3)))>)")
        assert decon_kwargs == {}
        assert q1._criteria.connector == Q.AND
        assert q1._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
Esempio n. 18
0
    def test_query_construction_with_multiple_filter_criteria(
            self, test_domain):
        # Filter by the last_name
        q1 = test_domain.repository_for(Person)._dao.query.filter(
            last_name="John")
        q2 = q1.filter(age=3)

        _, decon_args, decon_kwargs = q2._criteria.deconstruct()

        assert decon_args == (("last_name", "John"), ("age", 3))
        assert decon_kwargs == {}
        assert q1._criteria.connector == Q.AND
        assert q2._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q2._criteria
Esempio n. 19
0
    def test_query_construction_with_multiple_exclude_criteria(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.exclude(last_name="John")
        q2 = q1.exclude(age=3)

        _, decon_args, decon_kwargs = q2._criteria.deconstruct()

        assert str(decon_args) == (
            "(<Q: (NOT (AND: ('last_name', 'John')))>, " "<Q: (NOT (AND: ('age', 3)))>)"
        )
        assert decon_kwargs == {}
        assert q1._criteria.connector == Q.AND
        assert q2._criteria.negated is False
        assert Q(*decon_args, **decon_kwargs) == q2._criteria
Esempio n. 20
0
    def __init__(self,
                 owner_dao,
                 domain,
                 entity_cls,
                 criteria=None,
                 offset: int = 0,
                 limit: int = 10,
                 order_by: set = None):
        """Initialize either with empty preferences (when invoked on an Entity)
            or carry forward filters and preferences when chained
        """
        self._owner_dao = owner_dao
        self._domain = domain
        self._entity_cls = entity_cls
        self._criteria = criteria or Q()
        self._result_cache = None
        self._offset = offset or 0
        self._limit = limit or 10

        # `order_by` could be empty, or a string or a list.
        #   Initialize empty list if `order_by` is None
        #   Convert string to list if `order_by` is a String
        #   Safe-cast list to a list if `order_by` is already a list
        if order_by:
            self._order_by = [order_by] if isinstance(order_by,
                                                      str) else order_by
        else:
            self._order_by = []

        # `_temp_cache` is a data container for holding temporary
        #   It holds objects that have been added, but not yet persisted,
        #   as well as objects that have been removed, but are still
        #   present in the data store.
        #
        # The data set returned from Queryset is manipulated automatically
        #   to be up-to-date with temporary changes.
        self._temp_cache = {
            'added': list(),
            'removed': list(),
        }
Esempio n. 21
0
 def test_straightforward_AND_of_two_criteria(self, test_domain,
                                              create_3_people):
     # Filter by the Owner
     q1 = test_domain.get_dao(Person).query.filter(
         Q(last_name="John") & Q(age=3))
     assert q1.total == 1
Esempio n. 22
0
 def test_straightforward_OR_of_two_criteria(self, test_domain,
                                             create_3_people):
     q1 = test_domain.repository_for(Person)._dao.query.filter(
         Q(last_name="John") | Q(age=3))
     assert q1.total == 2
Esempio n. 23
0
    def test_query_construction_with_AND_OR_combinations(self, test_domain):
        # Filter by the last_name
        q1 = test_domain.get_dao(Person).query.filter(
            Q(last_name="John") | Q(age=3), first_name="Jean"
        )
        _, decon_args, decon_kwargs = q1._criteria.deconstruct()
        assert str(decon_args) == (
            "(<Q: (OR: ('last_name', 'John'), ('age', 3))>, " "('first_name', 'Jean'))"
        )
        assert Q(*decon_args, **decon_kwargs) == q1._criteria
        assert q1._criteria.children[0].connector == Q.OR

        q2 = test_domain.get_dao(Person).query.filter(
            (Q(last_name="John") | Q(age=3)), Q(first_name="Jean")
        )
        _, decon_args, decon_kwargs = q2._criteria.deconstruct()
        assert str(decon_args) == (
            "(<Q: (OR: ('last_name', 'John'), ('age', 3))>, "
            "<Q: (AND: ('first_name', 'Jean'))>)"
        )
        assert Q(*decon_args, **decon_kwargs) == q2._criteria
        assert q2._criteria.children[0].connector == Q.OR

        q3 = test_domain.get_dao(Person).query.filter(
            Q(first_name="Jean") & (Q(last_name="John") | Q(age=3))
        )
        _, decon_args, decon_kwargs = q3._criteria.deconstruct()
        assert str(decon_args) == (
            "(<Q: (AND: "
            "('first_name', 'Jean'), "
            "(OR: ('last_name', 'John'), ('age', 3)))>,)"
        )
        assert Q(*decon_args, **decon_kwargs) == q3._criteria
        assert q3._criteria.children[0].connector == Q.AND
Esempio n. 24
0
    def test_NEG_of_criteria(self, test_domain, create_5_people):
        q1 = test_domain.get_dao(Person).query.filter(~Q(last_name='John'))
        assert q1.total == 3

        q2 = test_domain.get_dao(Person).query.filter(~Q(last_name='John') | ~Q(age=7))
        assert q2.total == 4
Esempio n. 25
0
 def test_straightforward_OR_of_two_criteria(self, test_domain, create_3_people):
     q1 = test_domain.get_dao(Person).query.filter(Q(last_name='John') | Q(age=3))
     assert q1.total == 2