[英]django unit testing with fixtures - object matching query does not exist
我正在尝试使用夹具在 django 中设置单元测试。
我可以成功加载我的装置,但是当我尝试从中检索数据时,出现错误:
DoesNotExist: BlogIndexPage matching query does not exist.
这是我的测试代码(我使用的是 Wagtail CMS,它使用一些其他方法扩展了 unittest):
class BlogTests(WagtailPageTests):
fixtures = ['demosite.json']
def test_can_create_blog_entry(self):
blog_index_page = BlogIndexPage.objects.get(pk=5)
self.assertCanCreate(blog_index_page, BlogPage, {
'title': 'Post 2',
'date': '2017-10-11',
'intro': 'Post 2',
'body': '<p>Test Post</p>'
})
这是我的固定装置:
[
{
"pk": 1,
"model": "wagtailcore.page",
"fields": {
"title": "Root",
"draft_title": "Root",
"numchild": 1,
"show_in_menus": false,
"live": true,
"seo_title": "",
"depth": 1,
"search_description": "",
"content_type": [
"wagtailcore",
"page"
],
"has_unpublished_changes": false,
"owner": null,
"path": "0001",
"url_path": "/",
"slug": "root"
}
},
{
"pk": 2,
"model": "wagtailcore.page",
"fields": {
"title": "Home page",
"draft_title": "Home page",
"numchild": 5,
"show_in_menus": true,
"live": true,
"seo_title": "",
"depth": 2,
"search_description": "",
"content_type": [
"home",
"homepage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "00010002",
"url_path": "/home-page/",
"slug": "home-page"
}
},
{
"pk": 5,
"model": "wagtailcore.page",
"fields": {
"title": "Blog index",
"draft_title": "Blog index",
"numchild": 3,
"show_in_menus": true,
"live": true,
"seo_title": "",
"depth": 3,
"search_description": "",
"content_type": [
"blog",
"blogindexpage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "000100020002",
"url_path": "/blog/",
"slug": "blog"
}
},
{
"pk": 16,
"model": "wagtailcore.page",
"fields": {
"title": "Blog post",
"draft_title": "Blog post",
"numchild": 0,
"show_in_menus": false,
"live": true,
"seo_title": "",
"depth": 4,
"search_description": "The origin of the genus appears to be in the general area of Eastern Siberia/Mongolia. Wagtails spread rapidly across Eurasia and dispersed to Africa in the Zanclean (Early Pliocene) where the sub-Saharan lineage was later isolated. The African Pied Wagtail (and possibly the Mekong Wagtail) diverged prior to the massive radiation of the white-bellied black-throated and most yellow-bellied forms, all of which took place during the late Piacenzian (early Late Pliocene), c. 3 mya.",
"content_type": [
"blog",
"blogpage"
],
"has_unpublished_changes": false,
"owner": null,
"path": "0001000200020001",
"url_path": "/home-page/blog-index/blog-post/",
"slug": "blog-post"
}
}
]
所以基本上我只想抓取那个博客索引页面,并测试我是否可以在它下面创建一个博客页面(博客文章)。 我究竟做错了什么?
您的装置需要包含"model": "blog.blogindexpage"
以及"model": "wagtailcore.page"
,并具有匹配的pk
值。 这是因为 Wagtail 使用多表继承来表示页面:一个页面的数据被拆wagtailcore_page
表(包含所有页面类型共有的核心字段,例如标题)和另一个表(例如blog_blogindexpage
用于每个页面模型,包含为该特定模型定义的附加字段。 如果两个表中都没有记录,则在BlogIndexPage
查找将不会返回任何结果,从而导致上面的DoesNotExist
错误。
您可以运行./manage.py dumpdata --indent 4
以获取夹具使用的 JSON 格式的开发数据库的转储; 根据您的测试需要,您可以直接使用它( ./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json
)或将其用作手动编写自己的夹具的指南。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.