From be02ebf414485d96bd8dfb49bdf0cd5b777ca909 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:03:11 +0100 Subject: [PATCH 01/12] Update test instructions backend tests were removed earlier --- README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/README.md b/README.md index 4482fd7..2f86a3b 100644 --- a/README.md +++ b/README.md @@ -68,14 +68,6 @@ Ex(state).check_result() ## Running unit tests ```bash -pytest -m "not backend" -``` - -If you also want to run the backend tests, you need to set a `GITHUB_TOKEN` environment variable with access to the (private) `sqlbackend` repository. -After this, you can: - -```bash -make install pytest ``` From c63ade2cd46300e0769ead4daf563ed7e742b515 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:03:36 +0100 Subject: [PATCH 02/12] Update Python version --- .python-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.python-version b/.python-version index 87ce492..fa8da20 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.5.2 +3.5.8 From 6ff6af6da7eca88da55c78cee178ea43c8ee83a9 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:07:38 +0100 Subject: [PATCH 03/12] Update TestFail import --- sqlwhat/test_exercise.py | 2 +- tests/test_check_funcs.py | 2 +- tests/test_has_funcs.py | 2 +- tests/test_line_info.py | 3 ++- tests/test_messaging.py | 2 +- tests/test_protowhat_check_funcs.py | 2 +- tests/test_protowhat_messaging.py | 2 +- tests/test_selectors.py | 2 +- tests/test_state.py | 2 +- 9 files changed, 10 insertions(+), 9 deletions(-) diff --git a/sqlwhat/test_exercise.py b/sqlwhat/test_exercise.py index 32e9951..8c8fcdc 100644 --- a/sqlwhat/test_exercise.py +++ b/sqlwhat/test_exercise.py @@ -1,5 +1,5 @@ from sqlwhat.State import State -from protowhat.Test import TestFail +from protowhat.failure import TestFail from protowhat.Reporter import Reporter from sqlwhat.sct_syntax import SCT_CTX diff --git a/tests/test_check_funcs.py b/tests/test_check_funcs.py index 2ea24fe..d914709 100644 --- a/tests/test_check_funcs.py +++ b/tests/test_check_funcs.py @@ -1,6 +1,6 @@ import pytest -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF from tests.helper import ( prepare_state, passes, diff --git a/tests/test_has_funcs.py b/tests/test_has_funcs.py index 68f20a4..8a1d29e 100644 --- a/tests/test_has_funcs.py +++ b/tests/test_has_funcs.py @@ -1,5 +1,5 @@ import pytest -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF from sqlwhat.checks.has_funcs import sort_rows from tests.helper import ( diff --git a/tests/test_line_info.py b/tests/test_line_info.py index 95e200b..599e413 100644 --- a/tests/test_line_info.py +++ b/tests/test_line_info.py @@ -1,4 +1,5 @@ -from protowhat.Test import Feedback, TestFail as TF +from protowhat.failure import TestFail as TF +from protowhat.Feedback import Feedback from protowhat.Reporter import Reporter from protowhat.selectors import Dispatcher from sqlwhat.State import State, PARSER_MODULES diff --git a/tests/test_messaging.py b/tests/test_messaging.py index d2ebcae..ba6ae79 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -4,7 +4,7 @@ from protowhat.selectors import Dispatcher from sqlwhat.State import State, PARSER_MODULES from protowhat.Reporter import Reporter -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF from tests.helper import ( Connection, has_result, diff --git a/tests/test_protowhat_check_funcs.py b/tests/test_protowhat_check_funcs.py index 2ed9220..e02ed5b 100644 --- a/tests/test_protowhat_check_funcs.py +++ b/tests/test_protowhat_check_funcs.py @@ -11,7 +11,7 @@ from protowhat.selectors import Dispatcher from sqlwhat.State import State, PARSER_MODULES from protowhat.Reporter import Reporter -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF @pytest.fixture diff --git a/tests/test_protowhat_messaging.py b/tests/test_protowhat_messaging.py index bed3ce1..9a06904 100644 --- a/tests/test_protowhat_messaging.py +++ b/tests/test_protowhat_messaging.py @@ -12,7 +12,7 @@ from protowhat.selectors import Dispatcher from sqlwhat.State import State, PARSER_MODULES from protowhat.Reporter import Reporter -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF check_node = link_to_state(check_node) diff --git a/tests/test_selectors.py b/tests/test_selectors.py index d9277c8..1a09007 100644 --- a/tests/test_selectors.py +++ b/tests/test_selectors.py @@ -2,7 +2,7 @@ from sqlwhat.State import State, PARSER_MODULES import importlib from protowhat.Reporter import Reporter -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF import pytest diff --git a/tests/test_state.py b/tests/test_state.py index d0d5619..755a80d 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -3,7 +3,7 @@ from sqlwhat.sct_syntax import Ex from sqlwhat.State import State from protowhat.Reporter import Reporter -from protowhat.Test import TestFail as TF +from protowhat.failure import TestFail as TF from tests.helper import Connection import pytest From 4874406fe156a2a7e7a7f0c92859af1857c8996f Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:08:42 +0100 Subject: [PATCH 04/12] Update _debug import --- sqlwhat/checks/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sqlwhat/checks/__init__.py b/sqlwhat/checks/__init__.py index e9c944d..8b2c1ac 100644 --- a/sqlwhat/checks/__init__.py +++ b/sqlwhat/checks/__init__.py @@ -25,4 +25,4 @@ ) from protowhat.checks.check_logic import fail, multi, check_not, check_or, check_correct from protowhat.checks.check_simple import has_chosen, success_msg, allow_errors -from protowhat.utils import _debug +from protowhat.failure import _debug From a849197a35fde3773a372be805bda7e7b84f0696 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:15:53 +0100 Subject: [PATCH 05/12] Update context creation --- sqlwhat/sct_syntax.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sqlwhat/sct_syntax.py b/sqlwhat/sct_syntax.py index 33b51a1..be1ecf2 100644 --- a/sqlwhat/sct_syntax.py +++ b/sqlwhat/sct_syntax.py @@ -4,9 +4,9 @@ from sqlwhat import checks from protowhat.sct_syntax import get_checks_dict, create_sct_context -# used in Chain and F, to know what methods are available +# used by chains, to know what methods are available sct_dict = get_checks_dict(checks) -SCT_CTX = create_sct_context(State, sct_dict) +SCT_CTX = create_sct_context(sct_dict) # used in test_exercise, so that scts without Ex() don't run immediately globals().update(SCT_CTX) From bc1999b31e8dfe75b1e96348e3b2c22215fa3cbf Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:45:06 +0100 Subject: [PATCH 06/12] Replace build_message calls --- sqlwhat/checks/check_funcs.py | 19 ++++++++----------- sqlwhat/checks/has_funcs.py | 15 +++------------ 2 files changed, 11 insertions(+), 23 deletions(-) diff --git a/sqlwhat/checks/check_funcs.py b/sqlwhat/checks/check_funcs.py index 28495ae..324f5a4 100644 --- a/sqlwhat/checks/check_funcs.py +++ b/sqlwhat/checks/check_funcs.py @@ -1,4 +1,5 @@ from protowhat.Feedback import Feedback +from protowhat.failure import debugger from protowhat.sct_syntax import link_to_state from protowhat.checks.check_simple import allow_errors @@ -72,8 +73,7 @@ def check_row(state, index, missing_msg=None, expand_msg=None): ) if index >= n_stu: - _msg = state.build_message(missing_msg, fmt_kwargs=msg_kwargs) - state.report(_msg) + state.report(missing_msg, msg_kwargs) return state.to_child( append_message={"msg": expand_msg, "kwargs": msg_kwargs}, @@ -130,8 +130,7 @@ def check_column(state, name, missing_msg=None, expand_msg=None): raise BaseException("name %s not in solution column names" % name) if name not in stu_res: - _msg = state.build_message(missing_msg, fmt_kwargs=msg_kwargs) - state.report(_msg) + state.report(missing_msg, msg_kwargs) return state.to_child( append_message={"msg": expand_msg, "kwargs": msg_kwargs}, @@ -199,11 +198,10 @@ def check_all_columns(state, allow_extra=True, too_many_cols_msg=None, expand_ms set(state.student_result.keys()) - set(child_stu_result.keys()) ) if not allow_extra and len(cols_not_in_sol) > 0: - _msg = state.build_message( + state.report( "Your query result contains the column `{{col}}` but shouldn't.", - fmt_kwargs={"col": cols_not_in_sol[0]}, + {"col": cols_not_in_sol[0]}, ) - state.report(_msg) return state.to_child( append_message={"msg": expand_msg, "kwargs": {}}, @@ -298,8 +296,6 @@ def check_query(state, query, error_msg=None, expand_msg=None): # make sure that it didn't generate any errors has_no_error(state) - _msg = state.build_message(error_msg, fmt_kwargs=msg_kwargs) - # sqlbackend makes sure all queries are run in transactions. # Rerun the solution code first, after which we run the provided query with dbconn(state.solution_conn) as conn: @@ -307,7 +303,8 @@ def check_query(state, query, error_msg=None, expand_msg=None): sol_res = runQuery(conn, query) if sol_res is None: - raise ValueError("Solution failed: " + _msg) + with debugger(state): + state.report("Solution failed: " + error_msg) # sqlbackend makes sure all queries are run in transactions. # Rerun the student code first, after wich we run the provided query @@ -316,7 +313,7 @@ def check_query(state, query, error_msg=None, expand_msg=None): stu_res = runQuery(conn, query) if stu_res is None: - state.report(_msg) + state.report(error_msg, msg_kwargs) return state.to_child( append_message={"msg": expand_msg, "kwargs": msg_kwargs}, diff --git a/sqlwhat/checks/has_funcs.py b/sqlwhat/checks/has_funcs.py index 31611c1..c0c5d54 100644 --- a/sqlwhat/checks/has_funcs.py +++ b/sqlwhat/checks/has_funcs.py @@ -63,10 +63,7 @@ def has_nrows( n_sol = len(next(iter(state.solution_result.values()))) if n_stu != n_sol: - _msg = state.build_message( - incorrect_msg, fmt_kwargs={"n_stu": n_stu, "n_sol": n_sol} - ) - state.report(_msg) + state.report(incorrect_msg, {"n_stu": n_stu, "n_sol": n_sol}) return state @@ -109,10 +106,7 @@ def has_ncols( n_sol = len(state.solution_result) if n_stu != n_sol: - _msg = state.build_message( - incorrect_msg, fmt_kwargs={"n_stu": n_stu, "n_sol": n_sol} - ) - state.report(_msg) + state.report(incorrect_msg, {"n_stu": n_stu, "n_sol": n_sol}) return state @@ -182,10 +176,7 @@ def has_equal_value(state, ordered=False, ndigits=None, incorrect_msg=None): pass if sol_col_vals != stu_col_vals: - _msg = state.build_message( - incorrect_msg, fmt_kwargs={"col": sol_col_name, "ordered": ordered} - ) - state.report(_msg) + state.report(incorrect_msg, {"col": sol_col_name, "ordered": ordered}) return state From 36b2791391fc539793d02292ef4954cf9dcd022f Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Thu, 2 Jan 2020 23:50:53 +0100 Subject: [PATCH 07/12] Update test_exercise --- sqlwhat/test_exercise.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sqlwhat/test_exercise.py b/sqlwhat/test_exercise.py index 8c8fcdc..ab1fadf 100644 --- a/sqlwhat/test_exercise.py +++ b/sqlwhat/test_exercise.py @@ -1,5 +1,5 @@ from sqlwhat.State import State -from protowhat.failure import TestFail +from protowhat.failure import TestFail, InstructorError from protowhat.Reporter import Reporter from sqlwhat.sct_syntax import SCT_CTX @@ -21,6 +21,8 @@ def test_exercise( """ """ + reporter = Reporter(errors=error) + state = State( student_code=student_code, solution_code=solution_code, @@ -29,7 +31,7 @@ def test_exercise( solution_conn=solution_conn, student_result=student_result, solution_result=solution_result, - reporter=Reporter(errors=error), + reporter=reporter, force_diagnose=force_diagnose, ) @@ -37,7 +39,10 @@ def test_exercise( try: exec(sct, SCT_CTX) - except TestFail as tf: - return tf.payload + except (TestFail, InstructorError) as e: + if isinstance(e, InstructorError): + # TODO: decide based on context + raise e + return reporter.build_failed_payload(e.feedback) - return state.reporter.build_final_payload() + return reporter.build_final_payload() From 83badcf298761c3bcf0a788e5f95083700ef4b49 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Fri, 3 Jan 2020 18:44:43 +0100 Subject: [PATCH 08/12] Update highlight tests --- tests/test_line_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_line_info.py b/tests/test_line_info.py index 599e413..18180f4 100644 --- a/tests/test_line_info.py +++ b/tests/test_line_info.py @@ -36,4 +36,4 @@ def test_line_info(sql_cmd, start, pos): state.report("failure message") except TF as tf: for ii, k in enumerate(pos_names): - assert tf.payload[k] == pos[ii] + assert tf.feedback.get_highlight()[k] == pos[ii] From d255f50217a7edac2e7f912e732acd75b0c4fb5e Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Fri, 3 Jan 2020 18:45:14 +0100 Subject: [PATCH 09/12] Use FeedbackComponent --- sqlwhat/checks/check_funcs.py | 10 +++++----- tests/test_protowhat_messaging.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sqlwhat/checks/check_funcs.py b/sqlwhat/checks/check_funcs.py index 324f5a4..8bcb8f0 100644 --- a/sqlwhat/checks/check_funcs.py +++ b/sqlwhat/checks/check_funcs.py @@ -1,4 +1,4 @@ -from protowhat.Feedback import Feedback +from protowhat.Feedback import Feedback, FeedbackComponent from protowhat.failure import debugger from protowhat.sct_syntax import link_to_state from protowhat.checks.check_simple import allow_errors @@ -76,7 +76,7 @@ def check_row(state, index, missing_msg=None, expand_msg=None): state.report(missing_msg, msg_kwargs) return state.to_child( - append_message={"msg": expand_msg, "kwargs": msg_kwargs}, + append_message=FeedbackComponent(expand_msg, msg_kwargs), student_result={k: [v[index]] for k, v in stu_res.items()}, solution_result={k: [v[index]] for k, v in sol_res.items()}, ) @@ -133,7 +133,7 @@ def check_column(state, name, missing_msg=None, expand_msg=None): state.report(missing_msg, msg_kwargs) return state.to_child( - append_message={"msg": expand_msg, "kwargs": msg_kwargs}, + append_message=FeedbackComponent(expand_msg, msg_kwargs), student_result={name: stu_res[name]}, solution_result={name: sol_res[name]}, ) @@ -204,7 +204,7 @@ def check_all_columns(state, allow_extra=True, too_many_cols_msg=None, expand_ms ) return state.to_child( - append_message={"msg": expand_msg, "kwargs": {}}, + append_message=FeedbackComponent(expand_msg), student_result=child_stu_result, solution_result=child_sol_result, ) @@ -316,7 +316,7 @@ def check_query(state, query, error_msg=None, expand_msg=None): state.report(error_msg, msg_kwargs) return state.to_child( - append_message={"msg": expand_msg, "kwargs": msg_kwargs}, + append_message=FeedbackComponent(expand_msg, msg_kwargs), student_result=stu_res, solution_result=sol_res, ) diff --git a/tests/test_protowhat_messaging.py b/tests/test_protowhat_messaging.py index 9a06904..564ba84 100644 --- a/tests/test_protowhat_messaging.py +++ b/tests/test_protowhat_messaging.py @@ -148,7 +148,7 @@ def test_get_ast_path(query, path, target_desc): [check_edge, ["left"]], [has_equal_ast, []], ], - "Check the Script. Could not find the first `SELECT` statement.", + "Could not find the first `SELECT` statement.", ), ( "SELECT a FROM c", From 9e140e5289105fd5e816d7ec380f0da62b0c4bc3 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Tue, 7 Jan 2020 14:10:41 +0100 Subject: [PATCH 10/12] Fix pytest error --- tests/test_selectors.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_selectors.py b/tests/test_selectors.py index 1a09007..3885f06 100644 --- a/tests/test_selectors.py +++ b/tests/test_selectors.py @@ -12,8 +12,8 @@ def ast(): @pytest.fixture -def dispatcher(): - return Dispatcher.from_module(ast()) +def dispatcher(ast): + return Dispatcher.from_module(ast) def test_selector_standalone(): From 1b149f95cd5a9a7040d4c9496d67fe9ca2430d89 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Tue, 14 Jan 2020 10:53:40 +0100 Subject: [PATCH 11/12] Use sct_context --- sqlwhat/sct_context.py | 9 +++++++++ sqlwhat/sct_syntax.py | 15 -------------- sqlwhat/test_exercise.py | 43 +++++++++++++++++++++++----------------- tests/test_state.py | 2 +- 4 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 sqlwhat/sct_context.py delete mode 100644 sqlwhat/sct_syntax.py diff --git a/sqlwhat/sct_context.py b/sqlwhat/sct_context.py new file mode 100644 index 0000000..7752611 --- /dev/null +++ b/sqlwhat/sct_context.py @@ -0,0 +1,9 @@ +from protowhat.sct_context import create_sct_context, get_checks_dict +from sqlwhat import checks + +SCT_DICT = get_checks_dict(checks) +SCT_CTX = create_sct_context(SCT_DICT) + +# put on module for easy * importing +globals().update(SCT_CTX) +__all__ = list(SCT_CTX.keys()) diff --git a/sqlwhat/sct_syntax.py b/sqlwhat/sct_syntax.py deleted file mode 100644 index be1ecf2..0000000 --- a/sqlwhat/sct_syntax.py +++ /dev/null @@ -1,15 +0,0 @@ -# Wrap SCT checks ------------------------------------------------------------- - -from sqlwhat.State import State -from sqlwhat import checks -from protowhat.sct_syntax import get_checks_dict, create_sct_context - -# used by chains, to know what methods are available -sct_dict = get_checks_dict(checks) -SCT_CTX = create_sct_context(sct_dict) - -# used in test_exercise, so that scts without Ex() don't run immediately -globals().update(SCT_CTX) - -# put on module for easy importing -__all__ = list(SCT_CTX.keys()) diff --git a/sqlwhat/test_exercise.py b/sqlwhat/test_exercise.py index ab1fadf..a40bd8f 100644 --- a/sqlwhat/test_exercise.py +++ b/sqlwhat/test_exercise.py @@ -1,7 +1,9 @@ -from sqlwhat.State import State -from protowhat.failure import TestFail, InstructorError +from protowhat.failure import Failure, InstructorError from protowhat.Reporter import Reporter -from sqlwhat.sct_syntax import SCT_CTX +from protowhat.sct_context import create_sct_context, get_checks_dict + +from sqlwhat import checks +from sqlwhat.State import State def test_exercise( @@ -23,23 +25,28 @@ def test_exercise( reporter = Reporter(errors=error) - state = State( - student_code=student_code, - solution_code=solution_code, - pre_exercise_code=pre_exercise_code, - student_conn=student_conn, - solution_conn=solution_conn, - student_result=student_result, - solution_result=solution_result, - reporter=reporter, - force_diagnose=force_diagnose, - ) + try: + state = State( + student_code=student_code, + solution_code=solution_code, + pre_exercise_code=pre_exercise_code, + student_conn=student_conn, + solution_conn=solution_conn, + student_result=student_result, + solution_result=solution_result, + reporter=reporter, + force_diagnose=force_diagnose, + ) - SCT_CTX["Ex"].root_state = state + # the available SCT methods + sct_dict = get_checks_dict(checks) - try: - exec(sct, SCT_CTX) - except (TestFail, InstructorError) as e: + # the available global variables + sct_context = create_sct_context(sct_dict, state) + + exec(sct, sct_context) + + except Failure as e: if isinstance(e, InstructorError): # TODO: decide based on context raise e diff --git a/tests/test_state.py b/tests/test_state.py index 755a80d..ee16fe3 100644 --- a/tests/test_state.py +++ b/tests/test_state.py @@ -1,6 +1,6 @@ from unittest.mock import MagicMock -from sqlwhat.sct_syntax import Ex +from sqlwhat.sct_context import Ex from sqlwhat.State import State from protowhat.Reporter import Reporter from protowhat.failure import TestFail as TF From 88ac7e1ea891de859344aed04492240a1239f7f4 Mon Sep 17 00:00:00 2001 From: Jeroen Hermans Date: Wed, 29 Jan 2020 01:24:29 +0100 Subject: [PATCH 12/12] Update protowhat --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 3378078..8be4ff7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # pkg deps -protowhat~=1.13.0 +protowhat~=2.0.1 antlr-plsql~=0.9.1 antlr-tsql~=0.12.6