def test_recurrence_weichert_algorithm(self):
        context = create_context('config_recurrence_weichert.yml')

        workflow = create_workflow(context.config)

        run(workflow, context)

        sm = context.sm_definitions[0]

        self.assertAlmostEqual(
            sm.rupture_rate_model.truncated_gutenberg_richter.b_value,
            0.569790,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.recurrence_sigb,
            0.041210,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.rupture_rate_model.truncated_gutenberg_richter.a_value,
            132.051268,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.recurrence_siga_m,
            7.701386,
            self.decimal_places)
    def test_recurrence_mle_algorithm(self):
        context = create_context('config_recurrence_mle.yml')

        workflow = create_workflow(context.config)

        run(workflow, context)

        sm = context.sm_definitions[0]

        self.assertAlmostEqual(
            sm.rupture_rate_model.truncated_gutenberg_richter.b_value,
            0.595256,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.recurrence_sigb,
            0.024816,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.rupture_rate_model.truncated_gutenberg_richter.a_value,
            158.109066,
            self.decimal_places)

        self.assertAlmostEqual(
            sm.recurrence_siga_m,
            49.997286,
            self.decimal_places)
def test_check_function_twice(first_ok, second_ok, second_solves_first,
                              first_solves_second):
    patt = (
        'Ex().check_function("round", index={}).check_args("number").has_equal_value()'
    )
    sct = patt.format(0) + "\n" + patt.format(1)
    first_ok["DC_SCT"] = sct
    output = helper.run(first_ok)
    assert not output["correct"]
    helper.with_line_info(output, 2, 2, 7, 7)

    second_ok["DC_SCT"] = sct
    output = helper.run(second_ok)
    assert not output["correct"]
    helper.with_line_info(output, 1, 1, 7, 7)

    second_solves_first["DC_SCT"] = sct
    output = helper.run(second_solves_first)
    assert not output["correct"]
    helper.with_line_info(output, 1, 1, 7, 7)

    first_solves_second["DC_SCT"] = sct
    output = helper.run(first_solves_second)
    assert not output["correct"]
    helper.with_line_info(output, 1, 1, 7, 7)

    first_ok["DC_SCT"] = patt.format(1)
    output = helper.run(first_ok)
    assert not output["correct"]
    helper.with_line_info(output, 2, 2, 7, 7)  ## THIS IS INTUITIVE!
Exemple #4
0
def test_check_function_weirdness(sct, sol):
    data = {
        "DC_CODE": "round(1.23, ndigits = 1)",
        "DC_SOLUTION": sol,
        "DC_SCT": sct
    }
    with pytest.raises(InstructorError):
        helper.run(data)
def test_converter_err():
    code = "import numpy as np; x = np.array([1, 2, 3])"
    data = {
        "DC_CODE": code,
        "DC_SOLUTION": code,
        "DC_SCT": """def convert(): return abc\nset_converter('numpy.ndarray', convert); test_object('x') """,
    }
    with pytest.raises(InstructorError):
        helper.run(data)
Exemple #6
0
def test_with_env_old_fail(data, sct, should_err):
    data["DC_SCT"] = sct
    with helper.set_v2_only_env("1"):
        relooooad()
        if should_err:
            with pytest.raises((NameError, AttributeError)):
                sct_payload = helper.run(data)
        else:
            sct_payload = helper.run(data)
            assert not sct_payload["correct"]
Exemple #7
0
    def run_config(self, wprep, wnoprep):
        for alg in EquivalentWorkflowsTestCase.recurrence_alg:
            self.config_prep['Recurrence']['recurrence_algorithm'] = alg
            self.config_noprep['Recurrence']['recurrence_algorithm'] = alg
            self.context_prep.config = self.config_prep
            self.context_noprep.config = self.config_noprep
            run(wprep, self.context_prep)
            run(wnoprep, self.context_noprep)

            self.evaluate_values(self.context_prep.sm_definitions,
                self.context_noprep.sm_definitions)
    def test_gardner_knopoff(self):
        context = create_context('config_gardner_knopoff.yml')
        workflow = create_workflow(context.config)
        run(workflow, context)

        expected_vcl = np.array(
            [0, 0, 0, 2, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 3, 3])

        self.assertTrue(
            np.array_equal(self.expected_vmain_shock, context.working_catalog))
        self.assertTrue(np.array_equal(expected_vcl, context.vcl))
        self.assertTrue(
            np.array_equal(self.expected_flag_vector, context.flag_vector))
    def test_gardner_knopoff(self):
        context = create_context('config_gardner_knopoff.yml')
        workflow = create_workflow(context.config)
        run(workflow, context)

        expected_vcl = np.array([0, 0, 0, 2, 2, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
            0, 0, 0, 3, 3])

        self.assertTrue(np.array_equal(self.expected_vmain_shock,
                context.working_catalog))
        self.assertTrue(np.array_equal(expected_vcl, context.vcl))
        self.assertTrue(np.array_equal(self.expected_flag_vector,
                context.flag_vector))
