3

我正在尝试为 Django 制作一个最小的工作示例。这应该是一个文件,它允许在内存数据库中定义和实例化模型,然后可以将其用于 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()

该脚本可以运行单元测试,但会引发错误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
4

1 回答 1

2

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

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

这意味着该模块也将作为mwe.modelsbysyncdb和 as mweby translation 导入:

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 版本,但我们必须尝试看看。

于 2013-04-05T00:40:16.857 回答