2017-10-12 8 views
0

장고를 사용하여 장고에서 단위 테스트를 설정하려고합니다. ,django unit test with fixtures - 객체 일치 쿼리가 없습니다.

DoesNotExist: BlogIndexPage matching query does not exist.

여기 테스트 (필자는 할미새 CMS를 사용하고 내 코드입니다 :

나는 성공적으로 내 비품을로드 할 수 있습니다,하지만 난 그들로부터 데이터를 검색 할 때 나는 오류

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>' 
     }) 

을 그리고 이것은 내 고정물입니다 :하는) a few additional methods와 유닛 테스트 확장

[ 
{ 
    "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" 
    } 
} 
] 

기본적으로 나는 단지 블로그 색인 페이지를 잡고 그 밑에 블로그 페이지 (블로그 게시물)를 만들 수 있는지 테스트하려고합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

실제로 'BlogIndexPage'모델을 쿼리하고 있습니다. 그러나 픽스처에는'Page' 모델이 포함됩니다. 가장 가능성있는 것은 'from wagtailcore.models import page' 명령을 사용하여 해당 모델을 가져올 수 있다는 것입니다. – raratiru

답변

0

당신의 조명기에는 "model": "blog.blogindexpage""model": "wagtailcore.page"의 레코드가 포함되어야하며, 일치하는 값은 pk입니다. 이는 Wagtail이 multi-table inheritance을 사용하여 페이지를 나타 내기 때문입니다. 페이지의 데이터는 wagtailcore_page 테이블 (제목과 같은 모든 페이지 유형에 공통적 인 핵심 필드 포함)과 각 페이지 모델에 대한 blog_blogindexpage과 같은 다른 테이블로 나누어집니다. 해당 특정 모델에 대해 정의 된 추가 필드. 두 테이블에 레코드가 없으면 BlogIndexPage에서 조회하면 결과가 반환되지 않으므로 위의 DoesNotExist 오류가 발생합니다.

./manage.py dumpdata --indent 4을 실행하면 조명기에서 사용하는 JSON 형식으로 개발 데이터베이스를 덤프 할 수 있습니다. 테스트의 필요에 따라 직접 사용하거나 (./manage.py dumpdata --indent 4 > blog/fixtures/demosite.json) 수동으로 자신의 조명기를 작성하기위한 안내서로 사용할 수 있습니다.