|
| 1 | +# pyright: reportPrivateUsage=false |
| 2 | + |
| 3 | +"""Unit-test suite for Tables 2.0 Phase 4 — sizing & ergonomics, closing the epic. |
| 4 | +
|
| 5 | +Covers: |
| 6 | +
|
| 7 | +- Read-only count properties on |Table|: ``row_count``, ``column_count``, |
| 8 | + ``dimensions`` — convenience accessors that don't instantiate the |
| 9 | + full |_RowCollection| / |_ColumnCollection|. |
| 10 | +- Per-row height and per-column width round-trip preservation through |
| 11 | + save/reload (regression-lock — the underlying setters already work, |
| 12 | + but no test pinned that down before now). |
| 13 | +- Anti-criteria: Phase 1/2/3 surfaces unaffected. |
| 14 | +
|
| 15 | +Issue: https://github.com/MHoroszowski/python-pptx/issues/12 (Phase 4, closing PR). |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import io |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from pptx import Presentation |
| 25 | +from pptx.util import Emu, Inches |
| 26 | + |
| 27 | +# --------------------------------------------------------------------------- |
| 28 | +# Fixtures |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | + |
| 31 | + |
| 32 | +def _make_table(rows: int, cols: int): |
| 33 | + prs = Presentation() |
| 34 | + slide = prs.slides.add_slide(prs.slide_layouts[6]) |
| 35 | + gf = slide.shapes.add_table(rows, cols, Inches(1), Inches(1), Inches(6), Inches(2)) |
| 36 | + return prs, gf.table |
| 37 | + |
| 38 | + |
| 39 | +@pytest.fixture |
| 40 | +def t3x4(): |
| 41 | + _, t = _make_table(3, 4) |
| 42 | + return t |
| 43 | + |
| 44 | + |
| 45 | +# --------------------------------------------------------------------------- |
| 46 | +# Table.row_count / column_count / dimensions |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | + |
| 49 | + |
| 50 | +class DescribeTable_CountProperties(object): |
| 51 | + """Unit-test suite for `row_count`, `column_count`, `dimensions`.""" |
| 52 | + |
| 53 | + def it_reports_row_count_for_a_3x4_table(self, t3x4): |
| 54 | + assert t3x4.row_count == 3 |
| 55 | + |
| 56 | + def it_reports_column_count_for_a_3x4_table(self, t3x4): |
| 57 | + assert t3x4.column_count == 4 |
| 58 | + |
| 59 | + def it_reports_dimensions_as_rows_cols_tuple(self, t3x4): |
| 60 | + assert t3x4.dimensions == (3, 4) |
| 61 | + |
| 62 | + def it_matches_len_of_rows_and_columns_collections(self, t3x4): |
| 63 | + assert t3x4.row_count == len(t3x4.rows) |
| 64 | + assert t3x4.column_count == len(t3x4.columns) |
| 65 | + |
| 66 | + def it_increments_row_count_after_rows_add(self, t3x4): |
| 67 | + t3x4.rows.add() |
| 68 | + assert t3x4.row_count == 4 |
| 69 | + assert t3x4.dimensions == (4, 4) |
| 70 | + |
| 71 | + def it_decrements_row_count_after_rows_remove(self, t3x4): |
| 72 | + t3x4.rows.remove(1) |
| 73 | + assert t3x4.row_count == 2 |
| 74 | + assert t3x4.dimensions == (2, 4) |
| 75 | + |
| 76 | + def it_increments_column_count_after_columns_add(self, t3x4): |
| 77 | + t3x4.columns.add() |
| 78 | + assert t3x4.column_count == 5 |
| 79 | + assert t3x4.dimensions == (3, 5) |
| 80 | + |
| 81 | + def it_decrements_column_count_after_columns_remove(self, t3x4): |
| 82 | + t3x4.columns.remove(0) |
| 83 | + assert t3x4.column_count == 3 |
| 84 | + assert t3x4.dimensions == (3, 3) |
| 85 | + |
| 86 | + @pytest.mark.parametrize("attr", ["row_count", "column_count", "dimensions"]) |
| 87 | + def it_is_read_only_no_setter(self, t3x4, attr): |
| 88 | + with pytest.raises(AttributeError): |
| 89 | + setattr(t3x4, attr, 99) |
| 90 | + |
| 91 | + |
| 92 | +# --------------------------------------------------------------------------- |
| 93 | +# Per-row height / per-column width — round-trip regression-lock |
| 94 | +# --------------------------------------------------------------------------- |
| 95 | + |
| 96 | + |
| 97 | +class DescribeSizing_RoundTrip(object): |
| 98 | + """Save → reload preserves explicit row heights and column widths.""" |
| 99 | + |
| 100 | + def it_preserves_a_single_row_height_through_save_and_reload(self): |
| 101 | + prs, t = _make_table(3, 3) |
| 102 | + t.rows[0].height = Emu(500000) |
| 103 | + |
| 104 | + buf = io.BytesIO() |
| 105 | + prs.save(buf) |
| 106 | + buf.seek(0) |
| 107 | + |
| 108 | + prs2 = Presentation(buf) |
| 109 | + t2 = next(shp for shp in prs2.slides[0].shapes if shp.has_table).table |
| 110 | + assert t2.rows[0].height == 500000 |
| 111 | + |
| 112 | + def it_preserves_a_single_column_width_through_save_and_reload(self): |
| 113 | + prs, t = _make_table(3, 3) |
| 114 | + t.columns[1].width = Emu(1000000) |
| 115 | + |
| 116 | + buf = io.BytesIO() |
| 117 | + prs.save(buf) |
| 118 | + buf.seek(0) |
| 119 | + |
| 120 | + prs2 = Presentation(buf) |
| 121 | + t2 = next(shp for shp in prs2.slides[0].shapes if shp.has_table).table |
| 122 | + assert t2.columns[1].width == 1000000 |
| 123 | + |
| 124 | + def it_preserves_mixed_row_heights(self): |
| 125 | + prs, t = _make_table(3, 3) |
| 126 | + heights = [Emu(300000), Emu(600000), Emu(900000)] |
| 127 | + for idx, h in enumerate(heights): |
| 128 | + t.rows[idx].height = h |
| 129 | + |
| 130 | + buf = io.BytesIO() |
| 131 | + prs.save(buf) |
| 132 | + buf.seek(0) |
| 133 | + |
| 134 | + prs2 = Presentation(buf) |
| 135 | + t2 = next(shp for shp in prs2.slides[0].shapes if shp.has_table).table |
| 136 | + assert [t2.rows[i].height for i in range(3)] == [300000, 600000, 900000] |
| 137 | + |
| 138 | + def it_preserves_mixed_column_widths(self): |
| 139 | + prs, t = _make_table(2, 4) |
| 140 | + widths = [Emu(400000), Emu(800000), Emu(1200000), Emu(1600000)] |
| 141 | + for idx, w in enumerate(widths): |
| 142 | + t.columns[idx].width = w |
| 143 | + |
| 144 | + buf = io.BytesIO() |
| 145 | + prs.save(buf) |
| 146 | + buf.seek(0) |
| 147 | + |
| 148 | + prs2 = Presentation(buf) |
| 149 | + t2 = next(shp for shp in prs2.slides[0].shapes if shp.has_table).table |
| 150 | + assert [t2.columns[i].width for i in range(4)] == [400000, 800000, 1200000, 1600000] |
| 151 | + |
| 152 | + def it_preserves_count_properties_through_round_trip(self): |
| 153 | + prs, t = _make_table(4, 5) |
| 154 | + |
| 155 | + buf = io.BytesIO() |
| 156 | + prs.save(buf) |
| 157 | + buf.seek(0) |
| 158 | + |
| 159 | + prs2 = Presentation(buf) |
| 160 | + t2 = next(shp for shp in prs2.slides[0].shapes if shp.has_table).table |
| 161 | + assert t2.row_count == 4 |
| 162 | + assert t2.column_count == 5 |
| 163 | + assert t2.dimensions == (4, 5) |
| 164 | + |
| 165 | + |
| 166 | +# --------------------------------------------------------------------------- |
| 167 | +# Anti / Regression |
| 168 | +# --------------------------------------------------------------------------- |
| 169 | + |
| 170 | + |
| 171 | +class DescribePhase4_Regression(object): |
| 172 | + """Anti-criteria: existing surfaces unaffected by Phase 4 additions.""" |
| 173 | + |
| 174 | + def it_keeps_phase1_rows_add_remove_working(self, t3x4): |
| 175 | + t3x4.rows.add() |
| 176 | + assert t3x4.row_count == 4 |
| 177 | + t3x4.rows.remove(0) |
| 178 | + assert t3x4.row_count == 3 |
| 179 | + |
| 180 | + def it_keeps_phase1_columns_add_remove_working(self, t3x4): |
| 181 | + t3x4.columns.add() |
| 182 | + assert t3x4.column_count == 5 |
| 183 | + t3x4.columns.remove(0) |
| 184 | + assert t3x4.column_count == 4 |
| 185 | + |
| 186 | + def it_keeps_phase2_style_api_working(self, t3x4): |
| 187 | + assert t3x4.style_id == "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}" |
| 188 | + t3x4.apply_style("No Style, No Grid") |
| 189 | + assert t3x4.style_name == "No Style, No Grid" |
| 190 | + |
| 191 | + def it_keeps_phase3_merge_api_working(self, t3x4): |
| 192 | + t3x4.merge_cells((0, 1), (0, 2)) |
| 193 | + assert t3x4.cell(0, 0).grid_span == 3 |
| 194 | + assert t3x4.cell(0, 0).row_span == 2 |
| 195 | + t3x4.split_cells((0, 1), (0, 2)) |
| 196 | + assert t3x4.cell(0, 0).grid_span == 1 |
| 197 | + |
| 198 | + def it_keeps_existing_Row_height_setter_working(self, t3x4): |
| 199 | + t3x4.rows[0].height = Emu(500000) |
| 200 | + assert t3x4.rows[0].height == 500000 |
| 201 | + |
| 202 | + def it_keeps_existing_Column_width_setter_working(self, t3x4): |
| 203 | + t3x4.columns[0].width = Emu(800000) |
| 204 | + assert t3x4.columns[0].width == 800000 |
0 commit comments