» Django TestCase: dont flush the DB after each test
Django TestCase: dont flush the DB after each test
|August 21, 2014
Posted by forumadmin
I’m working on a Django API project with a rather unusual configuration (I think):
I have indeed two Django projects: one is the main API and one is the user API. Whenever I create a user using the main API, the user is in fact created in the database of the user API (I communicate between the two API using http requests). In the main API, I keep a table of users that contains only a unique id. When the user is created in the user API, it’s created with the same unique id as in the main API.
I have to do this because in production, I have to store the data in different servers.
Now comes my problem.
I want to write tests for my main API (for instance test the user creation, user update, user deletion). The problem is that when I run the tests, I have to run a separate instance of Django (using another port) that represents the user API. Running the tests, Django creates a test database for the main API but since I use http requests to communicate with the user API, there is no test database for the user API so I have to flush the DB after I run all the tests. Until now, I used the unittest library and everything was fine. But I would like to be able to override some settings during the tests (for instance, the address of the user API is a setting and I would like to have a different address for the tests). For that, I have to use django.test.TestCase but I have the following problem:
imagine I have a test_a method that creates a user A and a test_b method that creates a user B. With django.test.TestCase, test_a is run, user A is created with id 1. Then I believe that the test database (of the main API) is flushed because when test_b is run, user B is created with id 1 also. The problem is that between the two tests, the database of the user API is not flushed so I get an error because I cannot create user B in the test database.
I’m looking for an elegant way to deal with this problem but I really have not idea.
(Sorry, this is quite long but I wanted to be a little bit precise).
More Related Questions
- testing django: reason for unexpected http status code We have many unitests in our django application.
But if a tests fails because the status code does not match:
Traceback (most recent call last):
- Django object not saving even after "save" call I'm really at a loss because this makes no sense at all. I call save/create an object, and it dosn't show up in the admin site. I even checked the SQLite database with a SQLite Viewer […]
- Implementing matching in database I am working on a project where people choose certain slots everyday. Then I match people who are in the same slot with each other. I am using a separate table for implementing this. The […]
- False 404 from Django test? I'm having trouble with testing my Django app. I can load a page in the browser just fine, but my test code returns 404.
This test returns False:
response = […]
- approximate search in a database I have a large database with a list of institutions (universities, hospitals, etc). The names of institutions come from different sources and can be spelled differently for the same […]
- Assembly.GetEntryAssembly().CodeBase precludes unit testing I'm writing a logging library. I want the library, by default, to write to a directory in the common application data folder named for the application. For example, if the application is […]
- how to restore dropped table with django-south? I wanted to clear out a table in my DB, so I dropped the table. Normally I would do manage.py syncdb to re-create it. However, the answer here says not to use syncdb anymore. So, what do I […]
- How to separate features among website normal & premium users I'm making a website in django. My website will have both paid and unpaid content. I have no idea how to open some features only for paid users or how to protect my premium data from […]
- Whats the best strategy for unit-testing database-driven applications? I work with a lot of web applications that are driven by databases of varying complexity on the backend. Typically, there's an ORM layer separate from the business and presentation logic. […]
- How to write unit tests for database calls I'm near the beginning of a new project and (gasp!) for the first time ever I'm trying to include unit tests in a project of mine.
I'm having trouble devising some of the unit tests […]