diff --git a/app/main.py b/app/main.py index 147ba52..c58b8d0 100644 --- a/app/main.py +++ b/app/main.py @@ -192,7 +192,14 @@ def submit_lesson(lesson_id: str, submission: Submission) -> dict: question_ids = {question["id"] for question in lesson["questions"]} results, correct_count = grade_answers(submission.answers, question_ids) total = len(question_ids) - passed = correct_count >= 2 + code_question_ids = [ + question["id"] for question in lesson["questions"] if question["kind"] == "code" + ] + code_correct = bool(code_question_ids) and all( + any(r["question_id"] == question_id and r["correct"] for r in results) + for question_id in code_question_ids + ) + passed = (not code_question_ids or code_correct) and correct_count * 3 >= total * 2 gained = 0 if passed: gained, _ = save_lesson(lesson_id, correct_count, total, lesson["xp"]) @@ -200,11 +207,16 @@ def submit_lesson(lesson_id: str, submission: Submission) -> dict: "passed": passed, "correct_count": correct_count, "total_count": total, + "code_correct": code_correct, "xp_gained": gained, "results": results, "message": "Урок пройден! Новый урок уже открыт." if passed - else "Нужно минимум 2 верных ответа из 3. Попробуй ещё раз — это нормально.", + else ( + "Для зачёта нужно решить задачу на код" + if correct_count * 3 >= total * 2 and not code_correct + else "Нужно минимум 2 верных ответа из 3. Попробуй ещё раз — это нормально." + ), } diff --git a/tests/test_api.py b/tests/test_api.py index 7595a19..9f649a9 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -57,3 +57,34 @@ def test_extended_course_unlocks_after_foundation() -> None: assert code_check.status_code == 200 assert code_check.json()["correct"] is True client.post("/api/reset") + + +def test_lesson_requires_correct_code_for_completion() -> None: + with TestClient(app) as client: + client.post("/api/reset") + two_without_code = [ + {"question_id": "hello-print", "answer": "print('Привет')"}, + {"question_id": "hello-string", "answer": "строка"}, + {"question_id": "hello-code", "answer": ""}, + ] + response = client.post("/api/lessons/hello/submit", json={"answers": two_without_code}) + assert response.json()["passed"] is False + assert response.json()["message"] == "Для зачёта нужно решить задачу на код" + + code_and_one_other = [ + {"question_id": "hello-print", "answer": "print('неверно')"}, + {"question_id": "hello-string", "answer": "строка"}, + {"question_id": "hello-code", "answer": "print('Я начинаю путь в Python!')"}, + ] + response = client.post("/api/lessons/hello/submit", json={"answers": code_and_one_other}) + assert response.json()["passed"] is True + + code_only = [ + {"question_id": "hello-print", "answer": "неверно"}, + {"question_id": "hello-string", "answer": "неверно"}, + {"question_id": "hello-code", "answer": "print('Я начинаю путь в Python!')"}, + ] + client.post("/api/reset") + response = client.post("/api/lessons/hello/submit", json={"answers": code_only}) + assert response.json()["passed"] is False + client.post("/api/reset")