miscs (Interro02) : horizontal cutting resolution
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import queue
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from copienator_gui.batch_monitor import BatchMonitor, CHECK_INTERVAL_MS
|
||||
from copienator_gui.notifications import notify_desktop
|
||||
|
||||
|
||||
class BatchMonitorTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.scheduler = Mock()
|
||||
self.scheduler.after.side_effect = lambda delay, callback: (delay, callback)
|
||||
self.ready = Mock()
|
||||
self.status = Mock()
|
||||
self.monitor = BatchMonitor(self.scheduler, self.ready, self.status, Mock())
|
||||
self.runner = Mock()
|
||||
self.runner.events = queue.Queue()
|
||||
self.factory = patch("copienator_gui.batch_monitor.ProcessRunner", return_value=self.runner)
|
||||
self.factory.start()
|
||||
self.addCleanup(self.factory.stop)
|
||||
|
||||
def start(self):
|
||||
self.monitor.start(["check"], "/tmp", {}, None)
|
||||
|
||||
def test_checks_immediately_retries_in_five_minutes_and_notifies_once(self):
|
||||
self.start()
|
||||
self.runner.start.assert_called_once()
|
||||
self.start()
|
||||
self.runner.start.assert_called_once()
|
||||
self.runner.events.put(("finished", (4, False)))
|
||||
self.monitor._poll()
|
||||
self.assertEqual(self.monitor.timer[0], CHECK_INTERVAL_MS)
|
||||
self.assertEqual(CHECK_INTERVAL_MS, 300000)
|
||||
self.ready.assert_not_called()
|
||||
self.monitor.timer[1]()
|
||||
self.assertEqual(self.runner.start.call_count, 2)
|
||||
self.runner.events.put(("finished", (0, False)))
|
||||
self.monitor._poll()
|
||||
self.assertFalse(self.monitor.active)
|
||||
self.assertIsNone(self.monitor.timer)
|
||||
self.ready.assert_called_once()
|
||||
self.monitor._poll()
|
||||
self.ready.assert_called_once()
|
||||
|
||||
def test_stop_cancels_timer_and_running_check_without_notification(self):
|
||||
self.start()
|
||||
timer = self.monitor.timer
|
||||
self.monitor.stop()
|
||||
self.scheduler.after_cancel.assert_called_once_with(timer)
|
||||
self.runner.force_stop.assert_called_once()
|
||||
self.runner.events.put(("finished", (0, False)))
|
||||
self.monitor._poll()
|
||||
self.monitor._check()
|
||||
self.ready.assert_not_called()
|
||||
self.runner.start.assert_called_once()
|
||||
|
||||
def test_start_failure_retries_without_reporting_readiness(self):
|
||||
self.runner.start.side_effect = OSError("unavailable")
|
||||
self.start()
|
||||
self.assertEqual(self.monitor.timer[0], CHECK_INTERVAL_MS)
|
||||
self.ready.assert_not_called()
|
||||
|
||||
def test_linux_notification_passes_text_as_arguments(self):
|
||||
with patch("copienator_gui.notifications.sys.platform", "linux"), patch(
|
||||
"copienator_gui.notifications.find_executable", return_value="/usr/bin/notify-send"
|
||||
), patch("copienator_gui.notifications.subprocess.Popen") as launch:
|
||||
notify_desktop("Copienator", "Interro02 : résultats prêts")
|
||||
self.assertEqual(launch.call_args.args[0], ["/usr/bin/notify-send", "--app-name=Copienator",
|
||||
"--", "Copienator", "Interro02 : résultats prêts"])
|
||||
@@ -0,0 +1,54 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, call, patch
|
||||
|
||||
from copienator import CliError, EvaluationWorkspace, ExitCode, atomic_write_json
|
||||
from copienator.commands.batch_status import check_evaluation_jobs, main
|
||||
|
||||
|
||||
class BatchReadinessTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.workspace = EvaluationWorkspace(Path(self.temp.name))
|
||||
self.client = Mock()
|
||||
|
||||
def manifest(self, jobs):
|
||||
atomic_write_json(self.workspace.batch_jobs_file, {"jobs": jobs})
|
||||
|
||||
def test_only_recorded_jobs_are_checked_and_all_must_have_results(self):
|
||||
self.manifest({"flash": {"name": "batches/flash"}, "pro": {"name": "batches/pro"}})
|
||||
succeeded = SimpleNamespace(state="JOB_STATE_SUCCEEDED",
|
||||
dest=SimpleNamespace(file_name="files/result"))
|
||||
for state in ("JOB_STATE_PENDING", "JOB_STATE_RUNNING", "JOB_STATE_FAILED",
|
||||
"JOB_STATE_CANCELLED", "JOB_STATE_EXPIRED", "UNKNOWN", "JOB_STATE_SUCCEEDED"):
|
||||
with self.subTest(state=state):
|
||||
self.client.reset_mock()
|
||||
self.client.batches.get.side_effect = [succeeded, SimpleNamespace(
|
||||
state=SimpleNamespace(name=state), dest=SimpleNamespace(file_name="files/pro"))]
|
||||
result = check_evaluation_jobs(self.workspace, client=self.client)
|
||||
self.assertEqual(result, ExitCode.SUCCESS if state == "JOB_STATE_SUCCEEDED" else ExitCode.PARTIAL)
|
||||
self.assertEqual(self.client.batches.get.call_args_list,
|
||||
[call(name="batches/flash"), call(name="batches/pro")])
|
||||
self.client.batches.list.assert_not_called()
|
||||
self.client.files.download.assert_not_called()
|
||||
self.client.batches.get.side_effect = [succeeded, SimpleNamespace(
|
||||
state="JOB_STATE_SUCCEEDED", dest=None)]
|
||||
self.assertEqual(check_evaluation_jobs(self.workspace, client=self.client), ExitCode.PARTIAL)
|
||||
|
||||
def test_missing_empty_and_invalid_manifest_cannot_report_success(self):
|
||||
self.assertEqual(check_evaluation_jobs(self.workspace, client=self.client), ExitCode.PARTIAL)
|
||||
self.manifest({})
|
||||
self.assertEqual(check_evaluation_jobs(self.workspace, client=self.client), ExitCode.PARTIAL)
|
||||
for jobs in ([], {"flash": {}}, {"flash": {"name": ""}}, {"flash": None}):
|
||||
self.manifest(jobs)
|
||||
with self.assertRaises(CliError):
|
||||
check_evaluation_jobs(self.workspace, client=self.client)
|
||||
self.client.batches.get.assert_not_called()
|
||||
|
||||
def test_cli_returns_readiness_code_for_selected_evaluation(self):
|
||||
with patch("copienator.commands.batch_status.check_evaluation_jobs", return_value=ExitCode.PARTIAL) as check:
|
||||
self.assertEqual(main(["--evaluation", str(self.workspace.root)]), ExitCode.PARTIAL)
|
||||
self.assertEqual(check.call_args.args[0].root, self.workspace.root)
|
||||
@@ -0,0 +1,82 @@
|
||||
import copy
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from PIL import Image
|
||||
|
||||
from copienator import EvaluationWorkspace, atomic_write_json
|
||||
from copienator.annotation_data import GroupCoordinates, _scaled_result
|
||||
from copienator.annotation_actions import apply_checkbox_actions
|
||||
from copienator.commands import annotating, correction
|
||||
from copienator.feedback_boxes import valid_feedback_box
|
||||
|
||||
|
||||
class FeedbackBoxTests(unittest.TestCase):
|
||||
def test_checkbox_for_promoted_feedback_deletes_the_correct_comment(self):
|
||||
feedback = [{"text": "Invalid local", "box_2d": [753, 680, 287, 946]},
|
||||
{"text": "Existing global", "box_2d": None}]
|
||||
apply_checkbox_actions({"Ex 5": {"result": {"feedback": feedback}}},
|
||||
[{"label": "Ex 5", "type": "del_global", "index": 0}], lambda _: None)
|
||||
self.assertTrue(feedback[0]["to_delete"])
|
||||
self.assertNotIn("to_delete", feedback[1])
|
||||
|
||||
def test_bad_boxes_keep_their_comment_as_global_feedback_without_mutating_data(self):
|
||||
for box in ([753, 680, 287, 946], [10, 50, 20, 30], [10, 20, 10, 30],
|
||||
[1, 2, 3], [None, 0, 10, 20], [float("nan"), 0, 10, 20]):
|
||||
with self.subTest(box=box):
|
||||
result = {"score": 2, "feedback": [{"text": "Important comment", "box_2d": box}]}
|
||||
original_box = result["feedback"][0]["box_2d"]
|
||||
scaled = _scaled_result(result, GroupCoordinates(2415, 3297, 1655, 3297))
|
||||
callback = Mock()
|
||||
render = Mock(return_value=Image.new("RGBA", (200, 40), "white"))
|
||||
with patch.object(annotating, "render_score_text", return_value=Image.new("RGBA", (200, 40))):
|
||||
image, _ = annotating.compose_label_image(Image.new("RGBA", (800, 100)), "Ex 5", scaled,
|
||||
2415, render_fn=render, draw_callback=callback)
|
||||
self.assertIsNotNone(image)
|
||||
render.assert_called_once_with("Important comment", unittest.mock.ANY)
|
||||
self.assertFalse(any(call.args[0] == "local_rect" for call in callback.call_args_list))
|
||||
self.assertIs(result["feedback"][0]["box_2d"], original_box)
|
||||
|
||||
def test_valid_boxes_remain_local(self):
|
||||
result = {"feedback": [{"text": "Local", "box_2d": [10, 20, 30, 40]}]}
|
||||
before = copy.deepcopy(result)
|
||||
callback = Mock()
|
||||
with patch.object(annotating, "render_score_text", return_value=Image.new("RGBA", (200, 40))):
|
||||
annotating.compose_label_image(Image.new("RGBA", (800, 100)), "Ex 5", result, 0,
|
||||
render_fn=Mock(return_value=Image.new("RGBA", (200, 40))),
|
||||
draw_callback=callback)
|
||||
self.assertTrue(any(call.args[0] == "local_rect" for call in callback.call_args_list))
|
||||
self.assertEqual(result, before)
|
||||
self.assertTrue(valid_feedback_box([10, 20, 30, 40]))
|
||||
|
||||
def test_invalid_auxiliary_response_loses_only_its_rectangle(self):
|
||||
returned = [{"text": "Keep this", "box_2d": [753, 680, 287, 946]}]
|
||||
with patch.object(correction.prompting, "request_for_box_correction", return_value=([], {})), patch.object(
|
||||
correction, "call_gemini_with_retries", return_value=json.dumps(returned)
|
||||
):
|
||||
feedback = correction.correct_boxes_with_gemini("26", "Ex 5", Path("unused.pdf"), [], 0, 1000, 1, 1000)
|
||||
self.assertEqual(feedback, [{"text": "Keep this", "box_2d": None}])
|
||||
|
||||
def test_correction_requests_repair_for_inverted_boxes_and_falls_back_without_losing_comments(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
group = root / "Par label" / "Ex 5" / "Group_1.jpg"
|
||||
group.parent.mkdir(parents=True)
|
||||
group.touch()
|
||||
atomic_write_json(group.with_suffix(".json"), [["26", 0, 1000, 1, "Ex 5"]])
|
||||
args = correction.build_parser().parse_args([str(root)])
|
||||
correction.configure_runtime(EvaluationWorkspace(root), [(str(group), "Ex 5")], args, api_client=Mock())
|
||||
response = [{"id": "26", "result": {"score": 2, "error": "", "feedback": [
|
||||
{"text": "First", "box_2d": [753, 680, 287, 946]},
|
||||
{"text": "Second", "box_2d": [100, 900, 200, 100]}]}}]
|
||||
with patch.object(correction.prompting, "generate_request", return_value=([], {})), patch.object(
|
||||
correction, "correct_boxes_with_gemini", side_effect=RuntimeError("repair failed")
|
||||
) as repair:
|
||||
correction.process_single_task((str(group), "Ex 5"), json.dumps(response))
|
||||
repair.assert_called_once()
|
||||
feedback = correction.results["Ex 5"][0][0]["result"]["feedback"]
|
||||
self.assertEqual([f["text"] for f in feedback], ["First", "Second"])
|
||||
self.assertTrue(all(f["box_2d"] is None for f in feedback))
|
||||
@@ -68,6 +68,66 @@ class GuiConvenienceTests(unittest.TestCase):
|
||||
self.app.open_evaluation_button.invoke()
|
||||
opened.assert_called_once_with(self.evaluation)
|
||||
|
||||
def test_manual_resolution_panel_follows_command_target(self):
|
||||
manual = self.evaluation / "manual_resolutions.txt"
|
||||
manual.write_text("Copie01 A -> B|\n", encoding="utf-8")
|
||||
self.app.tree.selection_set("manual_resolution")
|
||||
self.app.update()
|
||||
self.assertEqual(len(self.app.manual_panel.pdf_buttons), 2)
|
||||
other = self.repository / "Other"
|
||||
other.mkdir()
|
||||
other_manual = other / "manual_resolutions.txt"
|
||||
other_manual.write_text("Copie02 C xs D\n", encoding="utf-8")
|
||||
self.app.arg_vars["target"].set(str(other))
|
||||
self.app.update()
|
||||
self.assertIn("Copie02", self.app.manual_panel.text.get("1.0", "end"))
|
||||
with patch("copienator_gui.manual_resolution.open_path") as opened:
|
||||
self.app.manual_panel.editor_button.invoke()
|
||||
opened.assert_called_once_with(other_manual)
|
||||
|
||||
def test_batch_status_waits_until_all_jobs_are_ready(self):
|
||||
self.app.state_store.update_step("correction", variant="batch")
|
||||
self.app.state_store.update_step("batch_status", visited=True)
|
||||
self.app.tree.selection_set("batch_status")
|
||||
self.app.update()
|
||||
command = self.app._make_command()
|
||||
self.assertIn("--evaluation", command)
|
||||
self.assertFalse(self.app.arg_vars)
|
||||
self.app.active_step_id = "batch_status"
|
||||
self.app._finish_process(4, False)
|
||||
self.app.update()
|
||||
self.assertEqual(self.app.current_step.id, "batch_status")
|
||||
self.app.active_step_id = "batch_status"
|
||||
self.app._finish_process(0, False)
|
||||
self.app.update()
|
||||
self.assertEqual(self.app.current_step.id, "fetch_batches")
|
||||
|
||||
def test_batch_watch_buttons_use_loaded_evaluation_and_stop_on_reload(self):
|
||||
self.app.state_store.update_step("correction", variant="batch")
|
||||
self.app.tree.selection_set("batch_status")
|
||||
self.app.update()
|
||||
with patch.object(self.app.batch_monitor, "start") as start:
|
||||
self.app.watch_batches_button.invoke()
|
||||
self.assertEqual(start.call_args.args[0][-2:], ["--evaluation", str(self.evaluation)])
|
||||
self.app.batch_monitor.active = True
|
||||
self.app._update_controls()
|
||||
self.assertEqual(str(self.app.stop_watch_batches_button.cget("state")), "normal")
|
||||
self.app.stop_watch_batches_button.invoke()
|
||||
self.assertFalse(self.app.batch_monitor.active)
|
||||
self.app.batch_monitor.active = True
|
||||
self.app._load_evaluation()
|
||||
self.assertFalse(self.app.batch_monitor.active)
|
||||
|
||||
def test_background_batch_readiness_notifies_without_changing_other_step(self):
|
||||
self.app.tree.selection_set("inputs")
|
||||
self.app.update()
|
||||
with patch("copienator_gui.app.notify_desktop") as notify:
|
||||
self.app._batch_results_ready()
|
||||
notify.assert_called_once()
|
||||
self.assertIn(self.evaluation.name, notify.call_args.args[1])
|
||||
self.assertEqual(self.app.current_step.id, "inputs")
|
||||
self.assertEqual(self.app.state_store.step("batch_status")["status"], "success")
|
||||
|
||||
def test_optional_blank_crop_can_target_a_copy_or_be_skipped(self):
|
||||
copies = self.evaluation/"Copies"
|
||||
copies.mkdir()
|
||||
|
||||
+10
-1
@@ -1340,11 +1340,20 @@ class StandardCliTests(unittest.TestCase):
|
||||
evaluation / "correction.json",
|
||||
{"text": "outside_name and $math_name$", "suffix": "_new"},
|
||||
)
|
||||
original = (evaluation / "correction.json").read_bytes()
|
||||
backup = evaluation / "correction_precleanup.json"
|
||||
backup.write_bytes(b"previous backup")
|
||||
self.assertEqual(module.main([str(evaluation)]), 0)
|
||||
self.assertEqual(backup.read_bytes(), original)
|
||||
self.assertEqual(
|
||||
read_json(evaluation / "correction.json"),
|
||||
{"text": r"outside\_name and $math_name$", "suffix": "_new"},
|
||||
)
|
||||
cleaned = (evaluation / "correction.json").read_bytes()
|
||||
with patch.object(module, "atomic_write_bytes", side_effect=OSError("backup failed")):
|
||||
self.assertNotEqual(module.main([str(evaluation)]), 0)
|
||||
self.assertEqual((evaluation / "correction.json").read_bytes(), cleaned)
|
||||
self.assertEqual(backup.read_bytes(), original)
|
||||
|
||||
def test_manual_resolution_with_no_actions_is_safe(self) -> None:
|
||||
module = self.modules["resolve_manual"]
|
||||
@@ -1891,7 +1900,7 @@ class WorkflowTests(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(
|
||||
command_arguments(status),
|
||||
["--download", "files/job-1", "--output", "/tmp/result.jsonl"],
|
||||
["--evaluation", self.evaluation],
|
||||
)
|
||||
|
||||
grouped = self.command(
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
import tempfile
|
||||
import tkinter as tk
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pymupdf
|
||||
|
||||
from copienator_gui.cut_helper import CutHelper, PAGE_GAP
|
||||
from copienator_gui.manual_resolution import ManualResolutionPanel
|
||||
|
||||
|
||||
@unittest.skipUnless(os.environ.get("DISPLAY"), "requires a display")
|
||||
class CutHelperTests(unittest.TestCase):
|
||||
def test_cut_button_drag_to_page_gap_and_enter_only_produces_command(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
directory = Path(temporary)
|
||||
source = directory / "Copies" / "Copie16" / "A.pdf"
|
||||
source.parent.mkdir(parents=True)
|
||||
with pymupdf.open() as document:
|
||||
for height in (200, 400):
|
||||
page = document.new_page(width=300, height=height)
|
||||
page.insert_text((20, 40), "Student answer")
|
||||
document.save(source)
|
||||
manual = directory / "manual_resolutions.txt"
|
||||
manual.write_text("Copie16 A -> B|\n")
|
||||
original = source.read_bytes()
|
||||
root = tk.Tk()
|
||||
try:
|
||||
panel = ManualResolutionPanel(root, lambda: directory)
|
||||
panel.pack()
|
||||
root.update()
|
||||
self.assertEqual(len(panel.cut_buttons), 1)
|
||||
panel.cut_buttons[0].invoke()
|
||||
helper = next(child for child in panel.winfo_children() if isinstance(child, CutHelper))
|
||||
helper.canvas.yview_moveto(0)
|
||||
root.update()
|
||||
helper.move_bar(SimpleNamespace(y=200 * helper.scale + PAGE_GAP / 2))
|
||||
self.assertIn("entre les pages 1 et 2", helper.caption.get())
|
||||
helper.keep.set(2)
|
||||
helper.mode.set("x")
|
||||
helper.focus_force()
|
||||
root.update()
|
||||
helper.event_generate("<Return>")
|
||||
root.update()
|
||||
self.assertFalse(helper.winfo_exists())
|
||||
self.assertEqual(panel.cut_result.get(), "Copie16 A c{33.333333}2x B|")
|
||||
panel.copy_cut_command()
|
||||
self.assertEqual(root.clipboard_get(), panel.cut_result.get())
|
||||
self.assertEqual(source.read_bytes(), original)
|
||||
self.assertEqual(manual.read_text(), "Copie16 A -> B|\n")
|
||||
panel.reload()
|
||||
self.assertEqual(len(panel.cut_buttons), 1)
|
||||
finally:
|
||||
root.destroy()
|
||||
@@ -0,0 +1,74 @@
|
||||
import os
|
||||
import tempfile
|
||||
import tkinter as tk
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import call, patch
|
||||
|
||||
from copienator_gui.manual_resolution import ManualResolutionPanel
|
||||
|
||||
|
||||
@unittest.skipUnless(os.environ.get("DISPLAY"), "requires a display")
|
||||
class ManualResolutionPanelTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.evaluation = Path(self.temp.name)
|
||||
self.root = tk.Tk()
|
||||
self.addCleanup(self.root.destroy)
|
||||
self.path = self.evaluation / "manual_resolutions.txt"
|
||||
|
||||
def panel(self):
|
||||
panel = ManualResolutionPanel(self.root, lambda: self.evaluation)
|
||||
panel.pack()
|
||||
self.root.update()
|
||||
return panel
|
||||
|
||||
def test_preview_editor_and_each_pdf_pair_use_shared_resolution_rules(self):
|
||||
content = "### Instructions\nCopie01 Ex 1 x> |Ex 2\n\nCopie02 Ex 3 ss Ex 4|\n"
|
||||
self.path.write_text(content, encoding="utf-8")
|
||||
paths = []
|
||||
for copy, label in (("01", "Ex 1_new"), ("01", "Ex 2_old"),
|
||||
("02", "Ex 3"), ("02", "Ex 4")):
|
||||
path = self.evaluation / "Copies" / f"Copie{copy}" / f"{label}.pdf"
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.touch()
|
||||
paths.append(path)
|
||||
panel = self.panel()
|
||||
self.assertEqual(panel.text.get("1.0", "end-1c"), content)
|
||||
self.assertEqual(len(panel.pdf_buttons), 4)
|
||||
self.assertEqual([button.cget("text") for button in panel.pdf_buttons],
|
||||
["PDF source", "PDF cible"] * 2)
|
||||
with patch("copienator_gui.manual_resolution.open_path") as opened:
|
||||
panel.editor_button.invoke()
|
||||
for button in panel.pdf_buttons:
|
||||
button.invoke()
|
||||
self.assertEqual(opened.call_args_list, [call(self.path), *map(call, paths)])
|
||||
|
||||
def test_reload_keeps_invalid_lines_visible_and_removes_stale_actions(self):
|
||||
self.path.write_text("Copie01 A -> B|\n", encoding="utf-8")
|
||||
panel = self.panel()
|
||||
self.path.write_text("bad instruction\nCopie02 C sx D\n", encoding="utf-8")
|
||||
panel.reload()
|
||||
self.assertIn("bad instruction", panel.text.get("1.0", "end"))
|
||||
self.assertIn("lignes invalides : 1", panel.status.cget("text"))
|
||||
self.assertEqual(len(panel.pdf_buttons), 2)
|
||||
self.path.unlink()
|
||||
panel.reload()
|
||||
self.assertFalse(panel.pdf_buttons)
|
||||
self.assertEqual(str(panel.editor_button.cget("state")), "disabled")
|
||||
|
||||
def test_missing_source_does_not_prevent_opening_destination(self):
|
||||
self.path.write_text("Copie01 A -> B|\n", encoding="utf-8")
|
||||
destination = self.evaluation / "Copies" / "Copie01" / "B.pdf"
|
||||
destination.parent.mkdir(parents=True)
|
||||
destination.touch()
|
||||
panel = self.panel()
|
||||
with patch("copienator_gui.manual_resolution.open_path") as opened, patch(
|
||||
"copienator_gui.manual_resolution.messagebox.showerror"
|
||||
) as error:
|
||||
panel.pdf_buttons[0].invoke()
|
||||
opened.assert_not_called()
|
||||
panel.pdf_buttons[1].invoke()
|
||||
opened.assert_called_once_with(destination)
|
||||
self.assertIn("A.pdf", error.call_args.args[1])
|
||||
@@ -0,0 +1,125 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import numpy as np
|
||||
import pymupdf
|
||||
|
||||
from copienator import CliError, EvaluationWorkspace, atomic_write_json, read_json
|
||||
from copienator.commands.resolve_manual import parse_instruction_text, resolve_manual
|
||||
from copienator.pdf_cut import cut_position, split_pdf
|
||||
from copienator_gui.cut_helper import PAGE_GAP, cut_operator, percentage_at_y, y_at_percentage
|
||||
|
||||
|
||||
def make_pdf(path, heights=(300, 700), rotation=0, cropped=False):
|
||||
with pymupdf.open() as document:
|
||||
for index, height in enumerate(heights):
|
||||
page = document.new_page(width=200, height=height)
|
||||
page.draw_rect(pymupdf.Rect(0, 0, 200, height / 2), color=None, fill=(1, 0, 0))
|
||||
page.draw_rect(pymupdf.Rect(0, height / 2, 200, height), color=None, fill=(0, 0, 1))
|
||||
page.insert_text((30, 30), f"Page {index + 1}")
|
||||
if cropped:
|
||||
page.set_cropbox(pymupdf.Rect(10, 20, 180, height - 30))
|
||||
page.set_rotation(rotation)
|
||||
document.save(path)
|
||||
|
||||
|
||||
class ManualCutTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
self.root = Path(self.temp.name)
|
||||
|
||||
def test_parser_accepts_new_operators_and_rejects_invalid_cuts(self):
|
||||
for keep in (1, 2):
|
||||
for action in (">", "x"):
|
||||
item = parse_instruction_text(f"Copie16 Ex 4 : 1) c{{43.125}}{keep}{action} |Ex 4 : 2)")[0]
|
||||
self.assertEqual(item.cut, (43.125, keep))
|
||||
self.assertEqual(item.should_merge, action == ">")
|
||||
self.assertTrue(item.pipe_first)
|
||||
for operator in ("c{0}1>", "c{100}1>", "c{-1}1>", "c{101}1>", "c{43}3>",
|
||||
"c{43}1s", "c{NaN}1>"):
|
||||
with self.assertRaises(CliError):
|
||||
parse_instruction_text(f"Copie16 A {operator} B")
|
||||
with self.assertRaises(CliError):
|
||||
parse_instruction_text("Copie16 A c{43}1> A")
|
||||
|
||||
def test_page_boundary_preserves_whole_pages_without_clipping(self):
|
||||
source, first, second = [self.root / name for name in ("source.pdf", "first.pdf", "second.pdf")]
|
||||
make_pdf(source, heights=(100, 200), rotation=180)
|
||||
before = source.read_bytes()
|
||||
with patch("pymupdf.Page.show_pdf_page", side_effect=AssertionError("must not clip whole pages")):
|
||||
split_pdf(source, 33.333333, first, second)
|
||||
for path, height in ((first, 100), (second, 200)):
|
||||
with pymupdf.open(path) as pdf:
|
||||
self.assertEqual(len(pdf), 1)
|
||||
self.assertEqual(pdf[0].rect.height, height)
|
||||
self.assertEqual(pdf[0].rotation, 180)
|
||||
self.assertEqual(source.read_bytes(), before)
|
||||
|
||||
def test_in_page_cut_preserves_visible_pixels_for_cropped_rotated_pages(self):
|
||||
source, first, second = [self.root / name for name in ("source.pdf", "first.pdf", "second.pdf")]
|
||||
for rotation in (0, 90, 180, 270):
|
||||
with self.subTest(rotation=rotation):
|
||||
make_pdf(source, heights=(400,), rotation=rotation, cropped=True)
|
||||
with pymupdf.open(source) as pdf:
|
||||
pix = pdf[0].get_pixmap()
|
||||
expected = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3)
|
||||
split_pdf(source, 50, first, second)
|
||||
for path, pixels in ((first, expected[:len(expected)//2]), (second, expected[len(expected)//2:])):
|
||||
with pymupdf.open(path) as pdf:
|
||||
pix = pdf[0].get_pixmap()
|
||||
actual = np.frombuffer(pix.samples, np.uint8).reshape(pix.height, pix.width, 3)
|
||||
self.assertEqual(actual.shape, pixels.shape)
|
||||
self.assertLess(np.abs(actual.astype(float) - pixels).mean(), 0.1)
|
||||
|
||||
def test_cut_within_page_preserves_subsequent_pages(self):
|
||||
source, first, second = [self.root / name for name in ("source.pdf", "first.pdf", "second.pdf")]
|
||||
make_pdf(source)
|
||||
split_pdf(source, 15, first, second)
|
||||
with pymupdf.open(first) as pdf:
|
||||
self.assertEqual([page.rect.height for page in pdf], [150])
|
||||
with pymupdf.open(second) as pdf:
|
||||
self.assertEqual([page.rect.height for page in pdf], [150, 700])
|
||||
|
||||
def test_resolver_archives_source_and_recorrrects_both_labels(self):
|
||||
for keep in (1, 2):
|
||||
for mode in (">", "x"):
|
||||
for pipe_first in (False, True):
|
||||
with self.subTest(keep=keep, mode=mode, pipe_first=pipe_first):
|
||||
root = self.root / f"{keep}{mode == '>'}{pipe_first}"
|
||||
copies = root / "Copies" / "Copie16"
|
||||
copies.mkdir(parents=True)
|
||||
source, target = copies / "A.pdf", copies / "B.pdf"
|
||||
make_pdf(source)
|
||||
make_pdf(target, heights=(80,))
|
||||
original, destination = source.read_bytes(), target.read_bytes()
|
||||
atomic_write_json(root / "correction.json", {
|
||||
label: [[{"id": "16", "result": {}}]] for label in ("A", "B")
|
||||
})
|
||||
new_label = "|B" if pipe_first else "B|"
|
||||
(root / "manual_resolutions.txt").write_text(f"Copie16 A c{{30}}{keep}{mode} {new_label}\n")
|
||||
self.assertEqual(resolve_manual(EvaluationWorkspace(root)), 0)
|
||||
self.assertEqual((copies / "A_old.pdf").read_bytes(), original)
|
||||
self.assertEqual((copies / "B_old.pdf").read_bytes(), destination)
|
||||
retained, moved = (300, 700) if keep == 1 else (700, 300)
|
||||
with pymupdf.open(copies / "A_new.pdf") as pdf:
|
||||
self.assertEqual([page.rect.height for page in pdf], [retained])
|
||||
with pymupdf.open(copies / "B_new.pdf") as pdf:
|
||||
expected = ([moved, 80] if pipe_first else [80, moved]) if mode == ">" else [moved]
|
||||
self.assertEqual([page.rect.height for page in pdf], expected)
|
||||
self.assertEqual(read_json(root / "refaire.json"), [["Copie16", ["A", "B"]]])
|
||||
self.assertTrue(all(read_json(root / "correction.json")[label][0][0]["result"]["suffix"] == "_new" for label in ("A", "B")))
|
||||
self.assertFalse(list(copies.glob("temp_*.pdf")))
|
||||
|
||||
def test_helper_snaps_to_gap_and_round_trips_percentage(self):
|
||||
heights = [100, 200]
|
||||
for y in (95, 100, 100 + PAGE_GAP / 2, 100 + PAGE_GAP + 5):
|
||||
percent = percentage_at_y(y, heights, 1)
|
||||
self.assertEqual(cut_position(heights, percent), (1, 0))
|
||||
self.assertEqual(y_at_percentage(percent, heights, 1), 100 + PAGE_GAP / 2)
|
||||
self.assertEqual(cut_operator(43, 1, ">"), "c{43}1>")
|
||||
self.assertEqual(cut_operator(100/3, 2, "x"), "c{33.333333}2x")
|
||||
self.assertEqual(cut_position(heights, 33.333333), (1, 0))
|
||||
self.assertNotEqual(cut_position(heights, 33.3)[1], 0)
|
||||
@@ -0,0 +1,65 @@
|
||||
import copy
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import pymupdf
|
||||
|
||||
from copienator import EvaluationWorkspace, atomic_write_json, read_json
|
||||
from copienator.commands import correction, resolve_manual
|
||||
|
||||
|
||||
class ManualResolutionStateTests(unittest.TestCase):
|
||||
def test_acknowledgement_clears_only_this_target_and_copy(self):
|
||||
for error in ("wrg-lbl:B?", "wrg-lbl:B?delayed", "wrg-lbl:B?exists", "al:(->)B?(->)C?", "al:(delayed)B"):
|
||||
with self.subTest(error=error):
|
||||
source = {"id": "01", "result": {"error": error, "delayed": [
|
||||
["wrong-label", "B"], ["add-label", "B"], ["add-label", "C"]]}}
|
||||
other = {"id": "02", "result": {"error": error, "delayed": [["wrong-label", "B"]]}}
|
||||
before_other = copy.deepcopy(other)
|
||||
results = {"A": [[source, other]]}
|
||||
resolve_manual.set_suffix_and_clean_error(results, "01", "A", None, "B")
|
||||
self.assertEqual(source["result"]["delayed"], [["add-label", "C"]])
|
||||
self.assertNotIn("B?", source["result"]["error"])
|
||||
self.assertEqual(other, before_other)
|
||||
resolve_manual.set_suffix_and_clean_error(results, "01", "A", None, "C")
|
||||
self.assertNotIn("delayed", source["result"])
|
||||
|
||||
def fixture(self, root, operator):
|
||||
copies = root / "Copies" / "Copie01"
|
||||
copies.mkdir(parents=True)
|
||||
for label in ("A", "B"):
|
||||
with pymupdf.open() as doc:
|
||||
page = doc.new_page(width=200, height=200)
|
||||
page.insert_text((20, 40), label)
|
||||
doc.save(copies / f"{label}.pdf")
|
||||
atomic_write_json(root / "correction.json", {
|
||||
"A": [[{"id": "01", "result": {"error": "wrg-lbl:B?", "delayed": [["wrong-label", "B"]]}}]],
|
||||
"B": [[{"id": "01", "result": {"error": ""}}]],
|
||||
})
|
||||
(root / "manual_resolutions.txt").write_text(f"Copie01 A {operator} B|\n")
|
||||
return EvaluationWorkspace(root)
|
||||
|
||||
def test_every_resolution_acknowledges_pending_conflict(self):
|
||||
for operator in (*resolve_manual.OPERATORS, "c{50}1>", "c{50}2x"):
|
||||
with self.subTest(operator=operator), tempfile.TemporaryDirectory() as temporary:
|
||||
workspace = self.fixture(Path(temporary), operator)
|
||||
resolve_manual.resolve_manual(workspace)
|
||||
result = read_json(workspace.correction_file)["A"][0][0]["result"]
|
||||
self.assertNotIn("delayed", result)
|
||||
self.assertFalse(workspace.manual_resolutions_file.exists())
|
||||
|
||||
def test_refaire_does_not_recreate_a_resolved_source_conflict(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
workspace = self.fixture(Path(temporary), "x>")
|
||||
resolve_manual.resolve_manual(workspace)
|
||||
(workspace.groups_dir / "B").mkdir(parents=True)
|
||||
args = correction.build_parser().parse_args([str(workspace.root), "--refaire"])
|
||||
correction.configure_runtime(workspace, [], args, api_client=Mock())
|
||||
with patch.object(correction.grouping, "get_pdf_height", return_value=400), patch.object(
|
||||
correction.grouping, "create_jpg"
|
||||
), patch.object(correction, "process_single_task", return_value=[]):
|
||||
self.assertEqual(correction.run_configured(args), 0)
|
||||
self.assertFalse(workspace.manual_resolutions_file.exists())
|
||||
self.assertNotIn("delayed", correction.results["A"][0][0]["result"])
|
||||
@@ -0,0 +1,33 @@
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from copienator import prompting
|
||||
|
||||
|
||||
class PromptingTests(unittest.TestCase):
|
||||
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()
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Compare extracted answers against the same Poppler view used for labels."""
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pymupdf
|
||||
from pdf2image import convert_from_path
|
||||
|
||||
from copienator.commands.splitting_int import _render_split_outputs
|
||||
|
||||
|
||||
class SplittingGeometryTests(unittest.TestCase):
|
||||
def make_source(self, path, rotation, cropped=True):
|
||||
with pymupdf.open() as document:
|
||||
page = document.new_page(width=800, height=1000)
|
||||
# Asymmetric colours exercise both translation and orientation.
|
||||
for row in range(20):
|
||||
for column in range(8):
|
||||
colour = (row / 20, column / 8, (row + column) % 7 / 7)
|
||||
page.draw_rect(
|
||||
pymupdf.Rect(column * 100, row * 50,
|
||||
(column + 1) * 100, (row + 1) * 50),
|
||||
color=None, fill=colour,
|
||||
)
|
||||
page.insert_text((200, 500), "Middle of the answer")
|
||||
if cropped:
|
||||
page.set_cropbox(pymupdf.Rect(20, 80, 760, 920))
|
||||
page.set_rotation(rotation)
|
||||
document.save(path)
|
||||
|
||||
def assert_matches_preview(self, answer, preview, bounds):
|
||||
actual = np.asarray(convert_from_path(answer, dpi=72)[0]).astype(float)
|
||||
expected = np.asarray(preview.crop(bounds)).astype(float)
|
||||
self.assertEqual(actual.shape, expected.shape)
|
||||
self.assertLess(np.abs(actual - expected).mean(), 0.5)
|
||||
|
||||
def test_cropped_and_uncropped_pages_at_every_rotation(self):
|
||||
visible = {
|
||||
0: (20, 80, 760, 920),
|
||||
90: (80, 20, 920, 760),
|
||||
180: (40, 80, 780, 920),
|
||||
270: (80, 40, 920, 780),
|
||||
}
|
||||
for rotation in (0, 90, 180, 270):
|
||||
for cropped in (False, True):
|
||||
for full_answer in (False, True):
|
||||
with self.subTest(rotation=rotation, cropped=cropped, full=full_answer):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
source = root / "copy.pdf"
|
||||
self.make_source(source, rotation, cropped)
|
||||
before = source.read_bytes()
|
||||
preview = convert_from_path(source, dpi=72, use_cropbox=False)[0]
|
||||
width, height = preview.size
|
||||
bounds = visible[rotation] if cropped else (0, 0, width, height)
|
||||
if full_answer:
|
||||
coordinates = [("A", 0, 0, 10, 0, 100)]
|
||||
else:
|
||||
coordinates = [("A", 0, 250, 260, 0, 100),
|
||||
("_", 0, 711, 721, 0, 100)]
|
||||
bounds = (bounds[0], max(bounds[1], height // 4),
|
||||
bounds[2], min(bounds[3], height * 3 // 4))
|
||||
_render_split_outputs(source, coordinates, root)
|
||||
self.assert_matches_preview(root / "A.pdf", preview, bounds)
|
||||
self.assertEqual(source.read_bytes(), before)
|
||||
|
||||
def test_answer_continues_to_bottom_then_next_page(self):
|
||||
with tempfile.TemporaryDirectory() as temporary:
|
||||
root = Path(temporary)
|
||||
source = root / "copy.pdf"
|
||||
first = root / "first.pdf"
|
||||
second = root / "second.pdf"
|
||||
self.make_source(first, 180)
|
||||
self.make_source(second, 0)
|
||||
with pymupdf.open() as document:
|
||||
for path in (first, second):
|
||||
with pymupdf.open(path) as part:
|
||||
document.insert_pdf(part)
|
||||
document.save(source)
|
||||
previews = convert_from_path(source, dpi=72, use_cropbox=False)
|
||||
_render_split_outputs(source, [("A", 0, 700, 720, 0, 100),
|
||||
("_", 1, 211, 230, 500, 600)], root)
|
||||
rendered = convert_from_path(root / "A.pdf", dpi=72)
|
||||
self.assertEqual(len(rendered), 2)
|
||||
for actual, preview, bounds in zip(rendered, previews,
|
||||
[(40, 700, 780, 920), (20, 80, 760, 250)]):
|
||||
expected = np.asarray(preview.crop(bounds)).astype(float)
|
||||
actual = np.asarray(actual).astype(float)
|
||||
self.assertEqual(actual.shape, expected.shape)
|
||||
self.assertLess(np.abs(actual - expected).mean(), 0.5)
|
||||
Reference in New Issue
Block a user