0

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).

1 Answers1

0

Can't you do the DB flushing in the setUp method of your TestCase? Since that method runs once before each test you can have a clean DB for test_a and test_b.

To flush the db using a bash script, you can use subprocess, like so:

def setUp(self):
    import subprocess
    subprocess.call(['<path-to-bash-script>', 'arg1', 'arg2'])
axelcdv
  • 753
  • 6
  • 18
  • The problem is that the DB that's being flushed is in another Django project so I don't do the flushing in the django code but in a bash script that runs the test. How could I flush the DB of one django project from another django project? – Hummingbird Aug 21 '14 at 12:26
  • Well you can call the bash script from the setup method, with subprocess for example. I'll add an example in my answer – axelcdv Aug 21 '14 at 12:28
  • The bash script works great if I run it from the command line but not if I run it from subprocess.call. I get this error: File "/user_API_path/manage.py", line 10, in execute_from_command_line(sys.argv) ImportError: Could not import settings 'core_API.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named core_API.settings – Hummingbird Aug 21 '14 at 12:38
  • This looks like a virtualenv issue, I guess you usually run your bash script from within the user project's environment? In that case you can either try to modify the script to have an absolute path to your python, or you can modify the environment variables before running subprocess.call (see http://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment for example) – axelcdv Aug 21 '14 at 12:44
  • I use indeed virtualenv but the path in my bashrc are absolute (see https://gist.github.com/anonymous/0a16a03bb811f576fe90) – Hummingbird Aug 21 '14 at 12:49