[Django] #31414: Django TestCase reads from "production" database with MySQL backend

17 views
Skip to first unread message

Django

unread,
Mar 31, 2020, 7:20:03 PM3/31/20
to django-...@googlegroups.com
#31414: Django TestCase reads from "production" database with MySQL backend
---------------------------------------------+------------------------
Reporter: Danilo Favato | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
The [Django
documentation](https://docs.djangoproject.com/en/3.0/topics/testing/overview
/#the-test-database) states that:

**Finding data from your production database when running tests?**
If your code attempts to access the database when its modules are
compiled, this will occur before the test database is set up, with
potentially unexpected results. For example, if you have a database query
in module-level code and a real database exists, **production data could
pollute your tests.**

However, the above mentioned behavior can occur in a brand new Django
project using `mysql`backend.

== How to reproduce the error:
1. Start a new project and apply django migrations
2. Change database backend to `django.db.backends.mysql`
3. Create a new user by running `./manage.py createsuperuser`
4. Add the following test module to the project

{{{
#!div style="font-size: 80%"
Code highlighting:
{{{#!python
# test.py
from django.test import TestCase
from django.contrib.auth.models import User

class TestUserCount(TestCase):
def test_empty_db(self):
self.assertEqual(0, User.objects.count())
}}}
}}}

The test above fails with `AssertionError: 0 != 1` but passes if you use a
`sqlite` backend.


Environment:
* Python 3.8.1
* Django 3.0.4
* mysqlclient 1.4.6
* MariaDB 10.4.12

--
Ticket URL: <https://code.djangoproject.com/ticket/31414>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 1, 2020, 1:14:00 AM4/1/20
to django-...@googlegroups.com
#31414: Django TestCase reads from "production" database with MySQL backend.
-----------------------------------+--------------------------------------

Reporter: Danilo Favato | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: 3.0
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => needsinfo


Old description:

New description:

The [https://docs.djangoproject.com/en/3.0/topics/testing/overview/#the-
test-database Django documentation] states that:

--

Comment:

I wasn't able to reproduce this issue (tested with both engines `InnoDB`
and `MyISAM`).

--
Ticket URL: <https://code.djangoproject.com/ticket/31414#comment:1>

Django

unread,
Apr 1, 2020, 1:14:31 AM4/1/20
to django-...@googlegroups.com
#31414: Django TestCase reads from "production" database with MySQL backend.
-----------------------------------+--------------------------------------

Reporter: Danilo Favato | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: 3.0
Severity: Normal | Resolution: worksforme

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by felixxm):

* resolution: needsinfo => worksforme


--
Ticket URL: <https://code.djangoproject.com/ticket/31414#comment:2>

Reply all
Reply to author
Forward
0 new messages