"""The personal configuration controls both annotation generators.""" import os from pathlib import Path import subprocess import sys import tempfile import unittest class AnnotationHeightConfigTests(unittest.TestCase): def load_generators(self, setting, attribute="MAX_HEIGHT_PX"): with tempfile.TemporaryDirectory() as directory: config = Path(directory) / "config.py" config.write_text("from default_config import *\n" + setting, encoding="utf-8") return subprocess.run( [sys.executable, "-c", "from copienator.commands import annotating_by_label as grouped; " "from copienator.commands import annotating_with_checks as checks; " f"print(grouped.annotation_limits()[{0 if attribute == 'MAX_HEIGHT_PX' else 1}], checks.annotation_limits()[{0 if attribute == 'MAX_HEIGHT_PX' else 1}])"], cwd=Path(__file__).resolve().parents[1], env={**os.environ, "COPIENATOR_CONFIG": str(config)}, capture_output=True, text=True, check=False, ) def test_custom_height_reaches_both_generators(self): result = self.load_generators("ANNOTATION_MAX_HEIGHT_PX = 16000\n") self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout.strip(), "16000 16000") def test_older_configuration_keeps_current_default(self): result = self.load_generators("del ANNOTATION_MAX_HEIGHT_PX\n") self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout.strip(), "33000 33000") def test_invalid_heights_are_rejected(self): for value in ("0", "-1", "1.5", "True", "'32000'"): with self.subTest(value=value): result = self.load_generators(f"ANNOTATION_MAX_HEIGHT_PX = {value}\n") self.assertNotEqual(result.returncode, 0) self.assertIn("ANNOTATION_MAX_HEIGHT_PX must be a positive integer", result.stderr) def test_custom_pixel_limit_reaches_both_generators(self): result = self.load_generators("ANNOTATION_MAX_PIXELS = 50000000\n", "MAX_PIXELS") self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout.strip(), "50000000 50000000") def test_older_configuration_keeps_default_pixel_limit(self): result = self.load_generators("del ANNOTATION_MAX_PIXELS\n", "MAX_PIXELS") self.assertEqual(result.returncode, 0, result.stderr) self.assertEqual(result.stdout.strip(), "67108864 67108864") def test_invalid_pixel_limits_are_rejected(self): for value in ("0", "-1", "1.5", "True", "'67108864'"): with self.subTest(value=value): result = self.load_generators(f"ANNOTATION_MAX_PIXELS = {value}\n") self.assertNotEqual(result.returncode, 0) self.assertIn("ANNOTATION_MAX_PIXELS must be a positive integer", result.stderr)