Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Django MySQL tests in memory

I have a django 1.4 project using mysql as the backend. I have the tests setup to run in memory

if 'test' in sys.argv:
  DATABASES['default'] = {'ENGINE': 'django.db.backends.sqlite3'}

The issue is I need to use mysql functionality (full text indexes).

Is there a way to have django run MySQL in memory for testing?

My project relies on fulltext indexes. When the project is being developed on I syncdb then execute a .sql file with the sql to create the full text indexes.

I would like to use django orm full text searching in the functions I test. I am trying to manually add the fulltext index on each tests' initialization like:

cursor.execute('alter table mytable add fulltext(one, two)')

This does not work when using sqlite (I believe because sqlite does not support Fulltext indexing)

The above sql DOES work when I remove the in memory tests. I like the speed of in memory tests. Is there a way I can run mysql in memory?

How do people test apps that rely on database specific features? like full text indexing or gis, etc... Do they have to run the tests normally on the filesystem?

Thank you

like image 251
dm03514 Avatar asked Dec 12 '22 17:12

dm03514


1 Answers

MySQL has a MEMORY storage engine. You can activate it using the OPTIONS key:

if 'test' in sys.argv:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'localhost',
            'NAME': 'foo',
            'USER': 'bar',
            'PASSWORD': 'baz',
            'OPTIONS': {
                'init_command': 'SET storage_engine=MEMORY'
            }
        }
    }

BUT according to the documentation:

MEMORY tables cannot contain BLOB or TEXT columns.

Therefore i suppose it's pretty useless for your (and many other) use cases.

I found a few other tips for speeding up MySQL tests in this thread. Make sure to read Daniel Roseman's answer about using the INNODB engine.

like image 187
Dirk Eschler Avatar answered Dec 22 '22 15:12

Dirk Eschler