Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running django with a sqlite in memory database

I am using Robot Framework for my acceptance testing.

To start the django server I run the python manage.py runserver command from the RobotFramework. After that, I call python manage.py migrate. But the tests are slow because is not using an in-memory database.

I tried creating a new setting file called testing and I an using the following configuration:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

and setting the enviroment variable DJANGO_SETTINGS_MODULE at runtime with this config.

I run a migrate followed by a runserver but the database does not exist. I can't figure out why. If I use a physical database it works.

I tried to check how the Django TestCase works to run and create the database but I could not find it.

like image 241
Carlos Goce Avatar asked Nov 27 '14 15:11

Carlos Goce


1 Answers

I try to simulate make migrate before running test cases by calling execute_from_command_line() manually.

So the command I choose is: (removing poetry run if you don't use poetry).

poetry run ./manage.py test --settings=myproject.settings_test

I put this command into GNUMakefile to allow me use make test.

Then inside myproject/settings_test.py:

from .settings import *

ALLOWED_HOSTS = [ '127.0.0.1', ]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': ':memory:',
    }
}

from django.core.management import execute_from_command_line
execute_from_command_line(['./manage.py', 'migrate'])
like image 170
Gea-Suan Lin Avatar answered Sep 20 '22 19:09

Gea-Suan Lin