测试Django模型的样板:寻求便携式单文件解决方案

时间:2013-03-27 16:23:55

标签: django django-models

我正在尝试为Django制作minimal working example。这应该是一个文件,它允许在内存数据库中定义和实例化模型,然后可以将其用于stackoverflow问题。如果可能的话,我希望它使用django / unittest框架,因为这样可以很容易地演示模型行为的问题。

Stackoverflow上有很多问题可以从这样的事情中受益。

到目前为止我管理的内容:

# Django Minimal Working Example
# (intended) usage: save as `mwe.py` and run `python mwe.py`

# Settings
from django.conf import settings
settings.configure(DATABASES = {
        'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}
        })

# Models
# with help from http://softwaremaniacs.org/blog/2011/01/07/django-micro-framework/en/
from django.db import models
import sys

sys.modules['mwe.mwe'] = sys.modules[__name__]
sys.modules[__name__].__name__ = 'mwe.mwe'
__package__ = 'mwe.mwe'

class Book(models.Model):
    isbn = models.IntegerField()

# Make a database and do things to it
from django.utils import unittest
from django.test import TestCase

class TestCreateObjects(TestCase):

    def setUp(self):
        book = Book.objects.create(isbn='9780470467244')

    def test_sanity(self):
        self.assertEqual(Book.objects.count(), 1)

unittest.main()

此脚本可以运行unittest,但会引发错误django.db.utils.DatabaseError: no such table: mwe_book

修改我还尝试将from django.utils import unittest中的行替换为:

from django.core.management import call_command
call_command('syncdb', interactive=False)
book = Book.objects.create(isbn='9780470467244')

在提出与上述相同的DatabaseError之前,会给出以下反馈:

Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

摘要感谢Pavel的帮助,这是有效的。作为参考,这里是脚本的紧凑版本减去无关的单元测试内容:

# Django Minimal Working Example
#   usage: save as `mwe.py` and run `python mwe.py`

# Setup django with an in-memory sqlite3 database
#   Thanks to Pavel Anossov http://stackoverflow.com/a/15824108/188595
import sys
from django.conf import settings
from django.core.management import call_command
settings.configure(
        DATABASES = {
            'default': {'ENGINE': 'django.db.backends.sqlite3',
                        'NAME': ':memory:'}
        },
        INSTALLED_APPS = ('mwe',),
    )
from django.db import models
sys.modules['mwe.models'] = sys.modules['mwe.mwe'] = sys.modules['mwe'] = sys.modules[__name__]
sys.modules[__name__].__name__ = __package__ = 'mwe.mwe'

# YOUR MODEL DEFINITIONS HERE

class Book(models.Model):
    isbn = models.IntegerField()

# The call_command line has to appear after all model definitions
call_command('syncdb', interactive=False)

# YOUR CODE HERE

Book.objects.create(isbn='9780470467244')
assert Book.objects.count() == 1

1 个答案:

答案 0 :(得分:2)

您缺少的是实际安装应用程序:

settings.configure(
        DATABASES = {
            'default': {'ENGINE': 'django.db.backends.sqlite3',
                        'NAME': ':memory:'}
        },
        INSTALLED_APPS = ('mwe',),
    )

这意味着此模块也将mwe.models导入为syncdb,翻译为mwe

sys.modules['mwe.models'] = sys.modules[__name__]
sys.modules['mwe'] = sys.modules[__name__]

我的最终版本:

from django.conf import settings
import sys

settings.configure(
        DATABASES = {
            'default': {'ENGINE': 'django.db.backends.sqlite3',
                        'NAME': ':memory:'}
        },
        INSTALLED_APPS = ('mwe',),
    )


from django.core.management import call_command
from django.utils import unittest
from django.db import models
from django.test import TestCase

sys.modules['mwe.models'] = sys.modules[__name__]
sys.modules['mwe.mwe'] = sys.modules[__name__]
sys.modules['mwe'] = sys.modules[__name__]
sys.modules[__name__].__name__ = 'mwe.mwe'
__package__ = 'mwe.mwe'


class Book(models.Model):
    isbn = models.IntegerField()


class TestCreateObjects(TestCase):
    def setUp(self):
        book = Book.objects.create(isbn='9780470467244')

    def test_sanity(self):
        self.assertEqual(Book.objects.count(), 1)

call_command('syncdb', interactive=False)
unittest.main()

$ python mwe.py
Creating tables ...
Creating table mwe_book
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
.
----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

我确信这会以一些可怕的方式破坏,并且依赖于django版本,但我们必须尝试看看。