|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pandas import ( |
| 6 | + DataFrame, |
| 7 | + Series, |
| 8 | +) |
| 9 | + |
| 10 | +pytest.importorskip("jinja2") |
| 11 | +from pandas.io.formats.style import Styler |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def df(): |
| 16 | + return DataFrame( |
| 17 | + {"A": [0, 1], "B": [-0.61, -1.22], "C": Series(["ab", "cd"], dtype=object)} |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def styler(df): |
| 23 | + return Styler(df, uuid_len=0, precision=2) |
| 24 | + |
| 25 | + |
| 26 | +def test_basic_table(styler): |
| 27 | + result = styler.to_typst() |
| 28 | + expected = dedent( |
| 29 | + """\ |
| 30 | + #table( |
| 31 | + columns: 4, |
| 32 | + [], [A], [B], [C], |
| 33 | +
|
| 34 | + [0], [0], [-0.61], [ab], |
| 35 | + [1], [1], [-1.22], [cd], |
| 36 | + )""" |
| 37 | + ) |
| 38 | + assert result == expected |
| 39 | + |
| 40 | + |
| 41 | +def test_concat(styler): |
| 42 | + result = styler.concat(styler.data.agg(["sum"]).style).to_typst() |
| 43 | + expected = dedent( |
| 44 | + """\ |
| 45 | + #table( |
| 46 | + columns: 4, |
| 47 | + [], [A], [B], [C], |
| 48 | +
|
| 49 | + [0], [0], [-0.61], [ab], |
| 50 | + [1], [1], [-1.22], [cd], |
| 51 | + [sum], [1], [-1.830000], [abcd], |
| 52 | + )""" |
| 53 | + ) |
| 54 | + assert result == expected |
| 55 | + |
| 56 | + |
| 57 | +def test_concat_recursion(styler): |
| 58 | + df = styler.data |
| 59 | + styler1 = styler |
| 60 | + styler2 = Styler(df.agg(["sum"]), uuid_len=0, precision=3) |
| 61 | + styler3 = Styler(df.agg(["sum"]), uuid_len=0, precision=4) |
| 62 | + result = styler1.concat(styler2.concat(styler3)).to_typst() |
| 63 | + expected = dedent( |
| 64 | + """\ |
| 65 | + #table( |
| 66 | + columns: 4, |
| 67 | + [], [A], [B], [C], |
| 68 | +
|
| 69 | + [0], [0], [-0.61], [ab], |
| 70 | + [1], [1], [-1.22], [cd], |
| 71 | + [sum], [1], [-1.830], [abcd], |
| 72 | + [sum], [1], [-1.8300], [abcd], |
| 73 | + )""" |
| 74 | + ) |
| 75 | + assert result == expected |
| 76 | + |
| 77 | + |
| 78 | +def test_concat_chain(styler): |
| 79 | + df = styler.data |
| 80 | + styler1 = styler |
| 81 | + styler2 = Styler(df.agg(["sum"]), uuid_len=0, precision=3) |
| 82 | + styler3 = Styler(df.agg(["sum"]), uuid_len=0, precision=4) |
| 83 | + result = styler1.concat(styler2).concat(styler3).to_typst() |
| 84 | + expected = dedent( |
| 85 | + """\ |
| 86 | + #table( |
| 87 | + columns: 4, |
| 88 | + [], [A], [B], [C], |
| 89 | +
|
| 90 | + [0], [0], [-0.61], [ab], |
| 91 | + [1], [1], [-1.22], [cd], |
| 92 | + [sum], [1], [-1.830], [abcd], |
| 93 | + [sum], [1], [-1.8300], [abcd], |
| 94 | + )""" |
| 95 | + ) |
| 96 | + assert result == expected |
0 commit comments