def test_check_result(stu, success): state = prepare_state({"a": [1, 2], "b": [3, 4]}, stu) if success: passes(check_result(state)) else: with pytest.raises(TF): check_result(state)
def test_args_kwargs_check_function_passing(argspec): code = "my_fun(1, 2, 3, 4, c = 5)" s = setup_state(pec="def my_fun(a, b, *args, **kwargs): pass", stu_code=code, sol_code=code) x = s.check_function("my_fun") helper.passes(x.check_args(argspec).has_equal_value())
def test_check_all_columns_stricter(stu, success): state = prepare_state({"a": [1], "b": [2]}, stu) if success: passes(check_all_columns(state, allow_extra=False)) else: with pytest.raises(TF): check_all_columns(state, allow_extra=False)
def check_function_sig_false(): code = "f(color = 'blue')" s = setup_state(pec="def f(*args, **kwargs): pass", sol_code=code, stu_code=code) helper.passes( s.check_function("f", 0, signature=False).check_args("color").has_equal_ast())
def test_check_column(stu, stu_sub, success): state = prepare_state({"a": [1]}, stu) if success: x = check_column(state, "a") passes(x) assert x.solution_result == {"a": [1]} assert x.student_result == stu_sub else: with pytest.raises(TF): check_column(state, "a")
def test_check_row(stu, stu_sub, success): state = prepare_state({"a": [1, 2, 3], "b": [4, 5, 6]}, stu) if success: x = check_row(state, 1) passes(x) assert x.solution_result == {"a": [2], "b": [5]} assert x.student_result == stu_sub else: with pytest.raises(TF): check_row(state, 1)
def test_method_2(): code = "print('a')" s = setup_state( sol_code=code, stu_code=code, pec="", ) helper.passes(s.check_function("print", signature=False)) from pythonwhat.signatures import sig_from_obj helper.passes(s.check_function("print", signature=sig_from_obj('print')))
def test_lower_case(): state = prepare_state({"a": [1]}, {"A": [1]}) # fails if not using lowercase with pytest.raises(TF): check_column(state, "a") # passes if lowercase is being used child = lowercase(state) child2 = check_column(child, "a") passes(child2) passes(has_equal_value(child2))
def test_method_2(): code = "df[df.b == 'x'].a.sum()" s = setup_state( sol_code=code, stu_code=code, pec="import pandas as pd; df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'x', 'y']})", ) helper.passes(s.check_function("df.a.sum", signature=False)) from pythonwhat.signatures import sig_from_obj import pandas as pd helper.passes(s.check_function("df.a.sum", signature=sig_from_obj(pd.Series.sum)))
def check_function_multiple_times(): from pythonwhat.local import setup_state s = setup_state(sol_code="print('test')", stu_code="print('test')") helper.passes(s.check_function("print")) helper.passes(s.check_function("print").check_args(0)) helper.passes(s.check_function("print").check_args("value"))
def test_method_1(): code = "df.groupby('b').sum()" s = setup_state( sol_code=code, stu_code=code, pec="import pandas as pd; df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'x', 'y']})", ) helper.passes(s.check_function("df.groupby").check_args(0).has_equal_value()) helper.passes(s.check_function("df.groupby.sum", signature=False)) from pythonwhat.signatures import sig_from_obj import pandas as pd helper.passes( s.check_function("df.groupby.sum", signature=sig_from_obj(pd.Series.sum)) )
def test_method_1(): code = "df.groupby('b').sum()" s = setup_state( sol_code=code, stu_code=code, pec= "import pandas as pd; df = pd.DataFrame({'a': [1, 2, 3], 'b': ['x', 'x', 'y']})", ) helper.passes( s.check_function("df.groupby").check_args(0).has_equal_value()) helper.passes(s.check_function("df.groupby.sum", signature=False)) import inspect from inspect import Parameter as param manual_sig = inspect.Signature([ param("x", param.POSITIONAL_OR_KEYWORD, default=None), param("axis", param.POSITIONAL_OR_KEYWORD, default=None) ]) helper.passes(s.check_function("df.groupby.sum", signature=manual_sig))
def test_has_expr_override_pass_2(): stu = "x = [1, 2, 3]" sol = "x = [1, 2, 5]" s = setup_state(stu_code=stu, sol_code=sol) helper.passes(s.check_object("x").has_equal_value(override=[1, 2, 3]))
def test_has_printout_multiple(stu): sol = 'print("randomness")\nprint(1, 2, 3)' s = setup_state(stu_code=stu, sol_code=sol) helper.passes(s.has_printout(1))
def test_check_object_single_process(): state1pid = setup_state("x = 3", "", pid=1) helper.passes(state1pid.check_object("x"))
def test_check_function_with_has_equal_value(): code = "import numpy as np\narr = np.array([1, 2, 3, 4, 5])\nnp.mean(arr)" s = setup_state(stu_code=code, sol_code=code) helper.passes(s.check_function("numpy.mean").has_equal_value())