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

REF: test_to_latex #36528

Merged
merged 7 commits into from
Sep 22, 2020
Merged

REF: test_to_latex #36528

merged 7 commits into from
Sep 22, 2020

Conversation

ivanovmg
Copy link
Member

  • closes #xxxx
  • tests added / passed
  • passes black pandas
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

Refactor/clean-up test_to_latex.py.

  • Split big test functions with multiple assertions into multiple functions
  • Make readable expected strings by first indenting for the good visual appearance and then dedenting by the leading whitespace for the assertion.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

Cool I think this helps readability - just one minor comment otherwise lgtm

assert result == expected

df = DataFrame.from_dict(
@pytest.fixture
def multiindex_frame(self):
Copy link
Member

Choose a reason for hiding this comment

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

Just as a matter of convention can you move the fixture to the top of the module rather than in between the tests? (though this was nice to keep here for review)

Copy link
Member Author

Choose a reason for hiding this comment

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

I moved the fixtures to the top of the class. If I take one more step in a separate PR, I would split the large test class into multiple classes and distribute relevant fixtures between them.

@WillAyd WillAyd added the Clean label Sep 21, 2020
@jreback jreback added IO LaTeX to_latex Testing pandas testing functions or related to the test suite labels Sep 21, 2020
@jreback jreback added this to the 1.2 milestone Sep 21, 2020
@jreback
Copy link
Contributor

jreback commented Sep 21, 2020

yeah agree with @WillAyd comment as well. Note that would be also fine to organize these into different classes (if that's useful). same / followon ok with this

ping on green.

@ivanovmg
Copy link
Member Author

yeah agree with @WillAyd comment as well. Note that would be also fine to organize these into different classes (if that's useful). same / followon ok with this

ping on green.

Yes, I though of re-organizing the tests into separate classes. I would prefer to do that in a separate PR, after this one.

What is the policy on the test functions naming? I mean if we organize tests in the appropriate classes, then would that be necessary for us to keep to_latex in all of them? As I understand, this is useful for pytest .. -k to_latex filtering, but similarly one can run pytest -k ToLatex if all classes contain ToLatex in them.

Similarly, if we have a separate class on, let's say multiindex, then I would rather remove multiindex from the test functions as well, as the information will be available in the class name.
What do you think about it?

Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

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

Thanks @ivanovmg for the PR. generally lgtm.

with open(path) as f:
assert float_frame.to_latex() == f.read()

def test_to_latex_to_file_utf8_with_encoding(self, float_frame):
Copy link
Member

Choose a reason for hiding this comment

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

float_frame not needed in this test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you! Removed.

# test with utf-8 and encoding option (GH 7061)
df = DataFrame([["au\xdfgangen"]])
with tm.ensure_clean("test.tex") as path:
df.to_latex(path, encoding="utf-8")
with codecs.open(path, "r", encoding="utf-8") as f:
assert df.to_latex() == f.read()

def test_to_latex_to_file_utf8_without_encoding(self, float_frame):
Copy link
Member

Choose a reason for hiding this comment

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

same

class TestToLatex:
def test_to_latex_filename(self, float_frame):
@pytest.fixture
def df_caption_label(self):
Copy link
Member

Choose a reason for hiding this comment

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

fixtures should have docstrings xref #19159

Copy link
Member Author

Choose a reason for hiding this comment

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

Added

)

@pytest.fixture
def df_caption_label_longtable(self):
Copy link
Member

Choose a reason for hiding this comment

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

might make the tests more readable (no tuple unpacking) if frame, caption, and label were composable fixtures from a longtable fixture yielding True/False

because fixtures returning tuples can be undesirable for several reasons, pytest-cases has a helper to overcome this https://smarie.github.io/python-pytest-cases/pytest_goodies/#unpack_fixture-unpack_into.

so should maybe consider avoiding creating fixtures returning tuples.

Copy link
Member Author

Choose a reason for hiding this comment

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

I tried to make it less painful by using namedtuple.
Anyway, I checked on the fixture unpacking (thank you for mentioning it, I was not aware of that).
So far, for the cases concerned, I do not see why it is better than separate fixtures. I mean, that is certainly beneficial if the fixture contains parametrization, but the ones involved do not have parametrization.

So, I decided to supply separate fixtures for dataframe, captions and labels.
What do you think about it?

Copy link
Member

Choose a reason for hiding this comment

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

changes look good.

So far, for the cases concerned, I do not see why it is better than separate fixtures. I mean, that is certainly beneficial if the fixture contains parametrization, but the ones involved do not have parametrization.

In a follow-on could maybe parameterise the caption and label the fixtures on longtable True/False and combine tests.

it is sometimes better to parameterise the tests firsts and then extract common paramterisation into fixtures.

Copy link
Member

@WillAyd WillAyd left a comment

Choose a reason for hiding this comment

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

@jreback
Copy link
Contributor

jreback commented Sep 22, 2020

thanks @ivanovmg very nice.

so a couple of things for followons.

  • prefer shorter PRs, though I do get that sometimes it makes sense to change everything in one go
  • we use classes for organization, i would still leave the to_latex in the test name as with pytest you could search on this as well
  • using composable fixtures is nice if you can, but again use what makes sense

@jreback jreback merged commit 254f509 into pandas-dev:master Sep 22, 2020
@ivanovmg ivanovmg deleted the refactor/test-latex branch September 28, 2020 18:53
kesmit13 pushed a commit to kesmit13/pandas that referenced this pull request Nov 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Clean IO LaTeX to_latex Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants