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

Fix test_sql pytest fixture warnings #22515

Merged
merged 10 commits into from
Sep 14, 2018
Merged

Fix test_sql pytest fixture warnings #22515

merged 10 commits into from
Sep 14, 2018

Conversation

alimcmaster1
Copy link
Member

@alimcmaster1 alimcmaster1 commented Aug 26, 2018

See Travis CI Python 3.6 build before and after my changes get rid of the warning for calling fixtures directly.

You can see this by searching for the string "pandas/tests/io/test_sql.py" to see that well over 100 warnings have been removed.

@pep8speaks
Copy link

pep8speaks commented Aug 26, 2018

Hello @alimcmaster1! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on August 31, 2018 at 17:49 Hours UTC

@codecov
Copy link

codecov bot commented Aug 26, 2018

Codecov Report

Merging #22515 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #22515   +/-   ##
=======================================
  Coverage   92.04%   92.04%           
=======================================
  Files         169      169           
  Lines       50782    50782           
=======================================
  Hits        46744    46744           
  Misses       4038     4038
Flag Coverage Δ
#multiple 90.45% <ø> (ø) ⬆️
#single 42.26% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14ccdd2...7427924. Read the comment docs.

@alimcmaster1 alimcmaster1 changed the title WIP: Fix test_sql pytest fixture warnings Fix test_sql pytest fixture warnings Aug 26, 2018
@alimcmaster1
Copy link
Member Author

See Travis CI Python 3.6 build before and after my changes this get rid of the warning for calling fixtures directly.

@gfyoung gfyoung added Testing pandas testing functions or related to the test suite IO SQL to_sql, read_sql, read_sql_query labels Aug 27, 2018
@@ -253,9 +253,13 @@ def _get_exec(self):
else:
return self.conn.cursor()

def _load_iris_data(self, datapath):
@pytest.fixture(params=[('io', 'data', 'iris.csv')])
def _load_iris_data(self, datapath, request):
Copy link
Member

Choose a reason for hiding this comment

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

Given that it's now a fixture, let's remove the initial underscore in the function name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure @gfyoung that makes sense! Will go ahead and do this, any other feedback from anyone?

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated

@gfyoung
Copy link
Member

gfyoung commented Aug 29, 2018

https://travis-ci.org/pandas-dev/pandas/jobs/421777999#L2544

@jreback : Not sure why scipy is breaking on these builds suddenly...did something happen to the 2.7 package for scipy ?

self._load_raw_sql()
self._load_test1_data()

@pytest.fixture(autouse=True)
def setup_method(self, load_iris_data):
Copy link
Member

@WillAyd WillAyd Aug 29, 2018

Choose a reason for hiding this comment

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

Hmm I realize this is not something you have introduced but we seem to be ambiguously using both the classic xunit-style setup and a fixture. I think we should just use one or the other. Probably easiest to just remove the fixture decorator for now

Copy link
Member Author

Choose a reason for hiding this comment

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

If we remove the fixture annotation this will cause the tests to fail in the current implementation. It seems like this is somewhat common to mix the old xunit style setup and fixtures. See @nicoddemus first comment on this issue. pytest-dev/pytest#517

Copy link
Member Author

Choose a reason for hiding this comment

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

But definitely agree we should maybe look at getting rid of the xunit style set up eventually :)

Copy link
Member

Choose a reason for hiding this comment

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

What failure did that cause? In the provided link AFAICT it is showcasing an easy transition from old style to new, which is what I'm suggesting here as well

Copy link
Member Author

Choose a reason for hiding this comment

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

The xunit setup is applied before any fixtures if we remove the annotation. For example in the test: TestSQLApi.test_read_sql_iris. The function self.load_test_data_and_sql() will be invoked before the load_iris_data fixture ( when @pytest.fixture(autouse=True) is not present ).

Since self.load_test_data_and_sql() requires self.conn to be set, this will error.

Hope that explains the issue clearly :)

Copy link
Member Author

Choose a reason for hiding this comment

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

hey a friendly ping: @WillAyd are you happy to review ? Hope my explanation addressed the concerns here

Copy link
Member

Choose a reason for hiding this comment

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

Hmm OK understood on the issues you are facing but just seems like this can be simplified instead of adding new methods. Is it not possible to set the connection here instead of having it as a separate method?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for getting back to me appreciate it - My thinking behind having it as a separate method was that the _TestSQLAlchemy class already has a separate method setup_connect. I did the same in TestSQLiteFallback and _TestSQLApi so that my fixture load_iris_data used across all these test classes could call the same method to do the required connection set up.

