我正在尝试使用固定装置在 django 中设置单元测试。
我可以成功加载我的灯具,但是当我尝试从中检索数据时,出现错误:
DoesNotExist:BlogIndexPage 匹配查询不存在。
这是我的测试代码(我使用的是 Wagtail CMS,它使用 a few additional methods 扩展了单元测试):
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 使用 multi-table inheritance表示页面:页面的数据分布在 wagtailcore_page
表(其中包含所有页面类型共有的核心字段,例如标题)和另一个表(例如 blog_blogindexpage
) > 对于每个页面模型,包含为该特定模型定义的附加字段。如果两个表中都没有记录,则对 BlogIndexPage
进行查找将不会返回任何结果,从而导致上面的 DoesNotExist
错误。
您可以运行./manage.py dumpdata --indent 4
以由fixtures使用的JSON格式获取开发数据库的转储;根据测试的需要,您可以直接使用它(./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json
)或将其用作编写您的测试的指南手动拥有自己的夹具。
关于python - django 使用装置进行单元测试 - 对象匹配查询不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46720446/