在django nose测试中安装hstore扩展

0at*_*man 8 python django postgresql nose hstore

我已成功安装了hstore扩展,一切都可以正常工作syncdb.(我正在使用djorm-ext-hstore)

但是,nose会创建一个新的临时数据库来运行测试,并且没有安装hstore.

我需要CREATE EXTENSION HSTORE;在鼻子同步数据库之前运行测试数据库,但我找不到有关如何执行此操作的任何信息.

有任何想法吗?

0at*_*man 26

这不是问题:解决此问题的最佳方法是在默认数据库上应用hstore扩展, template1

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

参考:如何创建已安装hstore扩展的新数据库?


Ris*_*nha 7

使用Django 1.8:

from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [
        HStoreExtension(),
        ...
    ]
Run Code Online (Sandbox Code Playgroud)

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

编辑:请注意,还有一个JSONField处理(un)编组json已经和内联搜索.HStoreExtension不是必需的.需要Django> = 1.11和Postgres> = 9.4.


Den*_*sky 4

我假设您正在使用django-nose。在这种情况下,您应该创建自己的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
Run Code Online (Sandbox Code Playgroud)

然后settings.py您应该指定您的自定义测试运行程序:

TEST_RUNNER = 'path.to.my.module.MyTestSuiteRunner'
Run Code Online (Sandbox Code Playgroud)