Perhaps we need to consider a larger refactor of this file in order to make this set up just solely use fixtures, but with regards to getting rid of 100+ warnings in the 3.6 build mentioned #22338 this was the simplest and clearest change I could think of, let me know if you think otherwise, would be interested to see. Thanks as always for taking a look!

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation and your patience. I do see your point; could you open a follow up issue to refactor this module to use fixtures exclusively instead of the mix with inheritance?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure i've raised here #22624. Mind adding the Tags, "Testing", "IO SQL" and perhaps "Medium Difficulty"

@@ -503,10 +507,14 @@ class _TestSQLApi(PandasSQLTest):
flavor = 'sqlite'
mode = None

@pytest.fixture(autouse=True)
def setup_method(self, datapath):
def setup_connect(self):
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this is a dedicated method? Seems like a simple enough expression to have just included in the setup method alone

Copy link
Member Author

Choose a reason for hiding this comment

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

load_iris_data fixture basically requires that self has a conn attribute set, see line 262. I basically wanted to separate out the connection setup for each test classes. Since _TestSQLAlchemy already did this I though it would be nice to keep it consistent

@alimcmaster1
Copy link
Member Author

Close/Re-open to restart CI after SciPy failures

@gfyoung
Copy link
Member

gfyoung commented Aug 31, 2018

@alimcmaster1: feel to also ping us if you want to restart one of your builds so that you don't have to retrigger the entire test suite.

@alimcmaster1
Copy link
Member Author

Thanks @gfyoung will do that in the future!

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.

Checking the 3.6 log there still appears to be one more warning - do you mind taking a look to see if that can be resolved?

self._load_raw_sql()
self._load_test1_data()

@pytest.fixture(autouse=True)
def setup_method(self, load_iris_data):
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the explanation and your patience. I do see your point; could you open a follow up issue to refactor this module to use fixtures exclusively instead of the mix with inheritance?

@alimcmaster1
Copy link
Member Author

Thanks @WillAyd for reviewing this. I've update to get rid of the final warning for this file. See travis 3.6 build now is warning free for this file :)

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.

Generally OK though certainly do not want to lose sight of the refactor for the issue you opened

@gfyoung what do you think?

Copy link
Member

@gfyoung gfyoung left a comment

Choose a reason for hiding this comment

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

LGTM too! Good call on breaking off the refactor for later.

cc @jreback

@@ -253,9 +253,13 @@ def _get_exec(self):
else:
return self.conn.cursor()

def _load_iris_data(self, datapath):
@pytest.fixture(params=[('io', 'data', 'iris.csv')])
Copy link
Member

Choose a reason for hiding this comment

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

Is there a technical reason the path needs to passed here as a param (instead of how it was before)?
We use datapath like this in many other places in the tests

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @jorisvandenbossche yes there is, in all the other places we pass in datapath we don't call it directly as a function. Like this code originally did datapath('io', 'data', 'iris.csv'). Which is now deprecated, see this pytest issue, with the suggested refactor.

Copy link
Member

Choose a reason for hiding this comment

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

What do you mean with "we don't call it directly as a function" (the datapath fixture itself, or the function that it is passed to?). Because the fixture datapath itself is called directly in many other places in the code base.

Copy link
Contributor

Choose a reason for hiding this comment

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

@alimcmaster1 can you respond here?

I'm in the middle of a PR fixing transitioning warnings to errors. Going to skip sql for now, but this will need to be merged first.

Copy link
Contributor

Choose a reason for hiding this comment

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

But, if I go from memory, is it because it's passed as a fixture to the caller of this method? The classes setup_method, which then passes it through to load_iris_data as a function, not a fixture?

Copy link
Member Author

Choose a reason for hiding this comment

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

Firstly apologies for the delayed response I havn't had internet access for the past few days. Yes @jorisvandenbossche agree we do, I worded that badly, but I basically meant what @TomAugspurger said above, we want to avoid passing it like self._load_iris_data(datapath). Then calling datapath directly.

But I see your original point and you are quite right we could write as so

@pytest.fixture()
def load_iris_data(self, datapath)
     csv = datapath('io', 'data', 'iris.csv')

The code in setup_method would remain as i've currently implemented. But I thought why not parameterize? In case we have another data-set we want to run these tests against in the future?

@TomAugspurger
Copy link
Contributor

This looks good to me. Let's merge so we can make progress on getting our CI in shape again.

@TomAugspurger TomAugspurger merged commit b7e5704 into pandas-dev:master Sep 14, 2018
aeltanawy pushed a commit to aeltanawy/pandas that referenced this pull request Sep 20, 2018
* Avoid calling pytest fixtures directly
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
* Avoid calling pytest fixtures directly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO SQL to_sql, read_sql, read_sql_query Testing pandas testing functions or related to the test suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TST: incorrectly used fixtures causing warnings
6 participants