|
| 1 | +import pandas as pd |
| 2 | +import pytest |
| 3 | + |
| 4 | +from great_tables import GT |
| 5 | +from great_tables._gt_data import Body, GroupRowInfo, GroupRows, Stub, RowInfo |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def grouped_gt(): |
| 10 | + """A GT table with a groupname column and two groups.""" |
| 11 | + df = pd.DataFrame( |
| 12 | + { |
| 13 | + "group": ["A", "A", "B", "B"], |
| 14 | + "value": [1, 2, 3, 4], |
| 15 | + } |
| 16 | + ) |
| 17 | + return GT(df, groupname_col="group") |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def numeric_grouped_gt(): |
| 22 | + """A GT table with a numeric groupname column and two groups.""" |
| 23 | + df = pd.DataFrame( |
| 24 | + { |
| 25 | + "group": [1000, 1000, 2000, 2000], |
| 26 | + "value": [1, 2, 3, 4], |
| 27 | + } |
| 28 | + ) |
| 29 | + return GT(df, groupname_col="group") |
| 30 | + |
| 31 | + |
| 32 | +def test_update_group_row_labels_uses_formatted_value(numeric_grouped_gt: GT): |
| 33 | + """When a formatter has been applied, group labels should reflect the formatted value.""" |
| 34 | + gt_fmt = numeric_grouped_gt.fmt_number(columns="group", decimals=0) |
| 35 | + |
| 36 | + built = gt_fmt._render_formats("html") |
| 37 | + labels = [gr.defaulted_label() for gr in built._stub.group_rows] |
| 38 | + |
| 39 | + assert labels == ["1,000", "2,000"] |
| 40 | + |
| 41 | + |
| 42 | +def test_update_group_row_labels_falls_back_to_original(grouped_gt: GT): |
| 43 | + """When no formatter is applied, group labels should use the original data value.""" |
| 44 | + built = grouped_gt._render_formats("html") |
| 45 | + labels = [gr.defaulted_label() for gr in built._stub.group_rows] |
| 46 | + |
| 47 | + assert labels == ["A", "B"] |
| 48 | + |
| 49 | + |
| 50 | +def test_update_group_row_labels_no_groups(): |
| 51 | + """When there is no groupname column, the stub is returned unchanged.""" |
| 52 | + df = pd.DataFrame({"value": [1, 2, 3]}) |
| 53 | + gt_tbl = GT(df) |
| 54 | + |
| 55 | + built = gt_tbl._render_formats("html") |
| 56 | + |
| 57 | + # No group rows should exist |
| 58 | + assert len(built._stub.group_rows) == 0 |
| 59 | + |
| 60 | + |
| 61 | +def test_formatted_group_label_in_html(numeric_grouped_gt: GT): |
| 62 | + """Formatted group labels should appear in the rendered HTML output.""" |
| 63 | + html = numeric_grouped_gt.fmt_number(columns="group", decimals=0).as_raw_html() |
| 64 | + |
| 65 | + assert ">1,000</th>" in html |
| 66 | + assert ">2,000</th>" in html |
| 67 | + |
| 68 | + |
| 69 | +def test_markdown_link_in_group_label_renders_as_anchor(): |
| 70 | + """Markdown links in group labels should render as <a> tags in HTML output.""" |
| 71 | + df = pd.DataFrame( |
| 72 | + { |
| 73 | + "group": [ |
| 74 | + "[Google](https://google.com)", |
| 75 | + "[Google](https://google.com)", |
| 76 | + "[GitHub](https://github.com)", |
| 77 | + "[GitHub](https://github.com)", |
| 78 | + ], |
| 79 | + "value": [1, 2, 3, 4], |
| 80 | + } |
| 81 | + ) |
| 82 | + html = GT(df, groupname_col="group").fmt_markdown(columns="group").as_raw_html() |
| 83 | + |
| 84 | + assert '<a href="https://google.com">Google</a>' in html |
| 85 | + assert '<a href="https://github.com">GitHub</a>' in html |
0 commit comments