Ejemplo n.º 1
0
    def test_impossible_or_query(self):
        qs = TestUser.objects.filter(username="******").filter(
            Q(username__in=["ruby", "jruby"])
            | (Q(username="******") & ~Q(username="******")))

        with self.assertRaises(EmptyResultSet):
            normalize_query(transform_query(connections['default'], qs.query))
Ejemplo n.º 2
0
    def test_and_with_child_or_promoted(self):
        """
            Given the following tree:

                   AND
                  / | \
                 A  B OR
                      / \
                     C   D

             The OR should be promoted, so the resulting tree is

                      OR
                     /   \
                   AND   AND
                  / | \ / | \
                 A  B C A B D
        """

        query = Query(TestUser, "SELECT")
        query.where = WhereNode('default')
        query.where.children.append(WhereNode('default'))
        query.where.children[-1].column = "A"
        query.where.children[-1].operator = "="
        query.where.children.append(WhereNode('default'))
        query.where.children[-1].column = "B"
        query.where.children[-1].operator = "="
        query.where.children.append(WhereNode('default'))
        query.where.children[-1].connector = "OR"
        query.where.children[-1].children.append(WhereNode('default'))
        query.where.children[-1].children[-1].column = "C"
        query.where.children[-1].children[-1].operator = "="
        query.where.children[-1].children.append(WhereNode('default'))
        query.where.children[-1].children[-1].column = "D"
        query.where.children[-1].children[-1].operator = "="

        query = normalize_query(query)

        self.assertEqual(query.where.connector, "OR")
        self.assertEqual(2, len(query.where.children))
        self.assertFalse(query.where.children[0].is_leaf)
        self.assertFalse(query.where.children[1].is_leaf)
        self.assertEqual(query.where.children[0].connector, "AND")
        self.assertEqual(query.where.children[1].connector, "AND")
        self.assertEqual(3, len(query.where.children[0].children))
        self.assertEqual(3, len(query.where.children[1].children))
Ejemplo n.º 3
0
    def test_or_queries(self):
        qs = TestUser.objects.filter(first_name="python").filter(
            Q(username__in=["ruby", "jruby"])
            | (Q(username="******") & ~Q(username="******")))

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        # After IN and != explosion, we have...
        # (AND: (first_name='python', OR: (username='******', username='******', AND: (username='******', AND: (username < 'perl', username > 'perl')))))

        # Working backwards,
        # AND: (username < 'perl', username > 'perl') can't be simplified
        # AND: (username='******', AND: (username < 'perl', username > 'perl')) can become (OR: (AND: username = '******', username < 'perl'), (AND: username='******', username > 'perl'))
        # OR: (username='******', username='******', (OR: (AND: username = '******', username < 'perl'), (AND: username='******', username > 'perl')) can't be simplified
        # (AND: (first_name='python', OR: (username='******', username='******', (OR: (AND: username = '******', username < 'perl'), (AND: username='******', username > 'perl'))
        # becomes...
        # (OR: (AND: first_name='python', username = '******'), (AND: first_name='python', username='******'), (AND: first_name='python', username='******', username < 'perl') \
        #      (AND: first_name='python', username='******', username > 'perl')

        self.assertTrue(4, len(query.where.children[0].children))

        self.assertEqual(query.where.children[0].connector, "AND")

        possible_children = [query.where.children[i] for i in range(4)]

        expected = find_children_containing_node(possible_children, "username",
                                                 "=", "jruby")
        self.assertTrue(
            find_children_containing_node([expected], "first_name", "=",
                                          "python"))
        self.assertTrue(
            find_children_containing_node([expected], "username", "=",
                                          "jruby"))

        expected = find_children_containing_node(possible_children, "username",
                                                 ">", "perl")
        self.assertTrue(
            find_children_containing_node([expected], "first_name", "=",
                                          "python"))
        self.assertTrue(
            find_children_containing_node([expected], "username", "=", "php"))
        self.assertTrue(
            find_children_containing_node([expected], "username", ">", "perl"))

        expected = find_children_containing_node(possible_children, "username",
                                                 "<", "perl")
        self.assertTrue(
            find_children_containing_node([expected], "first_name", "=",
                                          "python"))
        self.assertTrue(
            find_children_containing_node([expected], "username", "=", "php"))
        self.assertTrue(
            find_children_containing_node([expected], "username", "<", "perl"))

        expected = find_children_containing_node(possible_children, "username",
                                                 "=", "ruby")
        self.assertTrue(
            find_children_containing_node([expected], "first_name", "=",
                                          "python"))
        self.assertTrue(
            find_children_containing_node([expected], "username", "=", "ruby"))

        qs = TestUser.objects.filter(
            username="******") | TestUser.objects.filter(username="******")

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertEqual(query.where.connector, "OR")
        self.assertEqual(2, len(query.where.children))
        self.assertTrue(query.where.children[0].is_leaf)
        self.assertTrue("cheese" in (query.where.children[0].value,
                                     query.where.children[1].value))
        self.assertTrue(query.where.children[1].is_leaf)
        self.assertTrue("test" in (query.where.children[0].value,
                                   query.where.children[1].value))

        qs = TestUser.objects.using("default").filter(
            username__in=set()).values_list('email')

        with self.assertRaises(EmptyResultSet):
            query = normalize_query(
                transform_query(connections['default'], qs.query))

        qs = TestUser.objects.filter(
            username__startswith='Hello') | TestUser.objects.filter(
                username__startswith='Goodbye')

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertEqual(2, len(query.where.children))
        self.assertEqual("_idx_startswith_username",
                         query.where.children[0].column)
        self.assertTrue("Goodbye" in (query.where.children[0].value,
                                      query.where.children[1].value))
        self.assertEqual("_idx_startswith_username",
                         query.where.children[1].column)
        self.assertTrue("Hello" in (query.where.children[0].value,
                                    query.where.children[1].value))

        qs = TestUser.objects.filter(pk__in=[1, 2, 3])
        query = normalize_query(
            transform_query(connections['default'], qs.query))

        rpc = transaction._rpc(default_connection.alias)

        self.assertEqual(3, len(query.where.children))
        self.assertEqual("__key__", query.where.children[0].column)
        self.assertEqual("__key__", query.where.children[1].column)
        self.assertEqual("__key__", query.where.children[2].column)
        self.assertEqual(
            {
                rpc.key(TestUser._meta.db_table, 1),
                rpc.key(TestUser._meta.db_table, 2),
                rpc.key(TestUser._meta.db_table, 3),
            }, {
                query.where.children[0].value,
                query.where.children[1].value,
                query.where.children[2].value,
            })

        qs = TestUser.objects.filter(pk__in=[1, 2, 3]).filter(username="******")
        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertEqual(3, len(query.where.children))
        self.assertEqual("__key__", query.where.children[0].children[0].column)
        self.assertEqual("test", query.where.children[0].children[1].value)

        self.assertEqual("__key__", query.where.children[1].children[0].column)
        self.assertEqual("test", query.where.children[0].children[1].value)

        self.assertEqual("__key__", query.where.children[2].children[0].column)
        self.assertEqual("test", query.where.children[0].children[1].value)

        self.assertEqual(
            {
                rpc.key(TestUser._meta.db_table, 1),
                rpc.key(TestUser._meta.db_table, 2),
                rpc.key(TestUser._meta.db_table, 3),
            }, {
                query.where.children[0].children[0].value,
                query.where.children[1].children[0].value,
                query.where.children[2].children[0].value,
            })
Ejemplo n.º 4
0
    def test_and_queries(self):
        qs = TestUser.objects.filter(username="******").all()

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertTrue(1, len(query.where.children))
        self.assertEqual(query.where.children[0].children[0].column,
                         "username")
        self.assertEqual(query.where.children[0].children[0].operator, "=")
        self.assertEqual(query.where.children[0].children[0].value, "test")

        qs = TestUser.objects.filter(username="******", email="*****@*****.**")

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertTrue(2, len(query.where.children[0].children))

        self.assertEqual(query.where.connector, "OR")
        self.assertEqual(query.where.children[0].connector, "AND")

        self.assertTrue(
            find_children_containing_node([query.where.children[0]],
                                          "username", "=", "test"))
        self.assertTrue(
            find_children_containing_node([query.where.children[0]], "email",
                                          "=", "*****@*****.**"))

        qs = TestUser.objects.filter(username="******").exclude(
            email="*****@*****.**")
        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertTrue(2, len(query.where.children[0].children))
        self.assertEqual(query.where.connector, "OR")
        self.assertEqual(query.where.children[0].connector, "AND")

        possible_children = (query.where.children[0], query.where.children[1])

        expected = find_children_containing_node(possible_children, "email",
                                                 "<", "*****@*****.**")
        self.assertTrue(
            find_children_containing_node([expected], "username", "=", "test"))
        self.assertTrue(
            find_children_containing_node([expected], "email", "<",
                                          "*****@*****.**"))

        expected = find_children_containing_node(possible_children, "email",
                                                 ">", "*****@*****.**")
        self.assertTrue(
            find_children_containing_node([expected], "username", "=", "test"))
        self.assertTrue(
            find_children_containing_node([expected], "email", ">",
                                          "*****@*****.**"))

        instance = Relation(pk=1)
        qs = instance.related_set.filter(headline__startswith='Fir')

        query = normalize_query(
            transform_query(connections['default'], qs.query))

        self.assertTrue(2, len(query.where.children[0].children))
        self.assertEqual(query.where.connector, "OR")
        self.assertEqual(query.where.children[0].connector, "AND")
        self.assertEqual(query.where.children[0].children[0].column,
                         "relation_id")
        self.assertEqual(query.where.children[0].children[0].operator, "=")
        self.assertEqual(query.where.children[0].children[0].value, 1)
        self.assertEqual(query.where.children[0].children[1].column,
                         "_idx_startswith_headline")
        self.assertEqual(query.where.children[0].children[1].operator, "=")
        self.assertEqual(query.where.children[0].children[1].value, u"Fir")