-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_sqlquery.py
98 lines (85 loc) · 3.01 KB
/
test_sqlquery.py
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
"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""
import pytest
from simplesqlite.query import And, Or, Set, Where
from simplesqlite.sqlquery import SqlQuery
nan = float("nan")
inf = float("inf")
class Test_SqlQuery_make_update:
@pytest.mark.parametrize(
["table", "set_query", "where", "expected"],
[
["A", "B=1", None, "UPDATE A SET B=1"],
["A", "B=1", Where("C", 1, ">").to_query(), "UPDATE A SET B=1 WHERE C > 1"],
["A", "B=1", Where("C", 1, ">"), "UPDATE A SET B=1 WHERE C > 1"],
[
"A",
"B=1",
And([Where("C", 1, ">"), Where("D", 10)]),
"UPDATE A SET B=1 WHERE C > 1 AND D = 10",
],
[
"A",
"B=1",
Or([Where("C", 1, ">"), Where("D", 10)]),
"UPDATE A SET B=1 WHERE C > 1 OR D = 10",
],
[
"A",
[Set("B1", 10), Set("B2", 20)],
Where("D", 10),
"UPDATE A SET [B1] = 10, [B2] = 20 WHERE D = 10",
],
],
)
def test_normal(self, table, set_query, where, expected):
assert SqlQuery.make_update(table, set_query, where) == expected
@pytest.mark.parametrize(
["table", "set_query", "where", "expected"],
[
[None, "B=1", None, ValueError],
["", "B=1", None, ValueError],
["A", "", None, ValueError],
],
)
def test_exception(self, table, set_query, where, expected):
with pytest.raises(expected):
SqlQuery.make_update(table, set_query, where)
class Test_SqlQuery_make_where_in:
@pytest.mark.parametrize(
["key", "value", "expected"], [["key", ["attr_a", "attr_b"], "key IN ('attr_a', 'attr_b')"]]
)
def test_normal(self, key, value, expected):
assert SqlQuery.make_where_in(key, value) == expected
@pytest.mark.parametrize(
["key", "value", "expected"],
[
["key", None, TypeError],
["key", 1, TypeError],
[None, ["attr_a", "attr_b"], TypeError],
[None, None, TypeError],
],
)
def test_exception(self, key, value, expected):
with pytest.raises(expected):
SqlQuery.make_where_in(key, value)
class Test_SqlQuery_make_where_not_in:
@pytest.mark.parametrize(
["key", "value", "expected"],
[["key", ["attr_a", "attr_b"], "key NOT IN ('attr_a', 'attr_b')"]],
)
def test_normal(self, key, value, expected):
assert SqlQuery.make_where_not_in(key, value) == expected
@pytest.mark.parametrize(
["key", "value", "expected"],
[
["key", None, TypeError],
["key", 1, TypeError],
[None, ["attr_a", "attr_b"], TypeError],
[None, None, TypeError],
],
)
def test_exception(self, key, value, expected):
with pytest.raises(expected):
SqlQuery.make_where_not_in(key, value)