GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — rhwp-python/tests/test_ir_schema.py at main · DanMeon/rhwp-python Expand file tree/ Copy pathtest_ir_schema.pyMore file actions 304 lines (233 loc) · 9.28 KB / Copy pathtest_ir_schema.pyFile metadata and controls304 lines (233 loc) · 9.28 KB 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 """tests/test_ir_schema.py — Stage S1 Pydantic IR 단위 테스트. ir.md §테스트 전략 §단위 테스트 항목 1:1 매핑. 통합 테스트 (샘플 HWP 왕복) 는 S2 이후, JSON Schema conformance / LLM strict-mode 테스트는 S4 이후. """ import warnings import pytest from pydantic import ValidationError from rhwp.ir.nodes import ( CURRENT_SCHEMA_VERSION, DocumentMetadata, HwpDocument, InlineRun, ParagraphBlock, Provenance, Section, TableBlock, TableCell, UnknownBlock, ) pytestmark = pytest.mark.spec("v0.2.0/ir") # ^ soft retrofit — file-level spec mapping; v0.4.0+ specs add #AC-N to specific tests (CONVENTIONS § Trace report) # * 공통 픽스처 헬퍼 def _prov(section_idx: int = 0, para_idx: int = 0) -> Provenance: return Provenance(section_idx=section_idx, para_idx=para_idx) def _paragraph(text: str = "hello", para_idx: int = 0) -> ParagraphBlock: return ParagraphBlock( text=text, inlines=[InlineRun(text=text)], prov=_prov(para_idx=para_idx), ) def _simple_table(para_idx: int = 1) -> TableBlock: return TableBlock( rows=2, cols=2, cells=[ TableCell(row=0, col=0, grid_index=0, blocks=[_paragraph("A")]), TableCell(row=0, col=1, grid_index=1, blocks=[_paragraph("B")]), TableCell(row=1, col=0, grid_index=2, blocks=[_paragraph("C")]), TableCell(row=1, col=1, grid_index=3, blocks=[_paragraph("D")]), ], html="<table><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></table>", text="A\tB\nC\tD", prov=_prov(para_idx=para_idx), ) # * Test 1 — HwpDocument 왕복 def test_hwp_document_roundtrip(): doc = HwpDocument( metadata=DocumentMetadata(title="test", author="DanMeon"), sections=[Section(section_idx=0), Section(section_idx=1)], body=[_paragraph("first"), _simple_table()], ) dumped = doc.model_dump_json() reloaded = HwpDocument.model_validate_json(dumped) assert reloaded == doc assert reloaded.schema_name == "HwpDocument" assert reloaded.schema_version == CURRENT_SCHEMA_VERSION # * Test 2 — ParagraphBlock 왕복 def test_paragraph_block_roundtrip(): p = ParagraphBlock( text="hello world", inlines=[ InlineRun(text="hello", bold=True), InlineRun(text=" "), InlineRun(text="world", italic=True, href="https://example.com"), ], prov=_prov(), ) reloaded = ParagraphBlock.model_validate_json(p.model_dump_json()) assert reloaded == p assert reloaded.inlines[0].bold is True assert reloaded.inlines[2].href == "https://example.com" # * Test 3 — TableBlock 단순 왕복 def test_table_block_simple_roundtrip(): t = _simple_table() assert TableBlock.model_validate_json(t.model_dump_json()) == t # * Test 4 — 3단 중첩: TableCell → Block → TableCell def test_table_nested_three_levels(): innermost = TableBlock( rows=1, cols=1, cells=[TableCell(row=0, col=0, grid_index=0, blocks=[_paragraph("deep")])], prov=_prov(para_idx=3), ) middle = TableBlock( rows=1, cols=1, cells=[TableCell(row=0, col=0, grid_index=0, blocks=[innermost])], prov=_prov(para_idx=2), ) outer = TableBlock( rows=1, cols=1, cells=[TableCell(row=0, col=0, grid_index=0, blocks=[middle])], prov=_prov(para_idx=1), ) doc = HwpDocument(body=[outer]) reloaded = HwpDocument.model_validate_json(doc.model_dump_json()) assert reloaded == doc # * 재귀 타입이 보존되는지 직접 확인 — isinstance narrowing 동작 outer_blk = reloaded.body[0] assert isinstance(outer_blk, TableBlock) middle_blk = outer_blk.cells[0].blocks[0] assert isinstance(middle_blk, TableBlock) innermost_blk = middle_blk.cells[0].blocks[0] assert isinstance(innermost_blk, TableBlock) leaf_para = innermost_blk.cells[0].blocks[0] assert isinstance(leaf_para, ParagraphBlock) assert leaf_para.text == "deep" # * Test 5 — 미지의 kind 는 UnknownBlock 으로 라우팅 def test_discriminator_routes_unknown_kind(): raw = { # ^ v0.3.0 S3 시점 known: paragraph/table/picture/formula/footnote/endnote/ # list_item/caption/toc/field. 새 미지 kind 후보로 v0.4.0+ 가설적 변형 사용. "kind": "revision_mark", "prov": {"section_idx": 0, "para_idx": 0}, "level": 2, # ^ extra="allow" 로 payload 보존 확인 } doc = HwpDocument.model_validate({"body": [raw]}) blk = doc.body[0] assert isinstance(blk, UnknownBlock) assert blk.kind == "revision_mark" # ^ extra="allow" — 임의 필드 보존 assert blk.model_extra == {"level": 2} # * Test 6 — 알려진 kind 는 해당 Block 으로 라우팅 def test_discriminator_routes_known_kinds(): para_raw = { "kind": "paragraph", "text": "p", "inlines": [{"text": "p"}], "prov": {"section_idx": 0, "para_idx": 0}, } tbl_raw = { "kind": "table", "rows": 1, "cols": 1, "cells": [], "prov": {"section_idx": 0, "para_idx": 1}, } doc = HwpDocument.model_validate({"body": [para_raw, tbl_raw]}) assert isinstance(doc.body[0], ParagraphBlock) assert isinstance(doc.body[1], TableBlock) # * Test 7 — extra=forbid (UnknownBlock 제외) @pytest.mark.parametrize( "model_cls, base_data", [ (Provenance, {"section_idx": 0, "para_idx": 0}), (InlineRun, {"text": "x"}), (DocumentMetadata, {}), (Section, {"section_idx": 0}), (ParagraphBlock, {"prov": {"section_idx": 0, "para_idx": 0}}), ( TableBlock, {"rows": 1, "cols": 1, "prov": {"section_idx": 0, "para_idx": 0}}, ), ( TableCell, {"row": 0, "col": 0, "grid_index": 0}, ), (HwpDocument, {}), ], ) def test_extra_forbid_raises_on_unknown_field(model_cls, base_data): with pytest.raises(ValidationError): model_cls.model_validate({**base_data, "definitely_not_a_field": 1}) # * Test 8 — frozen=True 로 직접 변경 차단 def test_frozen_raises_on_mutation(): p = _paragraph() with pytest.raises(ValidationError): p.text = "mutated" # type: ignore[misc] def test_frozen_unknown_block_cannot_be_mutated(): """UnknownBlock 은 extra="allow" 지만 frozen 이므로 여전히 불변.""" u = UnknownBlock(kind="custom", prov=_prov()) with pytest.raises(ValidationError): u.kind = "other" # type: ignore[misc] # * Test 9/10 — schema_version pattern @pytest.mark.parametrize("ver", ["1.0", "1.1", "2.0.3", "10.20.30"]) def test_schema_version_accepts_valid(ver): # ^ major > current 는 UserWarning 나지만 validation 통과 with warnings.catch_warnings(): warnings.simplefilter("ignore", UserWarning) d = HwpDocument(schema_version=ver) assert d.schema_version == ver @pytest.mark.parametrize("ver", ["banana", "", "1", "1.", ".1", "1.0.0.0", "v1.0"]) def test_schema_version_rejects_invalid(ver): with pytest.raises(ValidationError): HwpDocument(schema_version=ver) # * Test 11 — major 상향 시 UserWarning def test_schema_version_warns_on_future_major(): with pytest.warns(UserWarning, match="newer than supported"): d = HwpDocument(schema_version="2.0") assert d.schema_version == "2.0" def test_schema_version_minor_bump_does_not_warn(): """같은 major 안의 minor 상향은 warning 없음.""" with warnings.catch_warnings(): warnings.simplefilter("error", UserWarning) # ^ 경고가 나면 error 로 raise — warning 없으면 정상 통과 d = HwpDocument(schema_version="1.5") assert d.schema_version == "1.5" # * Test 12 — UnknownBlock 은 임의 kind 수용 @pytest.mark.parametrize("k", ["custom_x", "hwp_field", "revision_mark", "side_note", "highlight"]) def test_unknown_block_preserves_arbitrary_kind(k): """v0.3.0 S1-S3 시점 known kinds (paragraph/table/picture/formula/footnote/endnote/ list_item/caption/toc/field) 외의 가설적 미래 변형이 UnknownBlock 으로 라우팅.""" u = UnknownBlock(kind=k, prov=_prov()) assert u.kind == k reloaded = UnknownBlock.model_validate_json(u.model_dump_json()) assert reloaded.kind == k # * Test 13 — Provenance codepoint offset 은 Python str slicing 과 호환 def test_provenance_char_offsets_are_codepoint_based(): """이모지 (SMP, UTF-16 surrogate pair) 포함 텍스트에서 offset 이 올바른지.""" text = "회의록 🙂 요약" target = "요약" start = text.index(target) # ^ Python str.index 는 codepoint 인덱스 prov = Provenance( section_idx=0, para_idx=0, char_start=start, char_end=start + len(target), ) assert prov.char_start is not None assert prov.char_end is not None assert text[prov.char_start : prov.char_end] == target # * Test 14 — InlineRun 기본값 def test_inline_run_defaults(): r = InlineRun(text="x") assert r.text == "x" assert r.bold is False assert r.italic is False assert r.underline is False assert r.strikethrough is False assert r.href is None assert r.ruby is None assert r.raw_style_id is None |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |