-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_canary_decks.py
More file actions
302 lines (256 loc) · 7.87 KB
/
test_canary_decks.py
File metadata and controls
302 lines (256 loc) · 7.87 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
from __future__ import annotations
import json
import zipfile
from dataclasses import dataclass
from pathlib import Path
from pptx import Presentation
from pptx.enum.chart import XL_CHART_TYPE
from pptx.enum.shapes import MSO_SHAPE_TYPE
from clean_slides.cli import cmd_generate
from clean_slides.cli_render import cmd_charts
from clean_slides.pptx_access import (
iter_shapes,
presentation_chart_types,
shape_has_connector_endpoints,
shape_text_frame_text,
)
@dataclass
class _GenerateArgs:
input: list[str]
template: str | None
slide_index: int | None
keep_existing: bool
detail: bool
output: str | None
config: str | None = None
@dataclass
class _ChartsArgs:
input: str
output: str
template: str | None = None
layout: str | None = None
expected_template: str | None = None
def _run_generate(
tmp_path: Path,
name: str,
slides: list[tuple[str, str]],
) -> Path:
inputs: list[str] = []
for filename, yaml_text in slides:
yaml_path = tmp_path / filename
yaml_path.write_text(yaml_text.strip(), encoding="utf-8")
inputs.append(str(yaml_path))
output_path = tmp_path / f"{name}.pptx"
result = cmd_generate(
_GenerateArgs(
input=inputs,
template=None,
slide_index=None,
keep_existing=False,
detail=False,
output=str(output_path),
)
)
assert result == 0
assert output_path.exists()
return output_path
def _run_charts(tmp_path: Path, name: str, spec: dict[str, object]) -> Path:
input_path = tmp_path / f"{name}.json"
input_path.write_text(json.dumps(spec), encoding="utf-8")
output_path = tmp_path / f"{name}.pptx"
result = cmd_charts(_ChartsArgs(input=str(input_path), output=str(output_path)))
assert result == 0
assert output_path.exists()
return output_path
def _chart_types(path: Path) -> list[int]:
prs = Presentation(str(path))
return presentation_chart_types(prs)
def _slide_texts(path: Path, slide_index: int) -> list[str]:
prs = Presentation(str(path))
texts: list[str] = []
for shape in iter_shapes(prs.slides[slide_index]):
raw_text = shape_text_frame_text(shape)
if raw_text is None:
continue
text = raw_text.strip()
if text:
texts.append(text)
return texts
def _connector_count(path: Path, slide_index: int) -> int:
prs = Presentation(str(path))
count = 0
for shape in iter_shapes(prs.slides[slide_index]):
if shape_has_connector_endpoints(shape):
count += 1
return count
def _blank_auto_shape_count(path: Path, slide_index: int) -> int:
prs = Presentation(str(path))
count = 0
for shape in iter_shapes(prs.slides[slide_index]):
shape_type = getattr(shape, "shape_type", None)
if shape_type != MSO_SHAPE_TYPE.AUTO_SHAPE:
continue
text = shape_text_frame_text(shape)
if text is not None and text.strip():
continue
count += 1
return count
def _pptx_part(path: Path, part_name: str) -> bytes:
with zipfile.ZipFile(path) as archive:
return archive.read(part_name)
def test_canary_generate_mixed_metadata_and_table_deck(tmp_path: Path) -> None:
output_path = _run_generate(
tmp_path,
"canary-mixed-generate",
[
(
"01-title.yaml",
"""
title: Release Canary
subtitle: Stability gate
""",
),
(
"02-table.yaml",
"""
title: KPI snapshot
table:
rows: 3
cols: 2
has_col_header: true
col_headers:
-
text: CAGR
sub: "% FY26-33E"
- Notes
cells:
- [North, Stable]
- [South, Upside]
""",
),
],
)
prs = Presentation(str(output_path))
assert len(prs.slides) == 2
assert _chart_types(output_path) == []
slide2_text = "\n".join(_slide_texts(output_path, 1))
assert "North" in slide2_text
assert "South" in slide2_text
assert "CAGR" in slide2_text
def test_canary_generate_waterfall_chart_cells_with_connectors(tmp_path: Path) -> None:
output_path = _run_generate(
tmp_path,
"canary-waterfall-cells",
[
(
"waterfall-cells.yaml",
"""
title: Waterfall canary
charts:
wf:
type: waterfall
dir: horizontal
values: [954, 13, -45, 1209]
totals: [1, 4]
decreases: [3]
format: "{:,.0f}"
connector: true
table:
rows: 4
cols: 1
has_col_header: false
cells:
- [wf-1]
- [wf-2]
- [wf-3]
- [wf-4]
""",
),
],
)
assert _chart_types(output_path) == [int(XL_CHART_TYPE.BAR_STACKED)]
assert _connector_count(output_path, 0) >= 3
texts = set(_slide_texts(output_path, 0))
assert {"954", "13", "-45", "1,209"}.issubset(texts)
def test_canary_charts_cli_multi_chart_overlay_deck(tmp_path: Path) -> None:
output_path = _run_charts(
tmp_path,
"canary-charts-multi",
{
"layout": "Default",
"charts": [
{
"type": "clustered",
"categories": ["A", "B"],
"series": [{"name": "S1", "values": [10, 20], "color": "accent1"}],
"show_data_labels": True,
"bar": {"orientation": "horizontal"},
},
{
"type": "waterfall",
"categories": ["Start", "Growth", "Costs", "End"],
"series": [
{
"name": "Values",
"values": [100, 40, -20, 120],
"color": "accent1",
}
],
"show_data_labels": True,
"add_overlay_labels": True,
"waterfall": {
"orientation": "horizontal",
"total_categories": ["Start", "End"],
"decrease_categories": ["Costs"],
"connector_style": "step",
"connector_dash_style": "dot",
},
},
],
},
)
prs = Presentation(str(output_path))
assert len(prs.slides) == 2
assert _chart_types(output_path) == [
int(XL_CHART_TYPE.BAR_CLUSTERED),
int(XL_CHART_TYPE.BAR_STACKED),
]
slide2_text = set(_slide_texts(output_path, 1))
assert {"Start", "End"}.issubset(slide2_text)
# Dot connector mode should render many small line-segment rectangles.
assert _blank_auto_shape_count(output_path, 1) >= 20
def test_canary_charts_cli_template_copy_roundtrip(tmp_path: Path) -> None:
template_path = _run_charts(
tmp_path,
"canary-template-source",
{
"type": "clustered",
"categories": ["A", "B"],
"series": [{"name": "S1", "values": [1, 2], "color": "accent1"}],
"show_data_labels": True,
"data_labels": {"format": "0.00", "font_size": 18},
},
)
target_path = _run_charts(
tmp_path,
"canary-template-target",
{
"type": "clustered",
"categories": ["A", "B"],
"series": [{"name": "S1", "values": [9, 8], "color": "accent2"}],
"show_data_labels": True,
"data_labels": {"format": "0", "font_size": 10},
"bar": {
"chart_template": str(template_path),
"chart_template_copy": True,
},
},
)
assert _pptx_part(target_path, "ppt/charts/chart1.xml") == _pptx_part(
template_path,
"ppt/charts/chart1.xml",
)
assert _pptx_part(target_path, "ppt/charts/_rels/chart1.xml.rels") == _pptx_part(
template_path,
"ppt/charts/_rels/chart1.xml.rels",
)