Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance test coverage for column selection #325

Merged
merged 1 commit into from
May 3, 2024

Conversation

jrycw
Copy link
Contributor

@jrycw jrycw commented May 2, 2024

Related to #35.

This PR aims to enhance test coverage for cols_move, cols_move_to_start, cols_move_to_end, and cols_hide functions in both Pandas and Polars.

Meanwhile, given the substantial reliance on column selection within the fmt_* functions, should we conduct thorough tests for them using Polars selectors, or are we satisfied with the current state?

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.43%. Comparing base (aa14ccf) to head (e1256f4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #325      +/-   ##
==========================================
+ Coverage   82.87%   83.43%   +0.55%     
==========================================
  Files          41       41              
  Lines        4287     4287              
==========================================
+ Hits         3553     3577      +24     
+ Misses        734      710      -24     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@machow machow self-requested a review May 2, 2024 13:08
Copy link
Collaborator

@machow machow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. These tests are so helpful! I left a question about potentially changing df_lib to be a fixture. WDYT?

@@ -231,3 +197,75 @@ def test_cols_width_fully_set_pct_2():
assert gt_tbl._boxhead[0].column_width == "10%"
assert gt_tbl._boxhead[1].column_width == "10%"
assert gt_tbl._boxhead[2].column_width == "40%"


@pytest.mark.parametrize("df_lib, columns", [(pd, "a"), (pl, cs.starts_with("a"))])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of making df_lib a fixture, that passes in the dataframe directly? Similar to this fixture in tbl_data tests

https://github.com/posit-dev/great-tables/blob/main/tests/test_tbl_data.py#L19-L25

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm mistaken, but it appears that the current test case will generate two small test cases for each test function. However, if we do:

params_frames = [pytest.param(pd.DataFrame, id="pandas"), pytest.param(pl.DataFrame, id="polars")]


@pytest.fixture(params=params_frames, scope="function")
def df(request):
    return request.param({"a": [1, 2], "b": [3, 4], "c": [5, 6], "d": [7, 8]})


@pytest.mark.parametrize("columns", ["a", cs.starts_with("a")])
def test_cols_move_single_col(df, columns):
    src_gt = GT(df)
    new_gt = cols_move(src_gt, columns=columns, after="b")
    assert [col.var for col in new_gt._boxhead] == ["b", "a", "c", "d"]

It seems that we'll have four small test cases for each function, resulting in two types of dataframes with two types of columns. It's evident that when Pandas interacts with Polars columns, the test will fail.

tests/test_spanners.py::test_cols_move_single_col[pandas-a] PASSED                                                                       [ 25%]
tests/test_spanners.py::test_cols_move_single_col[pandas-columns1] FAILED                                                                [ 50%]
tests/test_spanners.py::test_cols_move_single_col[polars-a] PASSED                                                                       [ 75%]
tests/test_spanners.py::test_cols_move_single_col[polars-columns1] PASSED                                                                [100%]

=========================================================== short test summary info ============================================================
FAILED tests/test_spanners.py::test_cols_move_single_col[pandas-columns1] - NotImplementedError: Unsupported selection expr: col("^(a).*$")
1 failed, 3 passed, 1 warning in 0.96s

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize this is testing specific combinations, where the suggestion just crosses everything. I seems reasonable to keep it as is!

@machow
Copy link
Collaborator

machow commented May 2, 2024

Meanwhile, given the substantial reliance on column selection within the fmt_* functions, should we conduct thorough tests for them using Polars selectors, or are we satisfied with the current state?

It seems okay to lean on the tests of fmt(), which the fmt_*() functions should end up calling (and handles the column selection).

https://github.com/posit-dev/great-tables/blob/main/tests/test_formats.py#L74

@@ -231,3 +197,75 @@ def test_cols_width_fully_set_pct_2():
assert gt_tbl._boxhead[0].column_width == "10%"
assert gt_tbl._boxhead[1].column_width == "10%"
assert gt_tbl._boxhead[2].column_width == "40%"


@pytest.mark.parametrize("df_lib, columns", [(pd, "a"), (pl, cs.starts_with("a"))])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize this is testing specific combinations, where the suggestion just crosses everything. I seems reasonable to keep it as is!

@machow
Copy link
Collaborator

machow commented May 3, 2024

Thanks so much for making sure things are covered! @rich-iannone do you want to merge?

@rich-iannone rich-iannone merged commit 5ea06af into posit-dev:main May 3, 2024
9 checks passed
@jrycw jrycw deleted the fix-35 branch May 3, 2024 16:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants