Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31414 closed Bug (worksforme)

Django TestCase reads from "production" database with MySQL backend.

Reported by: Danilo Favato Owned by: nobody
Component: Testing framework Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

The Django documentation 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 mysqlbackend.

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

Code highlighting:

# 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

Change History (2)

comment:1 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Resolution: needsinfo
Status: newclosed
Summary: Django TestCase reads from "production" database with MySQL backendDjango TestCase reads from "production" database with MySQL backend.

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

comment:2 by Mariusz Felisiak, 4 years ago

Resolution: needsinfoworksforme
Note: See TracTickets for help on using tickets.
Back to Top