Beispiel #1
0
    def test_steps(self):
        data = api("load_data")
        state = data["state"]
        transcript = []
        for page_index, page in enumerate(pages.values()):
            for step_index, step_name in enumerate(page.step_names[:-1]):
                assert page_index == state["page_index"]
                assert step_index == state["step_index"]
                step = getattr(page, step_name)

                for substep in [*step.messages, step]:
                    program = substep.program
                    if "\n" in program:
                        if step == UnderstandingProgramsWithSnoop.print_tail_snoop:
                            code_source = "snoop"
                        elif step == UnderstandingProgramsWithPythonTutor.run_with_python_tutor:
                            code_source = "pythontutor"
                        else:
                            code_source = "editor"
                    else:
                        code_source = "shell"
                    response = api("run_code",
                                   code=program,
                                   source=code_source)
                    state = response["state"]
                    del state["pages_progress"]
                    for line in response["result"]:
                        line["text"] = normalise_output(line["text"])
                    transcript.append(
                        dict(
                            program=program.splitlines(),
                            page=page.title,
                            step=step_name,
                            response=response,
                        ))
                    is_message = substep in step.messages
                    if is_message:
                        self.assertEqual(response["message"], substep.text)

                    self.assertEqual(step_index + (not is_message),
                                     state["step_index"], transcript[-1])

            if page_index < len(pages) - 1:
                state = api("set_page", index=page_index + 1)
        path = Path(__file__).parent / "test_transcript.json"
        if os.environ.get("FIX_TESTS", 0):
            dump = json.dumps(transcript, indent=4, sort_keys=True)
            path.write_text(dump)
        else:
            self.assertEqual(transcript, json.loads(path.read_text()))
Beispiel #2
0
    def load_data(self):
        user = self.user
        if user.is_anonymous:
            return {}

        return dict(
            pages=[
                select_attrs(page, "title step_texts")
                for page in pages.values()
            ],
            state=self.current_state(),
            user=dict(
                email=user.email,
                developerMode=user.developer_mode,
            ),
        )
Beispiel #3
0
    def load_data(self):
        user = self.user
        if user.is_anonymous:
            return {}

        return dict(
            pages=[
                dict(**select_attrs(page, "slug title index"), steps=page.step_dicts)
                for page in pages.values()
            ],
            state=self.current_state(),
            user=dict(
                email=user.email,
                developerMode=user.developer_mode,
            ),
            page_index=pages[self.user.page_slug].index,
        )
Beispiel #4
0
def test_steps(api):
    transcript = []
    for page_index, page in enumerate(pages.values()):
        for step_index, step_name in enumerate(page.step_names[:-1]):
            step = getattr(page, step_name)

            for substep in [*step.messages, step]:
                program = substep.program
                if "\n" in program:
                    code_source = step.expected_code_source or "editor"
                else:
                    code_source = "shell"
                response = api(
                    "run_code",
                    code=program,
                    source=code_source,
                    page_index=page_index,
                    step_index=step_index,
                )

                assert "state" in response
                state = response.pop("state")
                for line in response["result"]:
                    line["text"] = normalise_output(line["text"])
                del response["birdseye_url"]
                if not response["prediction"]["choices"]:
                    del response["prediction"]

                transcript_item = dict(
                    program=program.splitlines(),
                    page=page.title,
                    step=step_name,
                    response=response,
                )
                transcript.append(transcript_item)
                is_message = substep in step.messages

                if is_message:
                    response["message"] = only(response.pop("messages"))
                    assert response["message"] == substep.text
                else:
                    assert response.pop("messages") == []
                    response["message"] = ""

                    if step.get_solution:
                        get_solution = "".join(step.get_solution["tokens"])
                        assert "def solution(" not in get_solution
                        assert "returns_stdout" not in get_solution
                        assert get_solution.strip() in program
                        transcript_item[
                            "get_solution"] = get_solution.splitlines()
                        if step.parsons_solution:
                            is_function = transcript_item["get_solution"][
                                0].startswith("def ")
                            assert len(
                                step.get_solution["lines"]) >= 4 + is_function

                assert response["passed"] == (not is_message)
                assert step_index + response["passed"] == state[
                    "pages_progress"][page_index]

    path = Path(__file__).parent / "test_transcript.json"
    if os.environ.get("FIX_TESTS", 0):
        dump = json.dumps(transcript, indent=4, sort_keys=True)
        path.write_text(dump)
    else:
        assert transcript == json.loads(path.read_text())