Blog example app tests

This commit is contained in:
Danilo Bargen 2013-07-07 14:26:25 +02:00
parent 2a8bab0170
commit 6e42b9459d
2 changed files with 43 additions and 20 deletions

View file

@ -30,6 +30,9 @@ class PostAdmin(djadmin2.ModelAdmin2):
inlines = [CommentInline]
search_fields = ('title', '^body')
list_display = ('title', 'body', 'published')
field_renderers = {
'title': renderers.title_renderer,
}
class CommentAdmin(djadmin2.ModelAdmin2):

View file

@ -52,7 +52,7 @@ class CommentListTest(BaseIntegrationTest):
class PostListTest(BaseIntegrationTest):
def test_view_ok(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
response = self.client.get(reverse("admin2:blog_post_index"))
self.assertContains(response, post.title)
@ -67,33 +67,53 @@ class PostListTest(BaseIntegrationTest):
self.assertInHTML('<a tabindex="-1" href="#" data-name="action" data-value="DeleteSelectedAction">Delete selected items</a>', response.content)
def test_delete_selected_post(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
params = {'action': 'DeleteSelectedAction', 'selected_model_pk': str(post.pk)}
response = self.client.post(reverse("admin2:blog_post_index"), params)
# caution : uses pluralization
self.assertInHTML('<p>Are you sure you want to delete the selected post? The following item will be deleted:</p>', response.content)
def test_delete_selected_post_confirmation(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
params = {'action': 'DeleteSelectedAction', 'selected_model_pk': str(post.pk), 'confirmed': 'yes'}
response = self.client.post(reverse("admin2:blog_post_index"), params)
self.assertRedirects(response, reverse("admin2:blog_post_index"))
def test_delete_selected_post_none_selected(self):
Post.objects.create(title="a_post_title", body="body")
Post.objects.create(title="A Post Title", body="body")
params = {'action': 'DeleteSelectedAction'}
response = self.client.post(reverse("admin2:blog_post_index"), params, follow=True)
self.assertContains(response, "Items must be selected in order to perform actions on them. No items have been changed.")
def test_search_posts(self):
Post.objects.create(title="a_post_title", body="body")
Post.objects.create(title="another_post_title", body="body")
Post.objects.create(title="post_with_keyword_in_body", body="another post body")
Post.objects.create(title="A Post Title", body="body")
Post.objects.create(title="Another Post Title", body="body")
Post.objects.create(title="Post With Keyword In Body", body="another post body")
params = {"q":"another"}
response = self.client.get(reverse("admin2:blog_post_index"), params)
self.assertContains(response, "another_post_title")
self.assertContains(response, "post_with_keyword_in_body")
self.assertNotContains(response, "a_post_title")
self.assertContains(response, "Another Post Title")
self.assertContains(response, "Post With Keyword In Body")
self.assertNotContains(response, "A Post Title")
def test_renderer_title(self):
Post.objects.create(title='a lowercase title', body='body', published=False)
response = self.client.get(reverse('admin2:blog_post_index'))
self.assertContains(response, 'A Lowercase Title')
def test_renderer_body(self):
Post.objects.create(title='title', body='a lowercase body', published=False)
response = self.client.get(reverse('admin2:blog_post_index'))
self.assertContains(response, 'a lowercase body')
def test_renderer_unpublished(self):
Post.objects.create(title='title', body='body', published=False)
response = self.client.get(reverse('admin2:blog_post_index'))
self.assertContains(response, 'icon-minus-sign')
def test_renderer_published(self):
Post.objects.create(title='title', body='body', published=True)
response = self.client.get(reverse('admin2:blog_post_index'))
self.assertContains(response, 'icon-ok-sign')
class PostListTestCustomAction(BaseIntegrationTest):
@ -103,7 +123,7 @@ class PostListTestCustomAction(BaseIntegrationTest):
self.assertInHTML('<a tabindex="-1" href="#" data-name="action" data-value="CustomPublishAction">Publish selected items</a>', response.content)
def test_publish_selected_items(self):
post = Post.objects.create(title="a_post_title",
post = Post.objects.create(title="A Post Title",
body="body",
published=False)
self.assertEqual(Post.objects.filter(published=True).count(), 0)
@ -121,7 +141,7 @@ class PostListTestCustomAction(BaseIntegrationTest):
self.assertInHTML('<a tabindex="-1" href="#" data-name="action" data-value="unpublish_items">Unpublish selected items</a>', response.content)
def test_unpublish_selected_items(self):
post = Post.objects.create(title="a_post_title",
post = Post.objects.create(title="A Post Title",
body="body",
published=True)
self.assertEqual(Post.objects.filter(published=True).count(), 1)
@ -136,7 +156,7 @@ class PostListTestCustomAction(BaseIntegrationTest):
class PostDetailViewTest(BaseIntegrationTest):
def test_view_ok(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
response = self.client.get(reverse("admin2:blog_post_detail",
args=(post.pk, )))
self.assertContains(response, post.title)
@ -153,15 +173,15 @@ class PostCreateViewTest(BaseIntegrationTest):
"comment_set-INITIAL_FORMS": u'0',
"comment_set-MAX_NUM_FORMS": u'',
"comment_set-0-body": u'Comment Body',
"title": "a_post_title",
"title": "A Post Title",
"body": "a_post_body",
}
response = self.client.post(reverse("admin2:blog_post_create"),
post_data,
follow=True)
self.assertTrue(Post.objects.filter(title="a_post_title").exists())
Post.objects.get(title="a_post_title")
self.assertTrue(Post.objects.filter(title="A Post Title").exists())
Post.objects.get(title="A Post Title")
Comment.objects.get(body="Comment Body")
self.assertRedirects(response, reverse("admin2:blog_post_index"))
@ -174,14 +194,14 @@ class PostCreateViewTest(BaseIntegrationTest):
"comment_set-TOTAL_FORMS": u'2',
"comment_set-INITIAL_FORMS": u'0',
"comment_set-MAX_NUM_FORMS": u'',
"title": "a_post_title",
"title": "A Post Title",
"body": "a_post_body",
"_addanother": ""
}
self.client.login(username='admin', password='password')
response = self.client.post(reverse("admin2:blog_post_create"),
post_data)
Post.objects.get(title='a_post_title')
Post.objects.get(title='A Post Title')
self.assertRedirects(response, reverse("admin2:blog_post_create"))
def test_save_and_continue_editing_redirects_to_update(self):
@ -206,13 +226,13 @@ class PostCreateViewTest(BaseIntegrationTest):
class PostDeleteViewTest(BaseIntegrationTest):
def test_view_ok(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
response = self.client.get(reverse("admin2:blog_post_delete",
args=(post.pk, )))
self.assertContains(response, post.title)
def test_delete_post(self):
post = Post.objects.create(title="a_post_title", body="body")
post = Post.objects.create(title="A Post Title", body="body")
response = self.client.post(reverse("admin2:blog_post_delete",
args=(post.pk, )))
self.assertRedirects(response, reverse("admin2:blog_post_index"))