Skip to content

Commit 29be397

Browse files
authored
Merge pull request #1 from datacamp/colin-msft-check
added tsql function and tests
2 parents 09f0332 + 73c5004 commit 29be397

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ sqlwhat-ext
44
[![Build Status](https://travis-ci.org/datacamp/sqlwhat-ext.svg?branch=master)](https://travis-ci.org/datacamp/sqlwhat-ext)
55
[![PyPI version](https://badge.fury.io/py/sqlwhat-ext.svg)](https://badge.fury.io/py/sqlwhat-ext)
66

7-
Extensions (high-level SCTs) for sqlwhat.
7+
Extensions (high-level SCTs) for sqlwhat.
88

99
[Documentation here](http://sqlwhat-ext.readthedocs.io/).
1010

@@ -43,5 +43,7 @@ Running tests
4343
pip install -r requirements.txt
4444
# may need to uncomment line below
4545
#pip install -e .
46-
py.test tests
46+
pytest tests
47+
48+
# can also do: python -m pytest tests
4749
```

sqlwhat_ext.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__version__ = '0.0.1'
22

33
from sqlwhat.sct_syntax import *
4+
from sqlwhat.checks.check_result import TinyNone
45

56
@state_dec
67
def check_result2(state, col_names = None, sort = False, match = 'exact'):
@@ -31,3 +32,33 @@ def check_result2(state, col_names = None, sort = False, match = 'exact'):
3132

3233
# return state in case another SCT is used after
3334
return state
35+
36+
@state_dec
37+
def check_result_tsql(state, msg="Incorrect result."):
38+
"""High level function which wraps other SCTs for checking results."""
39+
40+
stu_res = state.student_result
41+
sol_res = state.solution_result
42+
43+
# empty test
44+
test_has_columns(state)
45+
# row test
46+
test_nrows(state)
47+
# column tests
48+
child = _sort_columns_indiv(state)
49+
for k in sol_res:
50+
msg = "Column `{}` in the solution does not have a column with matching values in your results."
51+
test_column(child, k, msg, match = 'any')
52+
53+
return state
54+
55+
def _sort_columns_indiv(state):
56+
tiny_none = TinyNone()
57+
sorted_columns = lambda res: {k: sorted(col, key = lambda el: el or tiny_none) for k, col in res.items()}
58+
59+
stu_res = sorted_columns(state.student_result)
60+
sol_res = sorted_columns(state.solution_result)
61+
62+
return state.to_child(
63+
student_result = stu_res,
64+
solution_result = sol_res)

tests/test_check_result_tsql.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from sqlwhat.Reporter import Reporter
2+
from sqlwhat.State import State
3+
from sqlwhat.selectors import Dispatcher
4+
from sqlwhat.Test import TestFail as TF
5+
6+
import pytest
7+
8+
def prepare_state(sol_result, stu_result):
9+
dispatcher = Dispatcher.from_dialect('postgresql')
10+
11+
return State(
12+
student_code = "",
13+
solution_code = "",
14+
reporter = Reporter(),
15+
# args below should be ignored
16+
pre_exercise_code = "",
17+
student_result = stu_result, solution_result = sol_result,
18+
student_conn = None, solution_conn = None,
19+
ast_dispatcher = dispatcher)
20+
21+
22+
from sqlwhat.sct_syntax import Ex
23+
from sqlwhat_ext import check_result_tsql
24+
25+
def test_check_result_tsql_chain():
26+
state = prepare_state({'a': [1,2,3]}, {'a': [1,2,3]})
27+
Ex(state) >> check_result_tsql()
28+
29+
@pytest.mark.parametrize('sol_res,stu_res', [ # pass cases where...
30+
( {'a': [1,2,3]}, {'a': [1,2,3]} ), # identical
31+
( {'a': [1,2,3]}, {'b': [1,2,3]} ), # diff colnames
32+
( {'a': [1,2,3]}, {'b': [3,2,1]} ), # diff colnames + reordered
33+
( {'a': [1]} , {'a': [1], 'b': [2]} ) # extra cols in student result
34+
])
35+
def test_check_result_tsql_pass(sol_res, stu_res):
36+
state = prepare_state(sol_res, stu_res)
37+
check_result_tsql(state)
38+
39+
@pytest.mark.parametrize('sol_res,stu_res', [ # fail cases where...
40+
( {'a': [1,2,3]}, {'a': [1,2]} ), # too few rows
41+
( {'a': [1,2,3]}, {'a': [1,2,2]} ) # values mismatch
42+
])
43+
def test_check_result_tsql_match_fail(sol_res, stu_res):
44+
state = prepare_state(sol_res, stu_res)
45+
with pytest.raises(TF):
46+
check_result_tsql(state)

0 commit comments

Comments
 (0)