[英]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.