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

Refine the django.conf module check to see if the settings really are configured #668

Merged
merged 1 commit into from
Oct 30, 2018

Conversation

olasd
Copy link
Contributor

@olasd olasd commented Oct 30, 2018

Some modules, e.g. hypothesis, load some Django modules for their own fixtures.
This means that the django.conf module sometimes gets loaded, even though it's
never used (and the settings are not initialized).

This causes unrelated test failures when pytest_django is installed, as
pytest_django considers that having a loaded django.conf means the settings are
set up and ready to be modified. The Django settings object provides a flag to
check this condition, which we now use.

Add a regression test that mimics what hypothesis did which makes pytest-django
fail.

Closes #599.

configured flag in the Django settings object if django.conf has already
been imported.

"""
Copy link
Contributor

Choose a reason for hiding this comment

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

In case this needs to be fixed anyway, please remove the newline at the end.

@blueyed
Copy link
Contributor

blueyed commented Oct 30, 2018

(Inner) test fails during collection..

This fixes it:

diff --git i/tests/test_django_settings_module.py w/tests/test_django_settings_module.py
index a7f716d..98b3f8e 100644
--- i/tests/test_django_settings_module.py
+++ w/tests/test_django_settings_module.py
@@ -368,18 +368,24 @@ def test_no_ds_but_django_conf_imported(testdir, monkeypatch):
     testdir.makepyfile("""
         import os
 
-        # line copied from hypothesis/extras/django.py
-        from django.conf import settings as django_settings
-
-        from pytest_django.lazy_django import django_settings_is_configured
-
         def test_django_settings_is_configured():
+            # Line copied from hypothesis/extras/django.py
+            from django.conf import settings as django_settings
+
+            from pytest_django.lazy_django import django_settings_is_configured
+
             assert django_settings_is_configured() is False
 
         def test_env():
+            # Line copied from hypothesis/extras/django.py
+            from django.conf import settings as django_settings
+
             assert 'DJANGO_SETTINGS_MODULE' not in os.environ
 
         def test_cfg(pytestconfig):
+            # Line copied from hypothesis/extras/django.py
+            from django.conf import settings as django_settings
+
             assert pytestconfig.option.ds is None
     """)
     r = testdir.runpytest_subprocess('-s')

It still fails then without your patch, but only with test_django_settings_is_configured.

Especially for test_cfg I wonder if this then still tests the initial issue, assuming that the hypothesis import happens during test collection?!

@olasd
Copy link
Contributor Author

olasd commented Oct 30, 2018

(Inner) test fails during collection..

[...]

It still fails then without your patch, but only with test_django_settings_is_configured.

Especially for test_cfg I wonder if this then still tests the initial issue, assuming that the hypothesis import happens during test collection?!

I've modified my patch to import django.conf.settings and then del it so that pytest doesn't try to poke in it. I've also added an inner test to check that django.conf is really imported.

I'm guessing the QA check is broken because of the recent flake8 release? At least it's not in code I touched :-)

@olasd
Copy link
Contributor Author

olasd commented Oct 30, 2018

Ah, and, obviously in the end, having that patch on pytest_django makes it not interfere with my other tests!


# Don't let pytest poke into this object, generating a
# django.core.exceptions.ImproperlyConfigured
del django_settings
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow.. inspect.isclass / isinstance triggers the ImproperlyConfigured already..

@blueyed
Copy link
Contributor

blueyed commented Oct 30, 2018

  1. Created Harden isclass? pytest#4266 for discussion about the isclass issue.

  2. obviously in the end, having that patch on pytest_django makes it not interfere with my other tests!

What do you mean?
That my patch did not fix the issue for you?

Copy link
Contributor

@blueyed blueyed left a comment

Choose a reason for hiding this comment

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

Will rebase this after #670.

@olasd
Copy link
Contributor Author

olasd commented Oct 30, 2018

1. Created [pytest-dev/pytest#4266](https://github.com/pytest-dev/pytest/issues/4266) for discussion about the `isclass` issue.

2. > obviously in the end, having that patch on pytest_django makes it not interfere with my other tests!

What do you mean?
That my patch did not fix the issue for you?

Ah, no, I did my patch before I saw yours; I just meant that the original issue was indeed fixed :)

… configured

Some modules, e.g. hypothesis, load some Django modules for their own fixtures.
This means that the `django.conf` module sometimes gets loaded, even though it's
never used (and the settings are not initialized).

This causes unrelated test failures when pytest_django is installed, as
pytest_django considers that having a loaded django.conf means the settings are
set up and ready to be modified. The Django settings object provides a flag to
check this condition, which we now use.

Add a regression test that mimics what hypothesis did which makes pytest-django
fail.

Closes pytest-dev#599.
@blueyed
Copy link
Contributor

blueyed commented Oct 30, 2018

I see, rebased.
Will merge once it is green.
Thank you!

@codecov-io
Copy link

Codecov Report

Merging #668 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #668      +/-   ##
=========================================
+ Coverage   94.38%   94.4%   +0.01%     
=========================================
  Files          33      33              
  Lines        1834    1840       +6     
  Branches      156     156              
=========================================
+ Hits         1731    1737       +6     
  Misses         78      78              
  Partials       25      25
Flag Coverage Δ
#dj110 86.25% <100%> (+0.04%) ⬆️
#dj111 88.64% <100%> (+0.03%) ⬆️
#dj18 87.22% <100%> (+0.04%) ⬆️
#dj19 86.03% <100%> (+0.04%) ⬆️
#dj20 86.52% <100%> (+0.04%) ⬆️
#dj21 82.55% <100%> (+0.05%) ⬆️
#djmaster 82.55% <100%> (+0.05%) ⬆️
#mysql_innodb 86.73% <100%> (+0.04%) ⬆️
#mysql_myisam 86.57% <100%> (+0.04%) ⬆️
#postgres 89.89% <100%> (+0.03%) ⬆️
#py27 91.57% <100%> (+0.02%) ⬆️
#py34 86.03% <100%> (+0.04%) ⬆️
#py35 86.25% <100%> (+0.04%) ⬆️
#py36 87.44% <100%> (+0.04%) ⬆️
#py37 82.55% <100%> (+0.05%) ⬆️
#sqlite 88.58% <100%> (+0.03%) ⬆️
#sqlite_file 86.03% <100%> (+0.04%) ⬆️
Impacted Files Coverage Δ
tests/test_django_settings_module.py 100% <100%> (ø) ⬆️
pytest_django/lazy_django.py 100% <100%> (ø) ⬆️

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 9804e61...d8ef4f1. Read the comment docs.

@blueyed blueyed merged commit accae0d into pytest-dev:master Oct 30, 2018
@olasd olasd deleted the fix/django.conf-imported branch October 30, 2018 15:21
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.

pytest-django fails pytest (django.core.exceptions.ImproperlyConfigured)
3 participants