-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_protowhat_messaging.py
More file actions
240 lines (223 loc) · 7.69 KB
/
test_protowhat_messaging.py
File metadata and controls
240 lines (223 loc) · 7.69 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import importlib
import pytest
from protowhat.sct_syntax import link_to_state
from protowhat.checks.check_funcs import (
check_node,
check_edge,
has_equal_ast,
has_code,
has_parsed_ast,
)
from protowhat.selectors import Dispatcher
from sqlwhat.State import State, PARSER_MODULES
from protowhat.Reporter import Reporter
from protowhat.failure import TestFail as TF
check_node = link_to_state(check_node)
check_edge = link_to_state(check_edge)
has_equal_ast = link_to_state(has_equal_ast)
def print_message(exc):
print(exc.value.payload["message"])
def prepare_state(sol_code, stu_code, dialect="postgresql"):
dispatcher = Dispatcher.from_module(PARSER_MODULES[dialect])
return State(
student_code=stu_code,
solution_code=sol_code,
reporter=Reporter(),
# args below should be ignored
pre_exercise_code="NA",
student_result=[],
solution_result=[],
student_conn=None,
solution_conn=None,
ast_dispatcher=dispatcher,
)
# get_ast_path ----------------------------------------------------------------
@pytest.mark.parametrize(
"query, path, target_desc",
[
(
"SELECT a as c FROM b",
[[check_node, ["SelectStmt"]]],
"first `SELECT` statement",
),
(
"SELECT a as c FROM b",
[[check_node, ["SelectStmt"]], [check_node, ["AliasExpr", 0]]],
"first alias expression",
),
(
"SELECT a FROM b",
[[check_node, ["SelectStmt"]], [check_edge, ["from_clause", None]]],
"`FROM` clause of the `SELECT` statement",
),
(
"SELECT a FROM b",
[[check_node, ["SelectStmt"]], [check_edge, ["from_clause"]]],
"first entry in the `FROM` clause of the `SELECT` statement",
),
(
"SELECT a FROM b",
[[check_node, ["SelectStmt"]], [check_edge, ["target_list", 0]]],
"first entry in the target list of the `SELECT` statement",
),
(
"SELECT a FROM b INNER JOIN c HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_edge, ["from_clause"]],
[check_edge, ["left"]],
],
None, # Doesn't generate anything for now!
),
(
"SELECT a FROM b INNER JOIN c HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left", None]],
],
"left operand of the join expression", # using check_node instead _does_ generate something
),
(
"SELECT a FROM b INNER JOIN c HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left"]],
],
"first entry in the left operand of the join expression", # using check_node instead _does_ generate something
),
(
"SELECT a FROM b WHERE c BETWEEN 1 AND 2",
[
[check_node, ["SelectStmt"]],
[check_edge, ["where_clause"]],
[check_edge, ["left"]],
],
None, # Doesn't generate anything!
),
(
"SELECT a FROM b WHERE c BETWEEN 1 AND 2",
[
[check_node, ["SelectStmt"]],
[check_node, ["BinaryExpr"]],
[check_edge, ["left", None]],
],
"left operand of the binary expression `between`", # using check_node instead _does_ generate something
),
(
"SELECT a FROM b WHERE c BETWEEN 1 AND 2",
[
[check_node, ["SelectStmt"]],
[check_node, ["BinaryExpr"]],
[check_edge, ["left"]],
],
"first entry in the left operand of the binary expression `between`", # using check_node instead _does_ generate something
),
],
)
def test_get_ast_path(query, path, target_desc):
state = prepare_state(query, query)
for step in path:
state = step[0](state, *step[1])
assert state.get_ast_path() == target_desc
# check_node - check_edge -----------------------------------------------------------------
@pytest.mark.parametrize(
"stu, path, msg",
[
(
"",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left"]],
[has_equal_ast, []],
],
"Could not find the first `SELECT` statement.",
),
(
"SELECT a FROM c",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left"]],
[has_equal_ast, []],
],
"Check the first `SELECT` statement. Could not find the first join expression.",
),
(
"SELECT a FROM c INNER JOIN b HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left", None]],
[has_equal_ast, []],
],
"Check the left operand of the join expression. The checker expected to find `b` in there.",
),
(
"SELECT a FROM c INNER JOIN b HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_node, ["JoinExpr"]],
[check_edge, ["left"]],
[has_equal_ast, []],
],
"Check the first entry in the left operand of the join expression. The checker expected to find `b` in there.",
),
(
# using check_edge('from_clause') instead of check_node('JoinExpr')
"SELECT a FROM c INNER JOIN b HAVING(d)",
[
[check_node, ["SelectStmt"]],
[check_edge, ["from_clause"]],
[check_edge, ["left"]],
[has_equal_ast, []],
],
"Check the highlighted code. The checker expected to find `b` in there.",
),
],
)
def test_check_messaging(stu, path, msg):
state = prepare_state("SELECT a FROM b INNER JOIN c HAVING(d)", stu)
with pytest.raises(TF, match=msg):
for step in path:
state = step[0](state, *step[1])
@pytest.mark.parametrize(
"stu, path, msg",
[
(
"SELECT a FROM b ORDER BY b",
[
[check_node, ["SelectStmt"]],
[check_edge, ["order_by_clause", None]],
[has_equal_ast, []],
],
"Check the `ORDER BY` clause of the `SELECT` statement. Something is missing.",
),
(
"SELECT a FROM b ORDER BY b",
[
[check_node, ["SelectStmt"]],
[check_edge, ["order_by_clause"]],
[has_equal_ast, []],
],
"Check the first entry in the `ORDER BY` clause of the `SELECT` statement. The checker expected to find `ORDER BY c` in there.",
),
(
"SELECT a FROM b ORDER BY b",
[
[check_node, ["SelectStmt"]],
[check_edge, ["order_by_clause"]],
[check_node, ["SortBy"]],
[has_equal_ast, []],
],
"Check the first sorting expression. The checker expected to find `c` in there.",
),
],
)
def test_check_messaging_2(stu, path, msg):
state = prepare_state("SELECT a FROM b ORDER BY c", stu)
with pytest.raises(TF, match=msg):
for step in path:
state = step[0](state, *step[1])