Skip to content

Commit f4e7515

Browse files
committed
Add test to cover the end-to-end flow, fixed laike9m#48
1 parent e09fcc2 commit f4e7515

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

tests/fixture.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
I can't get fixture to work well with parameterized test, so just create some constants here
3+
"""
4+
5+
from pathlib import Path
6+
7+
QUESTION = "question.py"
8+
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
9+
ALL_QUESTIONS = list(CHALLENGES_DIR.glob("**/question.py"))
10+
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))

tests/test_api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
from werkzeug.test import TestResponse
5+
from app import app
6+
from .fixture import ALL_QUESTIONS
7+
8+
9+
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
10+
def test_get_challenge(question_file: Path):
11+
level, challenge_name = question_file.parent.name.split("-", maxsplit=1)
12+
response = app.test_client().get(f"/{level}/{challenge_name}")
13+
14+
# Verify the returned HTML contains a challenge's source code
15+
assert "TODO" in response.text
16+
17+
18+
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
19+
def test_run_challenge(question_file: Path):
20+
level, challenge_name = question_file.parent.name.split("-", maxsplit=1)
21+
response = app.test_client().post(f"run/{level}/{challenge_name}", data="")
22+
23+
# Verify backend can run type check and return results
24+
assert response.json is not None

tests/test_identical.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import pytest
88

99
from views.challenge import Challenge, Level
10-
11-
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
12-
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))
13-
QUESTION = "question.py"
10+
from .fixture import ALL_SOLUTIONS, QUESTION
1411

1512

1613
@pytest.mark.parametrize("solution_file", ALL_SOLUTIONS, ids=lambda x: x.parent.name)

tests/test_questions.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77
import pytest
88

99
from views.challenge import ChallengeManager
10-
11-
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
12-
ALL_QUESTIONS = list(CHALLENGES_DIR.glob("**/question.py"))
10+
from .fixture import ALL_QUESTIONS
1311

1412

1513
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
1614
def test_solution_valid(question_file: Path):
17-
test_id = question_file.parent.name
18-
19-
# TODO: remove "advanced-generic-param" once fixed
2015
# Skip the challenges whose question can't fail type check no matter how many tests are added.
21-
if test_id in ("basic-any"):
16+
if question_file.parent.name == "basic-any":
2217
pytest.skip(f"Skipping test: {question_file}")
2318

2419
with question_file.open() as f:

tests/test_solutions.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import pytest
88

99
from views.challenge import ChallengeManager
10-
11-
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
12-
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))
10+
from .fixture import ALL_SOLUTIONS
1311

1412

1513
@pytest.mark.parametrize("solution_file", ALL_SOLUTIONS, ids=lambda x: x.parent.name)

views/challenge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ def run_challenge(self, key: ChallengeKey, user_code: str) -> TypeCheckResult:
7272
def _load_challenges() -> dict[ChallengeKey, Challenge]:
7373
challenges = {}
7474
for filename in glob.glob(f"{ROOT_DIR}/challenges/*/question.py"):
75-
dir_name = os.path.basename(os.path.dirname(filename))
76-
level, challenge_name = dir_name.split("-", maxsplit=1)
75+
challenge_folder_name = os.path.basename(os.path.dirname(filename))
76+
level, challenge_name = challenge_folder_name.split("-", maxsplit=1)
7777
with open(filename, "r") as file:
7878
code = file.read()
7979
challenges[ChallengeKey(Level(level), challenge_name)] = Challenge(

0 commit comments

Comments
 (0)