GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — rhwp-python/tests/test_ir_picture.py at main · DanMeon/rhwp-python

Expand file tree

/

test_ir_picture.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
294 lines (223 loc) · 9.67 KB
/

test_ir_picture.py

Copy path

File metadata and controls

294 lines (223 loc) · 9.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""tests/test_ir_picture.py — Stage S1 PictureBlock + ImageRef + bytes_for_image.
ir-expansion.md §S1 테스트 항목 매핑:
- PictureBlock + ImageRef 직렬화 왕복 — 모델 단독 검증
- mapper 의 bin:// URI 합성 / mime 매핑 / broken reference 폴백
- ``Document.bytes_for_image`` 헬퍼 — bin_data_id 파싱, broken reference,
unsupported scheme, lookup 실패 케이스
- 실제 샘플에 picture 가 있다면 lookup 성공까지 검증 (lenient — 샘플에 없으면 skip)
"""
import pytest
import rhwp
from pydantic import ValidationError
from rhwp.ir._mapper import _build_picture_block, _mime_for_extension
from rhwp.ir._raw_types import RawImageRef, RawPicture
from rhwp.ir.nodes import (
HwpDocument,
ImageRef,
PictureBlock,
Provenance,
)
pytestmark = pytest.mark.spec("v0.3.0/ir-expansion")
# ^ soft retrofit — file-level spec mapping; v0.4.0+ specs add #AC-N to specific tests (CONVENTIONS § Trace report)
# * 모델 단독 — ImageRef
def test_image_ref_roundtrip():
img = ImageRef(uri="bin://1", mime_type="image/png", width=640, height=480, dpi=96)
assert ImageRef.model_validate_json(img.model_dump_json()) == img
def test_image_ref_required_fields():
"""uri / mime_type 만 필수 — 나머지는 None 폴백."""
img = ImageRef(uri="bin://1", mime_type="image/jpeg")
assert img.width is None
assert img.height is None
assert img.dpi is None
def test_image_ref_extra_forbidden():
with pytest.raises(ValidationError):
ImageRef.model_validate({"uri": "bin://1", "mime_type": "image/png", "extra": True})
def test_image_ref_frozen():
img = ImageRef(uri="bin://1", mime_type="image/png")
with pytest.raises(ValidationError):
img.uri = "bin://2" # type: ignore[misc]
# * 모델 단독 — PictureBlock
def _prov(section_idx: int = 0, para_idx: int = 0) -> Provenance:
return Provenance(section_idx=section_idx, para_idx=para_idx)
def test_picture_block_roundtrip_with_image():
pic = PictureBlock(
image=ImageRef(uri="bin://7", mime_type="image/png"),
description="회로도",
prov=_prov(),
)
reloaded = PictureBlock.model_validate_json(pic.model_dump_json())
assert reloaded == pic
assert reloaded.image is not None
assert reloaded.image.uri == "bin://7"
def test_picture_block_broken_reference():
"""image=None 은 명시적 broken reference 표현 — 왕복도 보존."""
pic = PictureBlock(image=None, description=None, prov=_prov())
reloaded = PictureBlock.model_validate_json(pic.model_dump_json())
assert reloaded == pic
assert reloaded.image is None
def test_picture_block_kind_is_picture():
pic = PictureBlock(prov=_prov())
assert pic.kind == "picture"
def test_picture_block_extra_forbidden():
with pytest.raises(ValidationError):
PictureBlock.model_validate(
{"kind": "picture", "prov": {"section_idx": 0, "para_idx": 0}, "extra": "x"}
)
def test_picture_block_routes_via_discriminator():
"""본문에 PictureBlock dict 를 넣으면 picture variant 로 라우팅."""
raw = {
"kind": "picture",
"image": {"uri": "bin://3", "mime_type": "image/jpeg"},
"prov": {"section_idx": 0, "para_idx": 5},
}
doc = HwpDocument.model_validate({"body": [raw]})
blk = doc.body[0]
assert isinstance(blk, PictureBlock)
assert blk.image is not None
assert blk.image.mime_type == "image/jpeg"
# * mapper — mime 매핑
@pytest.mark.parametrize(
"ext,expected",
[
("png", "image/png"),
("PNG", "image/png"), # ^ 대소문자 무시
("jpg", "image/jpeg"),
("jpeg", "image/jpeg"),
("bmp", "image/bmp"),
("gif", "image/gif"),
("tiff", "image/tiff"),
("tif", "image/tiff"),
("wmf", "image/x-wmf"),
("emf", "image/x-emf"),
("svg", "image/svg+xml"),
("webp", "image/webp"),
],
)
def test_mime_mapping_known_extensions(ext: str, expected: str):
assert _mime_for_extension(ext) == expected
def test_mime_mapping_unknown_falls_back():
assert _mime_for_extension("xyz") == "application/octet-stream"
def test_mime_mapping_none_falls_back():
assert _mime_for_extension(None) == "application/octet-stream"
# * mapper — RawPicture → PictureBlock
def _raw_picture(
*,
section_idx: int = 0,
para_idx: int = 0,
bin_data_id: int = 1,
extension: str | None = "png",
has_content: bool = True,
description: str | None = None,
image: bool = True,
char_offset: int | None = None,
) -> RawPicture:
img: RawImageRef | None = None
if image:
img = RawImageRef(bin_data_id=bin_data_id, extension=extension, has_content=has_content)
return RawPicture(
section_idx=section_idx,
para_idx=para_idx,
image=img,
description=description,
caption=None,
char_offset=char_offset,
)
def test_build_picture_block_with_known_extension():
blk = _build_picture_block(_raw_picture(bin_data_id=4, extension="png"))
assert blk.image is not None
assert blk.image.uri == "bin://4"
assert blk.image.mime_type == "image/png"
assert blk.prov.section_idx == 0
assert blk.prov.para_idx == 0
assert blk.prov.char_start is None
assert blk.prov.char_end is None
def test_build_picture_block_unknown_extension_falls_back():
blk = _build_picture_block(_raw_picture(extension="xyz"))
assert blk.image is not None
assert blk.image.mime_type == "application/octet-stream"
def test_build_picture_block_no_extension_falls_back():
blk = _build_picture_block(_raw_picture(extension=None))
assert blk.image is not None
assert blk.image.mime_type == "application/octet-stream"
def test_build_picture_block_broken_reference_routes_to_none():
blk = _build_picture_block(_raw_picture(image=False))
assert blk.image is None
def test_build_picture_block_preserves_description():
blk = _build_picture_block(_raw_picture(description="<그림 1> 회로도"))
assert blk.description == "<그림 1> 회로도"
# * Document.bytes_for_image — 에러 경로 (broken/scheme/parse)
def test_bytes_for_image_raises_on_broken_reference(parsed_hwp: rhwp.Document):
"""image=None 인 PictureBlock 은 ValueError."""
pic = PictureBlock(image=None, prov=_prov())
with pytest.raises(ValueError, match="broken reference"):
parsed_hwp.bytes_for_image(pic)
def test_bytes_for_image_raises_on_unsupported_scheme(parsed_hwp: rhwp.Document):
pic = PictureBlock(
image=ImageRef(uri="data:image/png;base64,iVBOR=", mime_type="image/png"),
prov=_prov(),
)
with pytest.raises(ValueError, match="bin://"):
parsed_hwp.bytes_for_image(pic)
def test_bytes_for_image_raises_on_invalid_uri(parsed_hwp: rhwp.Document):
pic = PictureBlock(
image=ImageRef(uri="bin://not_a_number", mime_type="image/png"), prov=_prov()
)
with pytest.raises(ValueError, match="invalid bin://"):
parsed_hwp.bytes_for_image(pic)
def test_bytes_for_image_raises_on_out_of_range(parsed_hwp: rhwp.Document):
pic = PictureBlock(
image=ImageRef(uri="bin://99999", mime_type="image/png"), # ^ u16 초과
prov=_prov(),
)
with pytest.raises(ValueError, match="out of u16 range"):
parsed_hwp.bytes_for_image(pic)
def test_bytes_for_image_raises_on_lookup_miss(parsed_hwp: rhwp.Document):
"""존재하지 않는 bin_data_id (u16 범위 안) 도 ValueError."""
pic = PictureBlock(
image=ImageRef(uri="bin://65000", mime_type="image/png"),
prov=_prov(),
)
with pytest.raises(ValueError, match="not found"):
parsed_hwp.bytes_for_image(pic)
# * 실제 샘플 — 이미지가 있으면 lookup 성공, 없으면 skip
def _find_pictures(ir: HwpDocument) -> list[PictureBlock]:
return [b for b in ir.iter_blocks(scope="all", recurse=True) if isinstance(b, PictureBlock)]
def test_real_sample_picture_block_kind_is_picture(parsed_hwp: rhwp.Document):
"""샘플에 그림이 있을 경우 PictureBlock 으로 노출, 없으면 skip."""
ir = parsed_hwp.to_ir()
pictures = _find_pictures(ir)
if not pictures:
pytest.skip("aift.hwp 샘플에 그림 컨트롤 없음 — 검증 항목 없음")
for pic in pictures:
assert pic.kind == "picture"
assert isinstance(pic.prov, Provenance)
def test_real_sample_picture_uri_is_bin_scheme(parsed_hwp: rhwp.Document):
ir = parsed_hwp.to_ir()
pictures_with_image = [p for p in _find_pictures(ir) if p.image is not None]
if not pictures_with_image:
pytest.skip("샘플에 image=None 이 아닌 PictureBlock 없음")
for pic in pictures_with_image:
assert pic.image is not None
assert pic.image.uri.startswith("bin://"), pic.image.uri
# ^ bin_data_id 파싱 가능
bid = int(pic.image.uri[len("bin://") :])
assert bid >= 1
def test_real_sample_bytes_for_image_returns_nonempty(parsed_hwp: rhwp.Document):
"""샘플의 PictureBlock 중 lookup 가능한 것이 있으면 bytes 가 비어있지 않아야 함."""
ir = parsed_hwp.to_ir()
candidates = [p for p in _find_pictures(ir) if p.image is not None]
if not candidates:
pytest.skip("샘플에 lookup 가능한 PictureBlock 없음")
found_any = False
for pic in candidates:
try:
data = parsed_hwp.bytes_for_image(pic)
except ValueError:
# ^ 특정 picture 는 Link/Storage 라 lookup 실패할 수 있음 — 다음 후보로
continue
assert isinstance(data, bytes)
assert len(data) > 0
found_any = True
break
if not found_any:
pytest.skip("모든 PictureBlock 이 Embedding 이 아니거나 content 누락")
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.