summaryrefslogtreecommitdiff
path: root/test/sql/test_utils.py
blob: 84e7ad7328b7fcd2dcef88a0ad03a12ae284698f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from sqlalchemy.testing import fixtures, is_true, is_false
from sqlalchemy import MetaData, Table, Column, Integer, String
from sqlalchemy import and_, or_, bindparam
from sqlalchemy.sql.elements import ClauseList
from sqlalchemy.sql import operators


class CompareClausesTest(fixtures.TestBase):
    def setup(self):
        m = MetaData()
        self.a = Table(
            'a', m,
            Column('x', Integer),
            Column('y', Integer)
        )

        self.b = Table(
            'b', m,
            Column('y', Integer),
            Column('z', Integer)
        )

    def test_compare_clauselist_associative(self):

        l1 = and_(
            self.a.c.x == self.b.c.y,
            self.a.c.y == self.b.c.z
        )

        l2 = and_(
            self.a.c.y == self.b.c.z,
            self.a.c.x == self.b.c.y,
        )

        l3 = and_(
            self.a.c.x == self.b.c.z,
            self.a.c.y == self.b.c.y
        )

        is_true(l1.compare(l1))
        is_true(l1.compare(l2))
        is_false(l1.compare(l3))

    def test_compare_clauselist_not_associative(self):

        l1 = ClauseList(
            self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.sub)

        l2 = ClauseList(
            self.b.c.y, self.a.c.x, self.a.c.y, operator=operators.sub)

        is_true(l1.compare(l1))
        is_false(l1.compare(l2))

    def test_compare_clauselist_assoc_different_operator(self):

        l1 = and_(
            self.a.c.x == self.b.c.y,
            self.a.c.y == self.b.c.z
        )

        l2 = or_(
            self.a.c.y == self.b.c.z,
            self.a.c.x == self.b.c.y,
        )

        is_false(l1.compare(l2))

    def test_compare_clauselist_not_assoc_different_operator(self):

        l1 = ClauseList(
            self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.sub)

        l2 = ClauseList(
            self.a.c.x, self.a.c.y, self.b.c.y, operator=operators.div)

        is_false(l1.compare(l2))

    def test_compare_binds(self):
        b1 = bindparam("foo", type_=Integer())
        b2 = bindparam("foo", type_=Integer())
        b3 = bindparam("bar", type_=Integer())
        b4 = bindparam("foo", type_=String())

        def c1(): return 5

        def c2(): return 6

        b5 = bindparam("foo", type_=Integer(), callable_=c1)
        b6 = bindparam("foo", type_=Integer(), callable_=c2)
        b7 = bindparam("foo", type_=Integer(), callable_=c1)

        b8 = bindparam("foo", type_=Integer, value=5)
        b9 = bindparam("foo", type_=Integer, value=6)

        is_false(b1.compare(b5))
        is_true(b5.compare(b7))
        is_false(b5.compare(b6))
        is_true(b1.compare(b2))

        # currently not comparing "key", as we often have to compare
        # anonymous names.  however we should really check for that
        is_true(b1.compare(b3))

        is_false(b1.compare(b4))
        is_false(b1.compare(b8))
        is_false(b8.compare(b9))
        is_true(b8.compare(b8))