pytest-dev / pytest-dev/pytest-django
django_db_block access wrong database bug
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 1.5k
- Forks
- 367
- PR merge metrics
- No merged PRs in 30d
Description
when i tried this command pytest -s --import-mode=importlib it works fine.
but when i tried this pytest -s --import-mode=importlib app/tests/test_filename.py
first time was a general fail output but after that i got this error:
===================================================================================== ERRORS =====================================================================================
_________________________________________________________________ ERROR at setup of test_endpoint_containerList __________________________________________________________________
self = <django.db.backends.utils.CursorWrapper object at 0x107070190>
sql = 'INSERT INTO "auth_user" ("password", "last_login", "is_superuser", "username", "first_name", "last_name", "email", "is_staff", "is_active", "date_joined") VALUES (%s, %s,
%s, %s, %s, %s, %s, %s, %s, %s) RETURNING "auth_user"."id"'
params = ('pbkdf2_sha256$180000$UlJQcZHRuts0$jpA8wYudq5I+QdAPXWe6lvqU7V4t3CvADtn4iXpfR64=', None, False, 'test', '', '', ...)
ignored_wrapper_args = (False, {'connection': <django.db.backends.postgresql.base.DatabaseWrapper object at 0x1068fd290>, 'cursor': <django.db.backends.utils.CursorWrapper object
at 0x107070190>})
def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
# params default might be backend specific.
return self.cursor.execute(sql)
else:
> return self.cursor.execute(sql, params)
E psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "auth_user_username_key"
E DETAIL: Key (username)=(test) already exists.
../../.pyenv/versions/3.7.4/envs/dsl/lib/python3.7/site-packages/django/db/backends/utils.py:86: UniqueViolation
The above exception was the direct cause of the following exception:
django_db_blocker = <pytest_django.plugin._DatabaseBlocker object at 0x105377f90>
@pytest.fixture(scope='session')
def test_client(django_db_blocker):
with django_db_blocker.unblock():
> User.objects.create_user('test', 'test@test.com', 'test123')
conftest.py:20:
i checked my postgres database test_dbname first and select auth_user this table and it was empty i couldn't find this user i saved. i found it insert to my original database dbname auth_user table. it only occurred when i test specific test file
my conftest.py
@pytest.fixture(scope='session')
def test_client(django_db_blocker):
with django_db_blocker.unblock():
User.objects.create_user('test', 'test@test.com', 'test123')
client = APIClient()
client.login(username='test', password='test123')
return client
my project structure
│ conftest.py
│
└───app_folder
│ │ apps.py
│ │ models.py
│ │ ...
│ └───tests
│ │ test_file1.py
│ │ test_file2.py
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with conftest.py:20 and the session-scoped test_client fixture using django_db_blocker.unblock(). Reproduce with pytest -s --import-mode=importlib and then the app/tests/test_filename.py form, checking which database receives the auth_user insert. Done means the specific-file run uses the intended test database without the duplicate-user failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- django, postgresql, python
- Domain
- backend, databases, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100