Exemple #10
0
def test_has_chosen(stu_code, passes, mess):
    data = {"DC_CODE": stu_code, "DC_SCT": "test_mc(2, ['a', 'b', 'c'])"}
    res = helper.run(data)
    assert res["correct"] is passes
    assert res["message"] == mess

    data = {
        "DC_CODE": stu_code,
        "DC_SCT": "Ex().has_chosen(2, ['a', 'b', 'c'])"
    }
    res = helper.run(data)
    assert res["correct"] is passes
    assert res["message"] == mess
Exemple #11
0
def test_ex_check_query_wrong_usage(pec, query, patt):
    with pytest.raises(
        Exception,
        match=r"Solution failed: Running `%s` after your submission generated an error\."
        % patt,
    ):
        helper.run(
            {
                "DC_PEC": pec,
                "DC_SCT": "Ex().check_query(query = '%s')" % query,
                "DC_CODE": "",
                "DC_SOLUTION": "",
            }
        )
    def test_maximum_magnitude_kijko_npg(self):
        context = create_context('config_maxmag_kijko_npg.yml')

        workflow = create_workflow(context.config)

        run(workflow, context)

        sm = context.sm_definitions[0]

        self.assertAlmostEqual(6.44392,
            sm.rupture_rate_model.truncated_gutenberg_richter.max_magnitude,
            self.decimal_places)

        self.assertAlmostEqual(0.10922, sm.max_mag_sigma, self.decimal_places)
    def test_maximum_magnitude_cum_moment(self):
        # It depends on a random seed so precision is relaxed
        self.decimal_places = 1
        context = create_context('config_maxmag_cum_moment.yml')

        workflow = create_workflow(context.config)

        run(workflow, context)

        sm = context.sm_definitions[0]

        self.assertAlmostEqual(6.6,
            sm.rupture_rate_model.truncated_gutenberg_richter.max_magnitude,
            self.decimal_places)

        self.assertAlmostEqual(0.05, sm.max_mag_sigma, self.decimal_places)
Exemple #14
0
def test_with_highlighting(data):
    data[
        "DC_SCT"
    ] = 'Ex().check_function("round").check_args("number").has_equal_value()'
    output = helper.run(data)
    assert not output["correct"]
    helper.with_line_info(output, 1, 1, 7, 11)
def test_while_loop(sct, stu, passes):
    res = helper.run({
        "DC_CODE": stu,
        "DC_SOLUTION": "while 3 > 4: print(2)",
        "DC_SCT": sct
    })
    assert res["correct"] == passes
def test_several_assignments_2(diff_assign_data):
    res = helper.run({
        **diff_assign_data, "DC_SCT":
        "Ex().check_object('df2').has_equal_value()"
    })
    assert not res["correct"]
    helper.no_line_info(res)
def test_check_object_exotic_compare(stu_code, passes):
    output = helper.run({
        "DC_SOLUTION": "x = filter(lambda x: x > 0, [1, 1])",
        "DC_SCT": "Ex().check_object('x').has_equal_value()",
        "DC_CODE": stu_code,
    })
    assert output["correct"] == passes
Exemple #18
0
def test_check_function_equal_part_len(sct, stu, passes):
    res = helper.run({
        "DC_CODE": stu,
        "DC_SOLUTION": "def f(a, b): pass",
        "DC_SCT": sct
    })
    assert res["correct"] == passes
Exemple #19
0
def do_override_test(code,
                     base_check,
                     parts,
                     override=None,
                     part_name=None,
                     part_index="",
                     passes=True):
    """High level function used to generate tests"""
    if part_name:
        if not override:
            override = parts[part_name]
        sct = base_check + '.check_{}({}).override("""{}""").has_equal_ast()'.format(
            part_name, part_index, override)
    else:
        # whole code (e.g. if expression, or for loop)
        if not override:
            override = code.format(**parts)
        sct = base_check + '.override("""{}""").has_equal_ast()'.format(
            override)

    data = {
        "DC_SOLUTION": code.format(**parts),
        "DC_CODE": code.format(**parts),
        "DC_SCT": sct,
    }
    # import pdb; pdb.set_trace()
    sct_payload = helper.run(data)
    assert sct_payload["correct"] == passes
def test_for_loop(sct, stu, passes):
    res = helper.run({
        "DC_CODE": stu,
        "DC_SOLUTION": "for i in range(3): print(i)",
        "DC_SCT": sct
    })
    assert res["correct"] == passes
