Django测试抱怨丢失表

当我运行我的测试处理我的客户模型,我得到以下错误:

DatabaseError: (1146, "Table 'test_mcif2.customer' doesn't exist")

我并不惊讶,因为我的Django项目连接到一个“遗留”数据库.由于我的表没有创建“Django方式”,所以Django无法与他们交谈,这并不令人震惊.这是我的模型:

from django.db import models
from django.db import connection, transaction
from mcif.models.mcif_model import McifModel

class Customer(McifModel):

    class Meta:
        db_table = u'customer'
        app_name = 'mcif'

    id = models.BigIntegerField(primary_key=True)
    customer_number = models.CharField(unique=True, max_length=255)
    social_security_number = models.CharField(unique=True, max_length=33)
    name = models.CharField(unique=True, max_length=255)
    phone = models.CharField(unique=True, max_length=255)
    deceased = models.IntegerField(unique=True, null=True, blank=True)
    do_not_mail = models.IntegerField(null=True, blank=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    def distinguishing_column_names(self):
        return ['name', 'customer_number', 'social_security_number', 'phone']

任何想法为什么这不工作?

编辑:这里是McifModel:

from django.db import models
from django.db import connection, transaction

class McifModel(models.Model):
    class Meta:
        abstract = True

    def upsert(self):
        cursor = connection.cursor()
        cursor.execute(self.upsert_sql())
        transaction.commit_unless_managed()
        return self

    def value_list(self):
        return ','.join(map(lambda column_name: "'{c}'".format(c=getattr(self, column_name)), self.distinguishing_column_names()))

    def upsert_sql(self):
        column_names = ','.join(self.distinguishing_column_names())
        return "INSERT IGNORE INTO {t} ({c}) VALUES ({v})".format(t=self._meta.db_table, c=column_names, v=self.value_list())

    @classmethod
    def save_from_row(cls, row):
        object = cls()
        map(lambda column_name: setattr(object, column_name, row.value(object._meta.db_table, column_name)), object.distinguishing_column_names())
        return object.upsert()

编辑:我采取了tarequeh的建议,并将Caktus文件的内容放在mcif / utils.py中.我也设置了TEST_RUNNER =’mcif.utils.ManagedModelTestRunner’.如果我进入控制台,我可以验证客户是不管理的:

>>> [m for m in get_models() if not m._meta.managed]
[<class 'mcif.models.customer.Customer'>]

但是,我的测试仍然抱怨表不存在.我失踪了什么

这是我的settings.py:

# Django settings for mcifdjango project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Jason Swett', 'jason.swett@gmail.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'xxxxx',                # Or path to database file if using sqlite3.
        'USER': 'xxxxx',                       # Not used with sqlite3.
        'PASSWORD': 'xxxxx',           # Not used with sqlite3.
        'HOST': '',                           # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                           # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Chicago'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale
USE_L10N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '#7+qm%hqfe+z8ul5@x_i&sqmu!n=4sa0&i0_#)m99*w$fbk3%#'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'mcifdjango.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.admin',
    'django_extensions',
    'mcif',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

TEST_RUNNER = 'mcif.utils.ManagedModelTestRunner'

import os
ROOTDIR = os.path.abspath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    ROOTDIR + '/mcif/templates',
)

编辑2:

现在我的客户课程是:

from django.db import models
from django.db import connection, transaction
from mcif.models.mcif_model import McifModel

class Customer(McifModel):

    class Meta:
        db_table = u'customer'
        managed = False

    id = models.BigIntegerField(primary_key=True)
    customer_number = models.CharField(unique=True, max_length=255)
    social_security_number = models.CharField(unique=True, max_length=33)
    name = models.CharField(unique=True, max_length=255)
    phone = models.CharField(unique=True, max_length=255)
    deceased = models.IntegerField(unique=True, null=True, blank=True)
    do_not_mail = models.IntegerField(null=True, blank=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    def distinguishing_column_names(self):
        return ['name', 'customer_number', 'social_security_number', 'phone']

这是我在运行测试时所得到的:

$./manage.py test mcif.CustomerUpsertTest
Creating test database 'default'...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
Installing index for admin.LogEntry model
No fixtures found.
E
======================================================================
ERROR: test_upsert (mcif.tests.customer_upsert_test.CustomerUpsertTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/jason/projects/mcifdjango/mcif/tests/customer_upsert_test.py", line 9, in test_upsert
    customer.upsert()
  File "/home/jason/projects/mcifdjango/mcif/models/mcif_model.py", line 11, in upsert
    cursor.execute(self.upsert_sql())
  File "/usr/lib/pymodules/python2.6/django/db/backends/mysql/base.py", line 86, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 166, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
    raise errorclass, errorvalue
DatabaseError: (1146, "Table 'test_mcif_django.customer' doesn't exist")

----------------------------------------------------------------------
Ran 1 test in 3.724s

FAILED (errors=1)
Destroying test database 'default'...
最佳答案
由于您使用旧版数据库,您可能不会将应用名称添加到INSTALLED_APPS.如果应用程序未包含在INSTALLED_APPS中,则不会在syncdb上创建应用程序模型的表.由于您已经有了表,而是在测试环境中,因此在生产过程中适用于您.

您可以采取以下任何一种:

> supermonkeypatch方法:从客户类Meta中取出app_name,将模型放在一个模型名称为mcif的模型文件中,并将mcif添加到INSTALLED_APPS中 – 仅仅是为了测试
>更好的方法:扩展DjangoTestSuiteRunner并覆盖setup_test_environment以调用super,然后在测试数据库中手动创建遗留表.
>最好的方法:将你的模型放在正确命名的应用程序模块中.从模型Meta中删除app_name,但添加managed = False docs.在INSTALLED_APPS中包含应用程序名称.现在django将不会为该模型创建表.然后使用这个nice snippet的Caktus组人编译来运行你的测试.

干杯!

编辑 – 如何使用被覆盖的DjangoTestSuiteRunner

您将至少需要Django 1.2.

复制代码从here.把它放在utils.py里面的mcif应用程序.

在settings.py中添加/编辑以下内容:

TEST_RUNNER = 'mcif.utils.ManagedModelTestRunner'

现在当您运行测试时,所有非托管表将仅在测试期间被视为托管表.所以表将在运行测试之前创建.

注意这部分代码,这就是魔术的发生.

self.unmanaged_models = [m for m in get_models() if not m._meta.managed]
for m in self.unmanaged_models:
    m._meta.managed = True

第二编辑:可能的Gotchas

确保以下内容:

> DB用户有权创建数据库而不仅仅是表,因为django将尝试创建一个测试数据库
>测试用例扩展django.test.TransactionTestCase,因为您有事务性行为
>如果上述都不适用,请将ManagedModelTestRunner的setup_test_environment中的pdb放在一起,以确保正在接收代码.因为如果达到这个代码,那么应该创建表

第3编辑:调试
里面的mcif.utils.ManagedModelTestRunner用下面的代码替换setup_test_environment函数,让我知道你测试的输出是否改变了:

def setup_test_environment(self, *args, **kwargs):
    print "Loading ManagedModelTestRunner"
    from django.db.models.loading import get_models
    self.unmanaged_models = [m for m in get_models()
                             if not m._meta.managed]
    for m in self.unmanaged_models:
        print "Modifying model %s to be managed for testing" % m
        m._meta.managed = True
    super(ManagedModelTestRunner, self).setup_test_environment(*args, **kwargs)

转载注明原文:Django测试抱怨丢失表 - 代码日志