Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test database looks empty while test is running

I have a Django project that uses local PostgreSQL server. I'm using a debugger to debug some weird bugs that I have. While the debugger is stopped on one of the tests, I tried to look at the test database. I see this new database (test_project1) and all the schemes are defined as they should be. But all tables are empty.

I know that the tables are not empty:

  • I used a fixture and some tests already ran and returned data.
  • A post test created a new user and returned 201 status code.

And yet, I see no data when I try to access the database with pgAdmin3 or psql.

Any idea what is going on here? Is there some kind of sophisticated cache mechanism that Django uses?

like image 349
Uzi Avatar asked Apr 28 '12 20:04

Uzi


2 Answers

Django's TestCase is wrapping every test in its own transaction. So your database is not being used any time you do a request through ORM.

like image 136
ilvar Avatar answered Sep 24 '22 23:09

ilvar


The database transaction that is being used by django.test.TestCase can be avoided by inheriting from django.test.TransactionTestCase instead of TestCase. Then the data will be visible in the database.

You might want to just do this temporarily while debugging, so that you get the performance benefits of django.test.TestCase the rest of the time.

Django docs:
django.test.TransactionTestCase
django.test.TestCase

like image 41
Rob Bednark Avatar answered Sep 22 '22 23:09

Rob Bednark