我正在使用Django框架运行单元测试并得到此错误。
运行实际代码没有这个问题,运行单元测试会动态创建一个测试数据库,所以我怀疑问题就在那里。
抛出错误的代码如下所示
member = Member.objects.get(email=email_address)
,模型看起来像
class Member(models.Model):
member_id = models.IntegerField(primary_key=True)
created_on = models.DateTimeField(editable=False, default=datetime.datetime.utcnow())
flags = models.IntegerField(default=0)
email = models.CharField(max_length=150, blank=True)
phone = models.CharField(max_length=150, blank=True)
country_iso = models.CharField(max_length=6, blank=True)
location_id = models.IntegerField(null=True, blank=True)
facebook_uid = models.IntegerField(null=True, blank=True)
utc_offset = models.IntegerField(null=True, blank=True)
tokens = models.CharField(max_length=3000, blank=True)
class Meta:
db_table = u'member'
我看不到太奇怪了。
运行测试的用户与运行网站的用户具有相同的数据库服务器权限
这是在osx上运行的mariadb上的django 1.1
MJ-2:mysite Marsh$ python manage.py test sitecoming
Creating test database...
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table djangodblog_errorbatch
Creating table djangodblog_error
Installing index for djangodblog.ErrorBatch model
Installing index for djangodblog.Error model
E
======================================================================
ERROR: test_index (mysite.sitecoming.tests.SiteComingTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/Marsh/Development/deal/src/mysite/sitecoming/tests.py", line 19, in test_index
response = c.post('/submit', {'email':'marshall@offby3.com'})
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/test/client.py", line 313, in post
response = self.request(**r)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py", line 92, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/Users/Marsh/Development/deal/src/mysite/sitecoming/views.py", line 49, in submit
member = Member.objects.get(email=email_address)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/manager.py", line 120, in get
return self.get_query_set().get(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 300, in get
num = len(clone)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 81, in __len__
self._result_cache = list(self.iterator())
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/query.py", line 238, in iterator
for row in self.query.results_iter():
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 287, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
cursor.execute(sql, params)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/django/db/backends/mysql/base.py", line 84, in execute
return self.cursor.execute(query, args)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/cursors.py", line 173, in execute
self.errorhandler(self, exc, value)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.3-fat.egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'test_deal.member' doesn't exist")
----------------------------------------------------------------------
Ran 1 test in 0.447s
FAILED (errors=1)
Destroying test database...
我还能在哪里查看出现了什么问题,为什么这个表没有被创建?
更新 - 问题似乎是在运行单元测试时,用于生成测试数据库的模型来自应用程序内部,而不是来自项目内部。这似乎是奇怪的行为和违反DRY的原因,为了使其工作,我需要在每个应用程序中复制模型文件,而不是在项目中集中。
有人可以建议如何解决这个问题吗?
** update 2 ** - 项目结构如下:
项目结构如下:
/mysite (www.whatever.com)
/application1 (facebook app, handles all urls beginning with /fb)
/application2 (www app, handles everything else in the root dir of the site)
我希望将fb功能与网站的其他部分分开,但它们共享相同的数据库。我做错了吗?
答案 0 :(得分:9)
要解决此问题,请生成在项目文件夹的settings.py
文件中声明的所有表。
您可以在设置文件的INSTALLED APPS
块中找到。为此运行此命令:
manage.py syncdb
或python manage.py syncdb
如果这不起作用,则为python目录设置环境变量PATH。
答案 1 :(得分:3)
稍微不同的是,你绝对不希望这样:
created_on = models.DateTimeField(…, default=datetime.datetime.utcnow())
这实际上会在您加载utcnow()
文件时调用models.py
(这将在您manage.py runserver
时发生)并使用该单个时间点作为其后每个实例的默认值。你想要的是这个:
created_on = models.DateTimeField(…, default=datetime.datetime.utcnow)
default
参数可以接受可调用,每次新实例需要默认值时都会调用该调用。看看documentation。
答案 2 :(得分:1)
您可以通过在测试中添加 -v3 来获取有关正在发生的事情的更多信息,例如:
$ python manage.py test -v3 sitecoming