Files
Copies/tests/test_prompting.py
T

65 lines
3.0 KiB
Python

import unittest
import tempfile
from pathlib import Path
from unittest.mock import patch
from copienator import prompting
class PromptingTests(unittest.TestCase):
def test_confirmed_label_request_keeps_grading_material_and_identifies_single_answer(self):
with (
patch.object(prompting, "get_single_image_bytes", return_value=b"answer"),
patch.object(prompting, "get_label_text_content", return_value="Question"),
patch.object(prompting, "get_label_sol_content", return_value="Solution"),
patch.object(prompting, "get_label_persp_content", return_value="Marking criteria"),
):
contents, config = prompting.request_for_confirmed_label("evaluation", "answer.pdf", "Ex 2 : 2)b)", "01")
prompt = contents[0].parts[1].text
self.assertEqual(contents[0].parts[0].inline_data.data, b"answer")
for text in ("Question", "Solution", "Marking criteria", "`01`", "`Ex 2 : 2)b)`",
"Reprends sa correction mathématique", "Ne renvoie pas `wrong-label`"):
self.assertIn(text, prompt)
self.assertEqual(config.response_mime_type, "application/json")
def test_live_prompt_includes_only_metadata_ids_in_order(self) -> None:
with tempfile.TemporaryDirectory() as directory:
image = Path(directory) / "Group_16.jpg"
image.write_bytes(b"image")
image.with_suffix(".json").write_text(
'[["22", 50, 834, 1], ["37", 904, 3250, 1], '
'["13", 3320, 4072, 1], ["05", 4142, 4892, 1]]'
)
contents, _ = prompting.generate_request(directory, image, "I)12)")
prompt = contents[0].parts[1].text
self.assertIn('["22", "37", "13", "05"]', prompt)
self.assertIn("exactement un résultat par identifiant", prompt)
self.assertIn("ignore les numéros manuscrits", prompt)
def test_perspective_is_inserted_with_alternative_method_guidance(self) -> None:
with patch.object(
prompting, "get_label_text_content", return_value="Question"
), patch.object(
prompting, "get_label_sol_content", return_value="Solution"
), patch.object(
prompting, "get_label_persp_content", return_value="Barème détaillé"
):
prompt = prompting.make_prompt("evaluation", "Ex 1")
self.assertIn(prompting.PERSPECTIVE_GUIDANCE, prompt)
self.assertIn("Barème détaillé", prompt)
def test_alternative_method_guidance_is_omitted_without_perspective(self) -> None:
with patch.object(
prompting, "get_label_text_content", return_value="Question"
), patch.object(
prompting, "get_label_sol_content", return_value="Solution"
), patch.object(prompting, "get_label_persp_content", return_value=None):
prompt = prompting.make_prompt("evaluation", "Ex 1")
self.assertNotIn(prompting.PERSPECTIVE_GUIDANCE, prompt)
if __name__ == "__main__":
unittest.main()