Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing hstore extension in django nose tests

I've installed the hstore extension successfully, and everything works when I syncdb. (I'm using djorm-ext-hstore)

However, nose creates a new temp database to run tests in, and hstore is not installed in it.

I need to run CREATE EXTENSION HSTORE; on the test db right before nose syncs the db, but I can't find any info on how to do that.

Any ideas?

like image 531
0atman Avatar asked May 03 '13 09:05

0atman


3 Answers

This is a non-issue: The best way to fix this is to apply the hstore extension on the default database, template1

psql -d template1 -c 'create extension hstore;'

Reference: How to create a new database with the hstore extension already installed?

like image 161
0atman Avatar answered Oct 17 '22 03:10

0atman


With Django 1.8 (which is outdated now, but it still exists in 3.2):

from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [
        HStoreExtension(),
        ...
    ]

https://docs.djangoproject.com/en/3.2/ref/contrib/postgres/fields/#hstorefield

EDIT: Just note that there is also a JSONField which handles (un)marshalling of json already and inline search. The HStoreExtension is not necessary for it. Requires Django >=1.11 and Postgres >=9.4.

like image 8
Risadinha Avatar answered Oct 17 '22 05:10

Risadinha


I'm assuming you're using django-nose. In this case you should create your own TestSuiteRunner:

from django.db import connections, DEFAULT_DB_ALIAS
from django_nose import NoseTestSuiteRunner

class MyTestSuiteRunner(NoseTestSuiteRunner):
    def setup_databases(self):
        result = super(MyTestSuiteRunner, self).setup_databases()

        connection = connections[DEFAULT_DB_ALIAS]
        cursor = connection.cursor()
        cursor.execute('CREATE EXTENSION HSTORE')

        return result

Then in settings.py you should specify your custom test runner:

TEST_RUNNER = 'path.to.my.module.MyTestSuiteRunner'
like image 4
Denis Malinovsky Avatar answered Oct 17 '22 05:10

Denis Malinovsky