Exemple #21
0
def test_has_import(stu, patt):
    output = helper.run({
        "DC_CODE": stu,
        "DC_SOLUTION": "import pandas as pd",
        "DC_SCT": "Ex().has_import('pandas', same_as=True)",
    })
    assert not output["correct"]
    assert message(output, patt)
Exemple #22
0
def test_check_function_pkg2(stu, patt):
    output = helper.run({
        "DC_SOLUTION": "import numpy as np; x = np.random.rand(1)",
        "DC_CODE": stu,
        "DC_SCT": "test_function_v2('numpy.random.rand')",
    })
    assert not output["correct"]
    assert message(output, patt)
def test_check_object_exotic_compare2(stu_code, passes):
    output = helper.run({
        "DC_PEC": "import numpy as np",
        "DC_SOLUTION": "x = (np.array([1, 2]), np.array([3, 4]))",
        "DC_SCT": "Ex().check_object('x').has_equal_value()",
        "DC_CODE": stu_code,
    })
    assert output["correct"] == passes
Exemple #24
0
def test_check_function_pkg1(stu, patt):
    output = helper.run({
        "DC_SOLUTION": "import pandas as pd; pd.DataFrame({'a': [1, 2, 3]})",
        "DC_CODE": stu,
        "DC_SCT": "test_function_v2('pandas.DataFrame')",
    })
    assert not output["correct"]
    assert message(output, patt)
def test_list_iter_vars(sct, stu, passes):
    res = helper.run({
        "DC_PEC": "x = {'a':1, 'b':2}",
        "DC_SOLUTION": "[key for key, value in x.items()]",
        "DC_CODE": stu,
        "DC_SCT": sct,
    })
    res["correct"] == passes
Exemple #26
0
def test_test_with_has_context(sct, stu, passes):
    res = helper.run({
        "DC_PEC": "from io import StringIO",
        "DC_SOLUTION": "with StringIO() as f1, StringIO() as f2: pass",
        "DC_CODE": stu,
        "DC_SCT": sct,
    })
    assert res["correct"] == passes
Exemple #27
0
def test_has_expr(sct, patt):
    output = helper.run({
        "DC_SOLUTION": "x = [1]",
        "DC_CODE": "x = [0]",
        "DC_SCT": sct
    })
    assert not output["correct"]
    assert message(output, patt)
def test_check_if_else_basic(sct, stu, passes):
    res = helper.run({
        "DC_PEC": "offset = 8",
        "DC_SOLUTION": "if offset > 8: x = 5\nelse: x = round(2.123)",
        "DC_CODE": stu,
        "DC_SCT": sct,
    })
    assert res["correct"] == passes
Exemple #29
0
def test_fail_01(data):
    data["DC_CODE"] = ""
    sct_payload = helper.run(data)
    assert not sct_payload["correct"]
    assert (
        sct_payload["message"] ==
        "The system wants to check the first try statement but hasn't found it."
    )
Exemple #30
0
def test_normal_fail():
    data = {
        "DC_PEC": "#no pec",
        "DC_CODE": "x = 4",
        "DC_SOLUTION": "x = 6",
        "DC_SCT": 'test_object("x")\nsuccess_msg("nice")',
    }
    output = helper.run(data)
    assert not output["correct"]
Exemple #31
0
def test_check_object(stu, patt, cols, cole):
    output = helper.run({
        "DC_CODE": stu,
        "DC_SOLUTION": "x = 5",
        "DC_SCT": 'Ex().check_object("x").has_equal_value()',
    })
    assert not output["correct"]
    assert message(output, patt)
    assert lines(output, cols, cole)
    def test_stepp(self):
        context = create_context('config_stepp.yml')

        workflow = create_workflow(context.config)

        run(workflow, context)

        filtered_eq_events = np.array([
                    [1994., 4.0], [1994., 4.1], [1994., 4.2],
                    [1994., 4.3], [1994., 4.4], [1964., 4.5],
                    [1964., 4.6], [1964., 4.7], [1964., 4.8],
                    [1964., 4.9], [1964., 5.0], [1964., 5.1],
                    [1964., 5.2], [1964., 5.3], [1964., 5.4],
                    [1919., 5.5], [1919., 5.6], [1919., 5.7],
                    [1919., 5.8], [1919., 5.9], [1919., 6.0],
                    [1919., 6.1], [1919., 6.2], [1919., 6.3],
                    [1919., 6.4], [1919., 6.5], [1919., 6.6],
                    [1919., 6.7], [1919., 6.8], [1919., 6.9],
                    [1919., 7.0], [1919., 7.1], [1919., 7.2],
                    [1919., 7.3]])

        self.assertTrue(np.allclose(filtered_eq_events,
                context.completeness_table))