File size: 3,937 Bytes
3826b7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from __future__ import annotations

import subprocess
import sys
import tempfile
import unittest
from pathlib import Path


sys.path.insert(0, str(Path(__file__).parent))
import groundtruth_diff  # noqa: E402


class GroundtruthDiffTest(unittest.TestCase):
    def test_artifact_name(self) -> None:
        self.assertEqual(
            groundtruth_diff.artifact_name("document.pdf.page_no_2.py.json.word.txt"),
            ("document.pdf.page_no_2.py.json", "word"),
        )
        self.assertIsNone(groundtruth_diff.artifact_name("document.py.json.gz"))

    def test_measure_modified_added_and_deleted(self) -> None:
        modified = groundtruth_diff.measure(
            "page", "line", groundtruth_diff.ContentPair("same\nold\n", "same\nnew\nextra\n")
        )
        self.assertEqual((modified.status, modified.added, modified.removed), ("modified", 2, 1))
        self.assertGreater(modified.score, 0)
        coordinate_change = groundtruth_diff.measure(
            "page",
            "char",
            groundtruth_diff.ContentPair("(1, 2) /F1 text\n", "(1, 3) /F1 text\n"),
        )
        self.assertGreater(coordinate_change.score, 0)
        self.assertLess(coordinate_change.score, 1)
        self.assertEqual(
            groundtruth_diff.measure("page", "line", groundtruth_diff.ContentPair("", "new\n")).status,
            "added",
        )
        self.assertEqual(
            groundtruth_diff.measure("page", "line", groundtruth_diff.ContentPair("old\n", "")).status,
            "deleted",
        )

    def test_directory_comparison_groups_and_sorts_pages(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            old, new = root / "old" / "parser", root / "new" / "parser"
            old.mkdir(parents=True)
            new.mkdir(parents=True)
            (old / "a.pdf.page_no_1.py.json.word.txt").write_text("one\ntwo\n", encoding="utf-8")
            (new / "a.pdf.page_no_1.py.json.word.txt").write_text("changed\n", encoding="utf-8")
            (old / "b.pdf.page_no_1.py.json.line.txt").write_text("same\n", encoding="utf-8")
            (new / "b.pdf.page_no_1.py.json.line.txt").write_text("same\n", encoding="utf-8")
            (new / "c.pdf.page_no_1.py.json.char.txt").write_text("added\n", encoding="utf-8")

            pages = groundtruth_diff.analyze(groundtruth_diff.Comparison(old, new))

            self.assertEqual([page.name for page in pages], ["a.pdf.page_no_1.py.json", "c.pdf.page_no_1.py.json"])
            self.assertEqual(pages[1].artifacts["char"].status, "added")

    def test_git_comparison_uses_ref_and_finds_untracked_files(self) -> None:
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            parser = root / "groundtruth" / "parser"
            parser.mkdir(parents=True)
            subprocess.run(["git", "init", "-q"], cwd=root, check=True)
            subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=root, check=True)
            subprocess.run(["git", "config", "user.name", "Test"], cwd=root, check=True)
            tracked = parser / "a.pdf.page_no_1.py.json.word.txt"
            tracked.write_text("before\n", encoding="utf-8")
            subprocess.run(["git", "add", "groundtruth/parser"], cwd=root, check=True)
            subprocess.run(
                ["git", "-c", "core.hooksPath=/dev/null", "commit", "-qm", "baseline"],
                cwd=root,
                check=True,
            )
            tracked.write_text("after\n", encoding="utf-8")
            (parser / "b.pdf.page_no_1.py.json.line.txt").write_text("new\n", encoding="utf-8")

            pages = groundtruth_diff.analyze(groundtruth_diff.Comparison(parser, root=root))

            self.assertEqual({page.name for page in pages}, {"a.pdf.page_no_1.py.json", "b.pdf.page_no_1.py.json"})


if __name__ == "__main__":
    unittest.main()