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

chore: refactor compare_dicts #1224

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

EdAbati
Copy link
Contributor

@EdAbati EdAbati commented Oct 18, 2024

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

  • Related issue #
  • Closes #

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below.

I propose a couple of changes:

  • I'd like to have a more verbose assertion error message when a unitest fail. Taking inspiration from pandas, I'm printing all the values of the columns that differ and the index where the difference was first noticed. This is particularly helpful for dataframes libraries that don't keep row order (* cough cough * pyspark * cough cough *)
def test_select(constructor: Constructor) -> None:
    data = {"a": [1, 3, 2], "b": [4, 4, 6], "z": [7.0, 8, 9]}
    df = nw.from_native(constructor(data))
    result = df.select("a")
    expected = {"a": [1, 3, 3]}
    compare_dicts(result, expected)

Before:
Screenshot 2024-10-18 at 23 50 35

After:
Screenshot 2024-10-18 at 23 49 06

  • I think we should rename compare_dicts because we are not actually comparing 2 dictionaries. πŸ˜… I propose something like assert_equal_data, any better name? This makes the diff of the PR a bit large, happy to revert if you think we should keep the previous name

@EdAbati EdAbati marked this pull request as ready for review October 18, 2024 21:59
tests/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Thanks @EdAbati I like this a lot more! I left a comment but can also be thought as a follow up.

For the naming, I think this is the closest we have to what in pandas and polars is assert_frame_equal, but we are not there quite yet, so I am not sure πŸ™ˆ

else:
assert lhs == rhs, (lhs, rhs)
are_valid_values = lhs == rhs
assert are_valid_values, f"Mismatch at index {i}: {lhs} != {rhs}\nExpected: {expected}\nGot: {result}"
Copy link
Member

Choose a reason for hiding this comment

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

Should we record the full diff instead of stopping at the first unequal encounter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants