From 0b7ce94225306632cd477249a5ea7829bcaf94b6 Mon Sep 17 00:00:00 2001 From: Bruno Rocha Date: Fri, 20 Aug 2021 16:16:40 +0100 Subject: [PATCH] Fix #491 add docs and example on how to use pytest and django --- Makefile | 1 + docs/django.md | 19 +++ example/django_pytest/.env | 2 + example/django_pytest/.gitignore | 6 + example/django_pytest/README.md | 113 ++++++++++++++ example/django_pytest/app/__init__.py | 0 example/django_pytest/app/admin.py | 3 + example/django_pytest/app/apps.py | 5 + .../django_pytest/app/migrations/__init__.py | 0 example/django_pytest/app/models.py | 3 + .../django_pytest/app/templates/hello.html | 18 +++ example/django_pytest/app/tests/__init__.py | 0 example/django_pytest/app/tests/conftest.py | 16 ++ example/django_pytest/app/tests/test_app.py | 16 ++ example/django_pytest/app/urls.py | 10 ++ example/django_pytest/app/views.py | 11 ++ example/django_pytest/manage.py | 22 +++ example/django_pytest/project/__init__.py | 0 .../django_pytest/project/fixtures/admin.json | 20 +++ example/django_pytest/project/settings.py | 140 ++++++++++++++++++ example/django_pytest/project/settings.yaml | 7 + example/django_pytest/project/urls.py | 10 ++ example/django_pytest/project/wsgi.py | 8 + example/django_pytest/pytest.ini | 3 + example/django_pytest/requirements.txt | 4 + example/django_pytest/setup.sh | 10 ++ example/django_pytest/tests.sh | 5 + example/django_pytest_pure/.env | 2 + example/django_pytest_pure/.gitignore | 6 + example/django_pytest_pure/README.md | 113 ++++++++++++++ example/django_pytest_pure/app/__init__.py | 0 example/django_pytest_pure/app/admin.py | 3 + example/django_pytest_pure/app/apps.py | 5 + .../app/migrations/__init__.py | 0 example/django_pytest_pure/app/models.py | 3 + .../app/templates/hello.html | 18 +++ .../django_pytest_pure/app/tests/__init__.py | 0 .../django_pytest_pure/app/tests/conftest.py | 8 + .../django_pytest_pure/app/tests/test_app.py | 16 ++ example/django_pytest_pure/app/urls.py | 10 ++ example/django_pytest_pure/app/views.py | 11 ++ example/django_pytest_pure/manage.py | 22 +++ .../django_pytest_pure/project/__init__.py | 0 .../project/fixtures/admin.json | 20 +++ .../django_pytest_pure/project/settings.py | 120 +++++++++++++++ example/django_pytest_pure/project/urls.py | 10 ++ example/django_pytest_pure/project/wsgi.py | 8 + example/django_pytest_pure/pytest.ini | 3 + example/django_pytest_pure/requirements.txt | 4 + example/django_pytest_pure/setup.sh | 10 ++ example/django_pytest_pure/tests.sh | 5 + tests/test_django.py | 4 +- 52 files changed, 851 insertions(+), 2 deletions(-) create mode 100644 example/django_pytest/.env create mode 100644 example/django_pytest/.gitignore create mode 100644 example/django_pytest/README.md create mode 100644 example/django_pytest/app/__init__.py create mode 100644 example/django_pytest/app/admin.py create mode 100644 example/django_pytest/app/apps.py create mode 100644 example/django_pytest/app/migrations/__init__.py create mode 100644 example/django_pytest/app/models.py create mode 100644 example/django_pytest/app/templates/hello.html create mode 100644 example/django_pytest/app/tests/__init__.py create mode 100644 example/django_pytest/app/tests/conftest.py create mode 100644 example/django_pytest/app/tests/test_app.py create mode 100644 example/django_pytest/app/urls.py create mode 100644 example/django_pytest/app/views.py create mode 100755 example/django_pytest/manage.py create mode 100644 example/django_pytest/project/__init__.py create mode 100644 example/django_pytest/project/fixtures/admin.json create mode 100644 example/django_pytest/project/settings.py create mode 100644 example/django_pytest/project/settings.yaml create mode 100644 example/django_pytest/project/urls.py create mode 100644 example/django_pytest/project/wsgi.py create mode 100644 example/django_pytest/pytest.ini create mode 100644 example/django_pytest/requirements.txt create mode 100755 example/django_pytest/setup.sh create mode 100755 example/django_pytest/tests.sh create mode 100644 example/django_pytest_pure/.env create mode 100644 example/django_pytest_pure/.gitignore create mode 100644 example/django_pytest_pure/README.md create mode 100644 example/django_pytest_pure/app/__init__.py create mode 100644 example/django_pytest_pure/app/admin.py create mode 100644 example/django_pytest_pure/app/apps.py create mode 100644 example/django_pytest_pure/app/migrations/__init__.py create mode 100644 example/django_pytest_pure/app/models.py create mode 100644 example/django_pytest_pure/app/templates/hello.html create mode 100644 example/django_pytest_pure/app/tests/__init__.py create mode 100644 example/django_pytest_pure/app/tests/conftest.py create mode 100644 example/django_pytest_pure/app/tests/test_app.py create mode 100644 example/django_pytest_pure/app/urls.py create mode 100644 example/django_pytest_pure/app/views.py create mode 100755 example/django_pytest_pure/manage.py create mode 100644 example/django_pytest_pure/project/__init__.py create mode 100644 example/django_pytest_pure/project/fixtures/admin.json create mode 100644 example/django_pytest_pure/project/settings.py create mode 100644 example/django_pytest_pure/project/urls.py create mode 100644 example/django_pytest_pure/project/wsgi.py create mode 100644 example/django_pytest_pure/pytest.ini create mode 100644 example/django_pytest_pure/requirements.txt create mode 100755 example/django_pytest_pure/setup.sh create mode 100755 example/django_pytest_pure/tests.sh diff --git a/Makefile b/Makefile index 7afc62941..5aff87e21 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,7 @@ test_examples: cd example/jenkins_secrets_file;pwd;python app.py cd example/specific_settings_files;pwd;python app.py cd example/django_example/;pwd;python manage.py test polls -v 2 + cd example/django_pytest/;pwd;pip install pytest-django;DJANGO_SETTINGS_MODULE=project.settings DJANGO_ENVIRONMENT=default pytest;pip uninstall -y pytest-django cd example/django_example_compat/;pwd;python manage.py test polls -v 2 cd example/django_example/;pwd;PYTHONPATH=. DJANGO_SETTINGS_MODULE=foo.settings django-admin test polls -v 2 cd example/project_root/;pwd;rm -rf /tmp/dynaconf_project_root_test/settings.py;mkdir -p /tmp/dynaconf_project_root_test/;echo "MESSAGE = 'Hello from tmp'" > /tmp/dynaconf_project_root_test/settings.py;python app.py;rm -rf /tmp/dynaconf_project_root_test/ diff --git a/docs/django.md b/docs/django.md index 534eef398..307fa5f1b 100644 --- a/docs/django.md +++ b/docs/django.md @@ -209,6 +209,8 @@ print(settings.get('DATABASES')) Django testing must work out of the box! +## Mocking envvars with django + But in some cases when you `mock` stuff and need to add `environment variables` to `os.environ` on demand for test cases it may be needed to `reload` the `dynaconf`. To do that write up on your test case setup part: @@ -227,6 +229,23 @@ class TestCase(...): self.assertEqual(settings.FOO, 'BAR') ``` +## Using pytest and django + +Install `pip install pytest-django` + +Add to your conftest.py + +`project/tests/conftest.py` +```py +import pytest + +@pytest.fixture(scope="session", autouse=True) +def set_test_settings(): + # https://github.com/rochacbruno/dynaconf/issues/491#issuecomment-745391955 + from django.conf import settings + settings.setenv('testing') # force the environment to be whatever you want +``` + ## Explicit mode Some users have the preference to explicitly load each setting variable inside the `settings.py` and then let django manage it in the common way, it is possible. diff --git a/example/django_pytest/.env b/example/django_pytest/.env new file mode 100644 index 000000000..54302b4ac --- /dev/null +++ b/example/django_pytest/.env @@ -0,0 +1,2 @@ +DJANGO_SETTINGS_MODULE=project.settings +DJANGO_ENVIRONMENT=default diff --git a/example/django_pytest/.gitignore b/example/django_pytest/.gitignore new file mode 100644 index 000000000..c899468ac --- /dev/null +++ b/example/django_pytest/.gitignore @@ -0,0 +1,6 @@ +.idea +/venv +/static +__pycache__ +.cache +db.sqlite3 diff --git a/example/django_pytest/README.md b/example/django_pytest/README.md new file mode 100644 index 000000000..b8dd569f4 --- /dev/null +++ b/example/django_pytest/README.md @@ -0,0 +1,113 @@ +# Reproduce pytest + dynaconf error + +setup project +``` +./setup.sh +``` + +run tests +``` +./tests.sh +``` + +result +```python +=================================================================================================================================================== test session starts ==================================================================================================================================================== +platform linux -- Python 3.6.9, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 +django: settings: project.settings (from ini) +rootdir: /home/alex/dje/django-pytest-dynaconf, inifile: pytest.ini +plugins: django-4.1.0 +collected 1 item + +app/tests/test_app.py E [100%] + +========================================================================================================================================================== ERRORS ========================================================================================================================================================== +____________________________________________________________________________________________________________________________________________ ERROR at setup of test_admin_user _____________________________________________________________________________________________________________________________________________ + +request = > + + @pytest.fixture(autouse=True) + def _django_db_marker(request): + """Implement the django_db marker, internal to pytest-django. + + This will dynamically request the ``db``, ``transactional_db`` or + ``django_db_reset_sequences`` fixtures as required by the django_db marker. + """ + marker = request.node.get_closest_marker("django_db") + if marker: + transaction, reset_sequences = validate_django_db(marker) + if reset_sequences: + request.getfixturevalue("django_db_reset_sequences") + elif transaction: + request.getfixturevalue("transactional_db") + else: +> request.getfixturevalue("db") + +venv/lib/python3.6/site-packages/pytest_django/plugin.py:436: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +venv/lib/python3.6/site-packages/pytest_django/fixtures.py:108: in django_db_setup + **setup_databases_args +venv/lib/python3.6/site-packages/django/test/utils.py:174: in setup_databases + serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True), +venv/lib/python3.6/site-packages/django/db/backends/base/creation.py:68: in create_test_db + run_syncdb=True, +venv/lib/python3.6/site-packages/django/core/management/__init__.py:110: in call_command + command = load_command_class(app_name, command_name) +venv/lib/python3.6/site-packages/django/core/management/__init__.py:36: in load_command_class + module = import_module('%s.management.commands.%s' % (app_name, name)) +/usr/lib/python3.6/importlib/__init__.py:126: in import_module + return _bootstrap._gcd_import(name[level:], package, level) +venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py:14: in + from django.db.migrations.autodetector import MigrationAutodetector +venv/lib/python3.6/site-packages/django/db/migrations/autodetector.py:11: in + from django.db.migrations.questioner import MigrationQuestioner +venv/lib/python3.6/site-packages/django/db/migrations/questioner.py:9: in + from .loader import MigrationLoader +venv/lib/python3.6/site-packages/django/db/migrations/loader.py:8: in + from django.db.migrations.recorder import MigrationRecorder +venv/lib/python3.6/site-packages/django/db/migrations/recorder.py:9: in + class MigrationRecorder: +venv/lib/python3.6/site-packages/django/db/migrations/recorder.py:22: in MigrationRecorder + class Migration(models.Model): +venv/lib/python3.6/site-packages/django/db/models/base.py:101: in __new__ + new_class.add_to_class('_meta', Options(meta, app_label)) +venv/lib/python3.6/site-packages/django/db/models/options.py:101: in __init__ + self.db_tablespace = settings.DEFAULT_TABLESPACE +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +self = , name = 'DEFAULT_TABLESPACE' + + def __getattr__(self, name): + """Allow getting keys from self.store using dot notation""" + if self._wrapped is empty: + self._setup() + if name in self._wrapped._deleted: # noqa + raise AttributeError( + f"Attribute {name} was deleted, " "or belongs to different env" + ) + + if name not in RESERVED_ATTRS: + lowercase_mode = self._kwargs.get( + "LOWERCASE_READ_FOR_DYNACONF", + default_settings.LOWERCASE_READ_FOR_DYNACONF, + ) + if lowercase_mode is True: + name = name.upper() + + if ( + name.isupper() + and ( + self._wrapped._fresh + or name in self._wrapped.FRESH_VARS_FOR_DYNACONF + ) + and name not in dir(default_settings) + ): + return self._wrapped.get_fresh(name) +> value = getattr(self._wrapped, name) +E AttributeError: 'Settings' object has no attribute 'DEFAULT_TABLESPACE' + +venv/lib/python3.6/site-packages/dynaconf/base.py:164: AttributeError +================================================================================================================================================= short test summary info ================================================================================================================================================== +ERROR app/tests/test_app.py::test_admin_user - AttributeError: 'Settings' object has no attribute 'DEFAULT_TABLESPACE' +===================================================================================================================================================== 1 error in 0.25s ===================================================================================================================================================== +``` diff --git a/example/django_pytest/app/__init__.py b/example/django_pytest/app/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest/app/admin.py b/example/django_pytest/app/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/example/django_pytest/app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/example/django_pytest/app/apps.py b/example/django_pytest/app/apps.py new file mode 100644 index 000000000..200c5984c --- /dev/null +++ b/example/django_pytest/app/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AppConfig(AppConfig): + name = "app" diff --git a/example/django_pytest/app/migrations/__init__.py b/example/django_pytest/app/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest/app/models.py b/example/django_pytest/app/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/example/django_pytest/app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/example/django_pytest/app/templates/hello.html b/example/django_pytest/app/templates/hello.html new file mode 100644 index 000000000..376becdd0 --- /dev/null +++ b/example/django_pytest/app/templates/hello.html @@ -0,0 +1,18 @@ + + + + + {{ title }} + + + +

{{ message }}

+ + + diff --git a/example/django_pytest/app/tests/__init__.py b/example/django_pytest/app/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest/app/tests/conftest.py b/example/django_pytest/app/tests/conftest.py new file mode 100644 index 000000000..16e104e5d --- /dev/null +++ b/example/django_pytest/app/tests/conftest.py @@ -0,0 +1,16 @@ +import pytest +from django.core.management import call_command + + +@pytest.fixture(scope="session", autouse=True) +def set_test_settings(): + # https://github.com/rochacbruno/dynaconf/issues/491#issuecomment-745391955 + from django.conf import settings + + settings.setenv("pytest") + + +@pytest.fixture(scope="session") +def django_db_setup(django_db_setup, django_db_blocker): + with django_db_blocker.unblock(): + call_command("loaddata", "project/fixtures/admin.json") diff --git a/example/django_pytest/app/tests/test_app.py b/example/django_pytest/app/tests/test_app.py new file mode 100644 index 000000000..ce7f3e912 --- /dev/null +++ b/example/django_pytest/app/tests/test_app.py @@ -0,0 +1,16 @@ +import django + +django.setup() # noqa + +import pytest + +from django.contrib.auth import get_user_model +from django.conf import settings + + +@pytest.mark.django_db +def test_admin_user(): + User = get_user_model() + + assert User.objects.filter(username="admin").exists() + assert settings.FOO == "foo-test" diff --git a/example/django_pytest/app/urls.py b/example/django_pytest/app/urls.py new file mode 100644 index 000000000..c1e3a7427 --- /dev/null +++ b/example/django_pytest/app/urls.py @@ -0,0 +1,10 @@ +from app import views +from django.urls import include +from django.urls import path + +# namespacing +app_name = "app" + +urlpatterns = [ + path(r"", views.HelloView.as_view(), name="hello"), +] diff --git a/example/django_pytest/app/views.py b/example/django_pytest/app/views.py new file mode 100644 index 000000000..d53f9d5bb --- /dev/null +++ b/example/django_pytest/app/views.py @@ -0,0 +1,11 @@ +from django.views.generic import TemplateView + + +class HelloView(TemplateView): + template_name = "hello.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["title"] = "Hello!" + context["message"] = "Hello world!" + return context diff --git a/example/django_pytest/manage.py b/example/django_pytest/manage.py new file mode 100755 index 000000000..f522e0072 --- /dev/null +++ b/example/django_pytest/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/example/django_pytest/project/__init__.py b/example/django_pytest/project/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest/project/fixtures/admin.json b/example/django_pytest/project/fixtures/admin.json new file mode 100644 index 000000000..1c826649a --- /dev/null +++ b/example/django_pytest/project/fixtures/admin.json @@ -0,0 +1,20 @@ +[ + { + "model": "auth.user", + "pk": 1, + "fields": { + "password": "pbkdf2_sha256$120000$hzEDlLe2T6O2$YVDIso96yOXVq6+IcD0Iy1K6He/x9lzM5IFQU64UeKg=", + "last_login": null, + "is_superuser": true, + "username": "admin", + "first_name": "", + "last_name": "", + "email": "", + "is_staff": true, + "is_active": true, + "date_joined": "2020-12-15T08:31:26.387Z", + "groups": [], + "user_permissions": [] + } + } +] diff --git a/example/django_pytest/project/settings.py b/example/django_pytest/project/settings.py new file mode 100644 index 000000000..f75d8228a --- /dev/null +++ b/example/django_pytest/project/settings.py @@ -0,0 +1,140 @@ +""" +https://docs.djangoproject.com/en/1.11/topics/settings/ +https://docs.djangoproject.com/en/1.11/ref/settings/ +https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ +""" +import os +from collections import OrderedDict + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "yeicahshiquohc3Mae8ahl9yah7fa9eiCh6ooyiefaeheipiobe1fikohL6ahpho" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = os.environ.get("DJANGO_DEBUG", "false").lower() == "true" + +if DEBUG: + EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + +ALLOWED_HOSTS = ["*"] + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.humanize", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "app", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "project.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.media", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "project.wsgi.application" + +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(".", "db.sqlite3"), + } +} + +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": ( + "django.contrib.auth.password_validation." + "UserAttributeSimilarityValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." "MinimumLengthValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." + "CommonPasswordValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." + "NumericPasswordValidator" + ), + }, +] + +# https://docs.djangoproject.com/en/1.11/topics/i18n/ +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# https://docs.djangoproject.com/en/1.11/howto/static-files/ +STATIC_URL = "/static/" +STATIC_ROOT = "static" + + +STATICFILES_FINDERS = [ + "django.contrib.staticfiles.finders.FileSystemFinder", + "django.contrib.staticfiles.finders.AppDirectoriesFinder", +] + + +# +# HERE STARTS DYNACONF EXTENSION LOAD +# +import dynaconf # noqa +from dynaconf import Validator # noqa + + +settings = dynaconf.DjangoDynaconf( + __name__, + settings_files=["settings.yaml"], + env_switcher="DJANGO_ENVIRONMENT", + load_dotenv=True, + validators=[ + Validator("ENVIRONMENT", "TESTING", "FOO", must_exist=True), + ], +) # noqa + +# +# HERE ENDS DYNACONF EXTENSION LOAD (No more code below this line) +# diff --git a/example/django_pytest/project/settings.yaml b/example/django_pytest/project/settings.yaml new file mode 100644 index 000000000..80da69492 --- /dev/null +++ b/example/django_pytest/project/settings.yaml @@ -0,0 +1,7 @@ +default: + FOO: foo + TESTING: False + +pytest: + FOO: foo-test + TESTING: True diff --git a/example/django_pytest/project/urls.py b/example/django_pytest/project/urls.py new file mode 100644 index 000000000..b32ab9d8f --- /dev/null +++ b/example/django_pytest/project/urls.py @@ -0,0 +1,10 @@ +# https://docs.djangoproject.com/en/1.10/topics/http/urls/ +from django.contrib import admin +from django.urls import include +from django.urls import path +from django.views.generic import RedirectView + +urlpatterns = [ + path("", include("app.urls")), + path("admin/", admin.site.urls), +] diff --git a/example/django_pytest/project/wsgi.py b/example/django_pytest/project/wsgi.py new file mode 100644 index 000000000..1f6156f21 --- /dev/null +++ b/example/django_pytest/project/wsgi.py @@ -0,0 +1,8 @@ +# https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + +application = get_wsgi_application() diff --git a/example/django_pytest/pytest.ini b/example/django_pytest/pytest.ini new file mode 100644 index 000000000..596dc0964 --- /dev/null +++ b/example/django_pytest/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +DJANGO_SETTINGS_MODULE = project.settings +python_files = tests.py test_*.py *_tests.py diff --git a/example/django_pytest/requirements.txt b/example/django_pytest/requirements.txt new file mode 100644 index 000000000..e15c59280 --- /dev/null +++ b/example/django_pytest/requirements.txt @@ -0,0 +1,4 @@ +Django==2.1.5 +dynaconf==3.1.2 +pytest==5.4.3 +pytest-django==4.1.0 diff --git a/example/django_pytest/setup.sh b/example/django_pytest/setup.sh new file mode 100755 index 000000000..072805534 --- /dev/null +++ b/example/django_pytest/setup.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail + +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +source .env +./manage.py migrate +./manage.py collectstatic --noinput +./manage.py loaddata project/fixtures/admin.json diff --git a/example/django_pytest/tests.sh b/example/django_pytest/tests.sh new file mode 100755 index 000000000..909057cfc --- /dev/null +++ b/example/django_pytest/tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -euo pipefail + +source venv/bin/activate +pytest diff --git a/example/django_pytest_pure/.env b/example/django_pytest_pure/.env new file mode 100644 index 000000000..54302b4ac --- /dev/null +++ b/example/django_pytest_pure/.env @@ -0,0 +1,2 @@ +DJANGO_SETTINGS_MODULE=project.settings +DJANGO_ENVIRONMENT=default diff --git a/example/django_pytest_pure/.gitignore b/example/django_pytest_pure/.gitignore new file mode 100644 index 000000000..c899468ac --- /dev/null +++ b/example/django_pytest_pure/.gitignore @@ -0,0 +1,6 @@ +.idea +/venv +/static +__pycache__ +.cache +db.sqlite3 diff --git a/example/django_pytest_pure/README.md b/example/django_pytest_pure/README.md new file mode 100644 index 000000000..b8dd569f4 --- /dev/null +++ b/example/django_pytest_pure/README.md @@ -0,0 +1,113 @@ +# Reproduce pytest + dynaconf error + +setup project +``` +./setup.sh +``` + +run tests +``` +./tests.sh +``` + +result +```python +=================================================================================================================================================== test session starts ==================================================================================================================================================== +platform linux -- Python 3.6.9, pytest-5.4.3, py-1.10.0, pluggy-0.13.1 +django: settings: project.settings (from ini) +rootdir: /home/alex/dje/django-pytest-dynaconf, inifile: pytest.ini +plugins: django-4.1.0 +collected 1 item + +app/tests/test_app.py E [100%] + +========================================================================================================================================================== ERRORS ========================================================================================================================================================== +____________________________________________________________________________________________________________________________________________ ERROR at setup of test_admin_user _____________________________________________________________________________________________________________________________________________ + +request = > + + @pytest.fixture(autouse=True) + def _django_db_marker(request): + """Implement the django_db marker, internal to pytest-django. + + This will dynamically request the ``db``, ``transactional_db`` or + ``django_db_reset_sequences`` fixtures as required by the django_db marker. + """ + marker = request.node.get_closest_marker("django_db") + if marker: + transaction, reset_sequences = validate_django_db(marker) + if reset_sequences: + request.getfixturevalue("django_db_reset_sequences") + elif transaction: + request.getfixturevalue("transactional_db") + else: +> request.getfixturevalue("db") + +venv/lib/python3.6/site-packages/pytest_django/plugin.py:436: +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ +venv/lib/python3.6/site-packages/pytest_django/fixtures.py:108: in django_db_setup + **setup_databases_args +venv/lib/python3.6/site-packages/django/test/utils.py:174: in setup_databases + serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True), +venv/lib/python3.6/site-packages/django/db/backends/base/creation.py:68: in create_test_db + run_syncdb=True, +venv/lib/python3.6/site-packages/django/core/management/__init__.py:110: in call_command + command = load_command_class(app_name, command_name) +venv/lib/python3.6/site-packages/django/core/management/__init__.py:36: in load_command_class + module = import_module('%s.management.commands.%s' % (app_name, name)) +/usr/lib/python3.6/importlib/__init__.py:126: in import_module + return _bootstrap._gcd_import(name[level:], package, level) +venv/lib/python3.6/site-packages/django/core/management/commands/migrate.py:14: in + from django.db.migrations.autodetector import MigrationAutodetector +venv/lib/python3.6/site-packages/django/db/migrations/autodetector.py:11: in + from django.db.migrations.questioner import MigrationQuestioner +venv/lib/python3.6/site-packages/django/db/migrations/questioner.py:9: in + from .loader import MigrationLoader +venv/lib/python3.6/site-packages/django/db/migrations/loader.py:8: in + from django.db.migrations.recorder import MigrationRecorder +venv/lib/python3.6/site-packages/django/db/migrations/recorder.py:9: in + class MigrationRecorder: +venv/lib/python3.6/site-packages/django/db/migrations/recorder.py:22: in MigrationRecorder + class Migration(models.Model): +venv/lib/python3.6/site-packages/django/db/models/base.py:101: in __new__ + new_class.add_to_class('_meta', Options(meta, app_label)) +venv/lib/python3.6/site-packages/django/db/models/options.py:101: in __init__ + self.db_tablespace = settings.DEFAULT_TABLESPACE +_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ + +self = , name = 'DEFAULT_TABLESPACE' + + def __getattr__(self, name): + """Allow getting keys from self.store using dot notation""" + if self._wrapped is empty: + self._setup() + if name in self._wrapped._deleted: # noqa + raise AttributeError( + f"Attribute {name} was deleted, " "or belongs to different env" + ) + + if name not in RESERVED_ATTRS: + lowercase_mode = self._kwargs.get( + "LOWERCASE_READ_FOR_DYNACONF", + default_settings.LOWERCASE_READ_FOR_DYNACONF, + ) + if lowercase_mode is True: + name = name.upper() + + if ( + name.isupper() + and ( + self._wrapped._fresh + or name in self._wrapped.FRESH_VARS_FOR_DYNACONF + ) + and name not in dir(default_settings) + ): + return self._wrapped.get_fresh(name) +> value = getattr(self._wrapped, name) +E AttributeError: 'Settings' object has no attribute 'DEFAULT_TABLESPACE' + +venv/lib/python3.6/site-packages/dynaconf/base.py:164: AttributeError +================================================================================================================================================= short test summary info ================================================================================================================================================== +ERROR app/tests/test_app.py::test_admin_user - AttributeError: 'Settings' object has no attribute 'DEFAULT_TABLESPACE' +===================================================================================================================================================== 1 error in 0.25s ===================================================================================================================================================== +``` diff --git a/example/django_pytest_pure/app/__init__.py b/example/django_pytest_pure/app/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest_pure/app/admin.py b/example/django_pytest_pure/app/admin.py new file mode 100644 index 000000000..8c38f3f3d --- /dev/null +++ b/example/django_pytest_pure/app/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/example/django_pytest_pure/app/apps.py b/example/django_pytest_pure/app/apps.py new file mode 100644 index 000000000..200c5984c --- /dev/null +++ b/example/django_pytest_pure/app/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class AppConfig(AppConfig): + name = "app" diff --git a/example/django_pytest_pure/app/migrations/__init__.py b/example/django_pytest_pure/app/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest_pure/app/models.py b/example/django_pytest_pure/app/models.py new file mode 100644 index 000000000..71a836239 --- /dev/null +++ b/example/django_pytest_pure/app/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/example/django_pytest_pure/app/templates/hello.html b/example/django_pytest_pure/app/templates/hello.html new file mode 100644 index 000000000..376becdd0 --- /dev/null +++ b/example/django_pytest_pure/app/templates/hello.html @@ -0,0 +1,18 @@ + + + + + {{ title }} + + + +

{{ message }}

+ + + diff --git a/example/django_pytest_pure/app/tests/__init__.py b/example/django_pytest_pure/app/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest_pure/app/tests/conftest.py b/example/django_pytest_pure/app/tests/conftest.py new file mode 100644 index 000000000..7803503f4 --- /dev/null +++ b/example/django_pytest_pure/app/tests/conftest.py @@ -0,0 +1,8 @@ +import pytest +from django.core.management import call_command + + +@pytest.fixture(scope="session") +def django_db_setup(django_db_setup, django_db_blocker): + with django_db_blocker.unblock(): + call_command("loaddata", "project/fixtures/admin.json") diff --git a/example/django_pytest_pure/app/tests/test_app.py b/example/django_pytest_pure/app/tests/test_app.py new file mode 100644 index 000000000..b7f803aac --- /dev/null +++ b/example/django_pytest_pure/app/tests/test_app.py @@ -0,0 +1,16 @@ +import django + +django.setup() # noqa + +import pytest + +from django.contrib.auth import get_user_model +from django.conf import settings + + +@pytest.mark.django_db +def test_admin_user(): + User = get_user_model() + + assert User.objects.filter(username="admin").exists() + assert settings.FOO == "bar" diff --git a/example/django_pytest_pure/app/urls.py b/example/django_pytest_pure/app/urls.py new file mode 100644 index 000000000..c1e3a7427 --- /dev/null +++ b/example/django_pytest_pure/app/urls.py @@ -0,0 +1,10 @@ +from app import views +from django.urls import include +from django.urls import path + +# namespacing +app_name = "app" + +urlpatterns = [ + path(r"", views.HelloView.as_view(), name="hello"), +] diff --git a/example/django_pytest_pure/app/views.py b/example/django_pytest_pure/app/views.py new file mode 100644 index 000000000..d53f9d5bb --- /dev/null +++ b/example/django_pytest_pure/app/views.py @@ -0,0 +1,11 @@ +from django.views.generic import TemplateView + + +class HelloView(TemplateView): + template_name = "hello.html" + + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["title"] = "Hello!" + context["message"] = "Hello world!" + return context diff --git a/example/django_pytest_pure/manage.py b/example/django_pytest_pure/manage.py new file mode 100755 index 000000000..f522e0072 --- /dev/null +++ b/example/django_pytest_pure/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/example/django_pytest_pure/project/__init__.py b/example/django_pytest_pure/project/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/example/django_pytest_pure/project/fixtures/admin.json b/example/django_pytest_pure/project/fixtures/admin.json new file mode 100644 index 000000000..1c826649a --- /dev/null +++ b/example/django_pytest_pure/project/fixtures/admin.json @@ -0,0 +1,20 @@ +[ + { + "model": "auth.user", + "pk": 1, + "fields": { + "password": "pbkdf2_sha256$120000$hzEDlLe2T6O2$YVDIso96yOXVq6+IcD0Iy1K6He/x9lzM5IFQU64UeKg=", + "last_login": null, + "is_superuser": true, + "username": "admin", + "first_name": "", + "last_name": "", + "email": "", + "is_staff": true, + "is_active": true, + "date_joined": "2020-12-15T08:31:26.387Z", + "groups": [], + "user_permissions": [] + } + } +] diff --git a/example/django_pytest_pure/project/settings.py b/example/django_pytest_pure/project/settings.py new file mode 100644 index 000000000..27312417c --- /dev/null +++ b/example/django_pytest_pure/project/settings.py @@ -0,0 +1,120 @@ +""" +https://docs.djangoproject.com/en/1.11/topics/settings/ +https://docs.djangoproject.com/en/1.11/ref/settings/ +https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ +""" +import os +from collections import OrderedDict + +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "yeicahshiquohc3Mae8ahl9yah7fa9eiCh6ooyiefaeheipiobe1fikohL6ahpho" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = os.environ.get("DJANGO_DEBUG", "false").lower() == "true" + +if DEBUG: + EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" + +ALLOWED_HOSTS = ["*"] + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.humanize", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "app", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "project.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.media", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "project.wsgi.application" + +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": os.path.join(".", "db.sqlite3"), + } +} + +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": ( + "django.contrib.auth.password_validation." + "UserAttributeSimilarityValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." "MinimumLengthValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." + "CommonPasswordValidator" + ), + }, + { + "NAME": ( + "django.contrib.auth.password_validation." + "NumericPasswordValidator" + ), + }, +] + +# https://docs.djangoproject.com/en/1.11/topics/i18n/ +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + +# https://docs.djangoproject.com/en/1.11/howto/static-files/ +STATIC_URL = "/static/" +STATIC_ROOT = "static" + + +STATICFILES_FINDERS = [ + "django.contrib.staticfiles.finders.FileSystemFinder", + "django.contrib.staticfiles.finders.AppDirectoriesFinder", +] + +FOO = "bar" diff --git a/example/django_pytest_pure/project/urls.py b/example/django_pytest_pure/project/urls.py new file mode 100644 index 000000000..b32ab9d8f --- /dev/null +++ b/example/django_pytest_pure/project/urls.py @@ -0,0 +1,10 @@ +# https://docs.djangoproject.com/en/1.10/topics/http/urls/ +from django.contrib import admin +from django.urls import include +from django.urls import path +from django.views.generic import RedirectView + +urlpatterns = [ + path("", include("app.urls")), + path("admin/", admin.site.urls), +] diff --git a/example/django_pytest_pure/project/wsgi.py b/example/django_pytest_pure/project/wsgi.py new file mode 100644 index 000000000..1f6156f21 --- /dev/null +++ b/example/django_pytest_pure/project/wsgi.py @@ -0,0 +1,8 @@ +# https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings") + +application = get_wsgi_application() diff --git a/example/django_pytest_pure/pytest.ini b/example/django_pytest_pure/pytest.ini new file mode 100644 index 000000000..596dc0964 --- /dev/null +++ b/example/django_pytest_pure/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +DJANGO_SETTINGS_MODULE = project.settings +python_files = tests.py test_*.py *_tests.py diff --git a/example/django_pytest_pure/requirements.txt b/example/django_pytest_pure/requirements.txt new file mode 100644 index 000000000..e15c59280 --- /dev/null +++ b/example/django_pytest_pure/requirements.txt @@ -0,0 +1,4 @@ +Django==2.1.5 +dynaconf==3.1.2 +pytest==5.4.3 +pytest-django==4.1.0 diff --git a/example/django_pytest_pure/setup.sh b/example/django_pytest_pure/setup.sh new file mode 100755 index 000000000..072805534 --- /dev/null +++ b/example/django_pytest_pure/setup.sh @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail + +python3 -m venv venv +source venv/bin/activate +pip install -r requirements.txt +source .env +./manage.py migrate +./manage.py collectstatic --noinput +./manage.py loaddata project/fixtures/admin.json diff --git a/example/django_pytest_pure/tests.sh b/example/django_pytest_pure/tests.sh new file mode 100755 index 000000000..909057cfc --- /dev/null +++ b/example/django_pytest_pure/tests.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -euo pipefail + +source venv/bin/activate +pytest diff --git a/tests/test_django.py b/tests/test_django.py index a28948d2c..92e3bd0a5 100644 --- a/tests/test_django.py +++ b/tests/test_django.py @@ -6,7 +6,7 @@ def test_djdt_382(tmpdir): settings_file = tmpdir.join("settings.py") - settings_file.write("SECRET_KEY = 'dasfadfds2'") + settings_file.write("\n".join(["SECRET_KEY = 'dasfadfds2'"])) tmpdir.join("__init__.py").write("") os.environ["DJANGO_SETTINGS_MODULE"] = "settings" sys.path.append(str(tmpdir)) @@ -19,7 +19,7 @@ def test_djdt_382(tmpdir): def test_override_settings_596(tmpdir): settings_file = tmpdir.join("other_settings.py") - settings_file.write("SECRET_KEY = 'abcdef'") + settings_file.write("\n".join(["SECRET_KEY = 'abcdef'"])) tmpdir.join("__init__.py").write("") os.environ["DJANGO_SETTINGS_MODULE"] = "other_settings" sys.path.append(str(tmpdir))