File size: 6,546 Bytes
a916ede
5c05a34
a916ede
 
 
 
 
 
5c05a34
 
 
 
 
a916ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c05a34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a916ede
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""TDD contract for Sejm interpellations / written-questions ingestion."""
import json
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent))

ROOT = Path(__file__).resolve().parent.parent
PARQUET = ROOT / "data" / "sejm_interpellations" / "sejm_interpellations.parquet"
STATS = ROOT / "data" / "sejm_interpellations" / "sejm_interpellations.stats.json"
CANON = ["id", "text", "source", "added", "created", "token_count", "license", "author"]

from fetch_sejm_interpellations import (
    MIN_CHARS,
    normalize_document,
    should_fetch_reply,
)

BODY = (
    "Szanowny Panie Ministrze! " + ("Ogrody działkowe wymagają ochrony prawnej. " * 12)
)

Q_HTML = f"""<!DOCTYPE html>
<html lang="pl"><head><title>Interpelacja w sprawie ogrodów</title></head>
<body>
<h1>Interpelacja nr 1</h1>
<p class="int-recipient">do ministra rozwoju i technologii</p>
<p class="int-title">w sprawie sytuacji w rodzinnych ogrodach działkowych</p>
<p class="intAuthor">Zgłaszający: Katarzyna Osos</p>
<p class="intDateTresc">Data wpływu: 15-11-2023</p>
<p>{BODY}</p>
<p>Podstawa: decyzja (znak: BPRM.4820.2.3.2020).</p>
</body></html>
"""

R_HTML = f"""<!DOCTYPE html>
<html lang="pl"><head><title>Odpowiedź na interpelację w sprawie ogrodów</title></head>
<body>
<h1>Odpowiedź na interpelację nr 8</h1>
<p class="int-title">w sprawie tzw. specustawy</p>
<p class="intAuthor">Odpowiadający: minister rodziny Agnieszka Dziemianowicz-Bąk</p>
<p class="intDate">Warszawa, 22-02-2024</p>
<p>Szanowny Panie Marszałku, {"odpowiadając informuję jak poniżej. " * 15}</p>
</body></html>
"""

STUB_HTML = """<!DOCTYPE html>
<html><head><title>Odpowiedź</title></head>
<body>
<h1>Odpowiedź na interpelację nr 806</h1>
<p class="int-title">w sprawie warunków sanitarnych</p>
<p class="intAuthor">Odpowiadający: sekretarz stanu Krzysztof Kukucki</p>
<p class="intDate">Warszawa, 20-02-2024</p>
<p>Treść odpowiedzi znajduje się w załączniku.</p>
<p>Załączniki</p>
<p>LUB-OMK.601.1.2024.3.pdf</p>
</body></html>
"""

ITEM = {
    "num": 1,
    "term": 10,
    "title": "Interpelacja w sprawie sytuacji w rodzinnych ogrodach działkowych",
    "receiptDate": "2023-11-15",
    "from": ["277"],
    "to": ["minister rozwoju i technologii"],
}


class SejmInterpellationsContractTest(unittest.TestCase):
    def test_should_fetch_reply_requires_key_and_html(self):
        self.assertTrue(should_fetch_reply({"key": "D2QJNB", "onlyAttachment": False}))
        self.assertFalse(should_fetch_reply({"key": "X", "onlyAttachment": True}))
        self.assertFalse(should_fetch_reply({"onlyAttachment": False}))
        self.assertFalse(should_fetch_reply({"key": None, "onlyAttachment": False}))
        self.assertFalse(should_fetch_reply({}))

    def test_question_strips_header_keeps_body_and_file_number(self):
        row = normalize_document("interpellation", ITEM, Q_HTML)
        self.assertIsNotNone(row)
        self.assertEqual(set(row), {"text", "meta"})
        self.assertNotIn("<", row["text"])
        self.assertNotIn("Interpelacja nr 1", row["text"])
        self.assertNotIn("Zgłaszający:", row["text"])
        self.assertNotIn("Data wpływu:", row["text"])
        self.assertNotIn("do ministra rozwoju", row["text"])
        self.assertIn("Ogrody działkowe", row["text"])
        self.assertIn("BPRM.4820.2.3.2020", row["text"])  # not a phone
        self.assertGreaterEqual(len(row["text"]), MIN_CHARS)
        self.assertEqual(row["meta"]["num"], 1)
        self.assertEqual(row["meta"]["term"], 10)
        self.assertEqual(row["meta"]["kind"], "interpellation")
        self.assertEqual(row["meta"]["author"], "Katarzyna Osos")
        self.assertTrue(row["meta"]["url"].endswith("/interpellations/1/body"))

    def test_reply_strips_header_and_records_key(self):
        reply = {"key": "D2QJNB", "from": "Minister Agnieszka Dziemianowicz-Bąk",
                 "receiptDate": "2024-02-22", "onlyAttachment": False}
        item = {**ITEM, "num": 8}
        row = normalize_document("interpellation_reply", item, R_HTML, reply=reply)
        self.assertIsNotNone(row)
        self.assertNotIn("Odpowiedź na interpelację nr 8", row["text"])
        self.assertNotIn("Odpowiadający:", row["text"])
        self.assertNotIn("Warszawa, 22-02-2024", row["text"])
        self.assertIn("Szanowny Panie Marszałku", row["text"])
        self.assertEqual(row["meta"]["kind"], "interpellation_reply")
        self.assertEqual(row["meta"]["reply_key"], "D2QJNB")
        self.assertIn("Dziemianowicz", row["meta"]["author"])
        self.assertTrue(row["meta"]["url"].endswith("/interpellations/8/reply/D2QJNB/body"))

    def test_attachment_stub_is_rejected(self):
        reply = {"key": "ABC", "from": "X", "receiptDate": "2024-02-20"}
        self.assertIsNone(normalize_document(
            "interpellation_reply", {**ITEM, "num": 806}, STUB_HTML, reply=reply
        ))

    def test_written_question_url(self):
        item = {**ITEM, "title": "Zapytanie w sprawie świadczeń"}
        row = normalize_document("written_question", item, Q_HTML)
        self.assertTrue(row["meta"]["url"].endswith("/writtenQuestions/1/body"))
        self.assertEqual(row["meta"]["kind"], "written_question")



class SejmInterpellationsParquetTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        if not PARQUET.is_file():
            raise unittest.SkipTest("parquet not built yet")
        import pyarrow.parquet as pq
        cls.tbl = pq.read_table(PARQUET)
        cls.stats = json.loads(STATS.read_text(encoding="utf-8"))

    def test_canonical_schema_and_uniform_source_license(self):
        self.assertEqual(self.tbl.column_names, CANON)
        src = set(self.tbl["source"].to_pylist())
        lic = set(self.tbl["license"].to_pylist())
        self.assertEqual(src, {"sejm_interpellations"})
        self.assertEqual(lic, {"public-domain (official documents)"})

    def test_nonempty_text_and_positive_tokens(self):
        texts = self.tbl["text"].to_pylist()
        toks = self.tbl["token_count"].to_pylist()
        self.assertTrue(all(t and t.strip() for t in texts))
        self.assertTrue(all(n > 0 for n in toks))

    def test_stats_match_parquet(self):
        self.assertEqual(self.stats["kept"], self.tbl.num_rows)
        self.assertEqual(self.stats["tokens"], sum(self.tbl["token_count"].to_pylist()))
        self.assertEqual(self.stats["authors_with_value"], self.tbl.num_rows)


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