-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_basic.py
More file actions
147 lines (128 loc) · 4.14 KB
/
Copy pathtest_basic.py
File metadata and controls
147 lines (128 loc) · 4.14 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
from sqlwhat.test_exercise import test_exercise as te
from tests.helper import Connection
import pytest
@pytest.fixture
def conn():
return Connection("postgresql")
def test_pass(conn):
result = {"id": [1], "name": ["greg"]}
sct_payload = te(
sct="Ex().check_result()",
student_code="SELECT * FROM company",
solution_code="SELECT * FROM company",
pre_exercise_code="",
student_conn=conn,
solution_conn=None,
student_result=result,
solution_result=result,
ex_type="NormalExercise",
error=[],
)
assert sct_payload.get("correct") is True
def test_fail(conn):
sol_result = {"id": [1], "name": ["greg"]}
stu_result = {"id": [1, 2], "name": ["greg", "fred"]}
sct_payload = te(
sct="Ex().check_result()",
student_code="SELECT * FROM company",
solution_code="SELECT * FROM company",
pre_exercise_code="",
student_conn=conn,
solution_conn=None,
student_result=stu_result,
solution_result=sol_result,
ex_type="NormalExercise",
error=[],
)
assert sct_payload.get("correct") is False
xfail_def = pytest.mark.xfail(reason="implement deferrel")
@pytest.mark.parametrize(
"sct",
[
"Ex().check_node('SelectStmt')",
"Ex().check_node('SelectStmt', priority=99)",
"Ex().check_node('SelectStmt').check_edge('target_list')",
"Ex().has_code('SELECT')",
"Ex().has_equal_ast()",
"Ex().multi(has_code('SELECT'))",
"Ex().check_or(has_code('SELECT'))",
"Ex().check_correct(has_code('SELECT'), has_code('SEL'))",
"Ex().check_correct(has_code('SELECT'), has_code('SEL').has_code('SEL'))",
"Ex().check_not(has_code('WHERE'), incorrect_msg='do not write WHERE')",
"Ex().has_result()",
"Ex().has_nrows()",
"Ex().has_ncols()",
"Ex().check_column('id')",
"Ex().check_column('id').has_equal_value()",
],
)
def test_test_exercise_pass(conn, sct):
result = {"id": [1], "name": ["greg"]}
sct_payload = te(
sct=sct,
student_code="SELECT * FROM company",
solution_code="SELECT * FROM company",
pre_exercise_code="",
student_conn=conn,
solution_conn=None,
student_result=result,
solution_result=result,
ex_type="NormalExercise",
error=[],
)
assert sct_payload.get("correct") is True
@pytest.mark.parametrize(
"sct",
[
"Ex().check_node('SelectStmt').check_edge('target_list').has_equal_ast()",
"Ex().has_code('id', fixed=True)",
"Ex().has_equal_ast()",
"Ex().multi(has_code('id'))",
"Ex().check_or(has_code('id'))",
"Ex().check_correct(has_code('id'), has_code('i'))",
"Ex().has_nrows()",
"Ex().has_ncols()",
"Ex().check_column('id')",
"Ex().check_column('name').has_equal_value()",
],
)
def test_test_exercise_fail(conn, sct):
sol_result = {"id": [1], "name": ["greg"]}
stu_result = {"id2": [1, 2], "name": ["greg", "fred"], "c": [1, 2]}
sct_payload = te(
sct=sct,
student_code="SELECT * FROM company",
solution_code="SELECT id FROM company",
pre_exercise_code="",
student_conn=conn,
solution_conn=None,
student_result=stu_result,
solution_result=sol_result,
ex_type="NormalExercise",
error=[],
)
assert sct_payload.get("correct") is False
@pytest.mark.parametrize(
"sct, passes, msg",
[
("", False, "Your code generated an error. Fix it and try again!"),
('Ex().has_no_error(incorrect_msg="wow")', False, "wow"),
("Ex().allow_error()", True, None),
],
)
def test_error_handling(sct, passes, msg):
sct_payload = te(
sct=sct,
student_code="",
solution_code="",
pre_exercise_code="",
student_conn=None,
solution_conn=None,
student_result={},
solution_result={},
ex_type="NormalExercise",
error=["an error"],
)
assert sct_payload.get("correct") == passes
if msg:
assert sct_payload.get("message") == msg