python - django 使用装置进行单元测试 - 对象匹配查询不存在

标签 python django unit-testing python-unittest wagtail

我正在尝试使用固定装置在 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/

相关文章:

django - 管理.py : Unknown command: 'migrate'

python - 我们可以用扭曲的 python 创建应用程序作为 django ......?

java - 如何在mockito中模拟Class.forName()

c# - 模拟存储库返回 null

python - 生成和发送 MS Outlook 任务

python - 无法在 AWS elastic-beanstalk 上提供 Django 静态文件

python - x86_64 上的 gevent 安装失败 : "undefined symbol: evhttp_accept_socket"

使用集合集合的 C# 单元测试

python - 用双引号括起来时忽略逗号

python - 带有 Django 的 matplotlib 在 print_figure 上失败,抛出 [Errno 2] 没有这样的文件或目录