Skip to content

Commit 5434c22

Browse files
author
zmrenwu
committed
Step29: unittest for blog app
1 parent e69a1c5 commit 5434c22

10 files changed

+626
-53
lines changed

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ elasticsearch = ">=2,<3"
1616
django-haystack = "*"
1717

1818
[requires]
19-
python_version = "3.6"
19+
python_version = "3"

Pipfile.lock

+44-49
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ tutorial 分支为项目的主分支,每一篇教程的代码都和历史提
149149
26. [开启 Django 博客的 RSS 功能](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/84/)
150150
27. [Django 博客实现简单的全文搜索](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/85/)
151151
28. [Django Haystack 全文检索与关键词高亮](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/86/)
152+
29. [单元测试:测试 blog 应用](https://www.zmrenwu.com/courses/hellodjango-blog-tutorial/materials/87/)
152153

153154
## 公众号
154155
<p align="center">

blog/tests.py

-3
This file was deleted.

blog/tests/__init__.py

Whitespace-only changes.

blog/tests/test_models.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from django.apps import apps
2+
from django.contrib.auth.models import User
3+
from django.test import TestCase
4+
from django.urls import reverse
5+
6+
from ..models import Category, Post, Tag
7+
from ..search_indexes import PostIndex
8+
9+
10+
class CategoryModelTestCase(TestCase):
11+
def setUp(self):
12+
self.cate = Category.objects.create(name="测试")
13+
14+
def test_str_representation(self):
15+
self.assertEqual(self.cate.__str__(), self.cate.name)
16+
17+
18+
class TagModelTestCase(TestCase):
19+
def setUp(self):
20+
self.tag = Tag.objects.create(name="测试")
21+
22+
def test_str_representation(self):
23+
self.assertEqual(self.tag.__str__(), self.tag.name)
24+
25+
26+
class PostModelTestCase(TestCase):
27+
def setUp(self):
28+
# 断开 haystack 的 signal,测试生成的文章无需生成索引
29+
apps.get_app_config("haystack").signal_processor.teardown()
30+
user = User.objects.create_superuser(
31+
username="admin", email="[email protected]", password="admin"
32+
)
33+
cate = Category.objects.create(name="测试")
34+
self.post = Post.objects.create(
35+
title="测试标题", body="测试内容", category=cate, author=user,
36+
)
37+
38+
def test_str_representation(self):
39+
self.assertEqual(self.post.__str__(), self.post.title)
40+
41+
def test_auto_populate_modified_time(self):
42+
self.assertIsNotNone(self.post.modified_time)
43+
44+
old_post_modified_time = self.post.modified_time
45+
self.post.body = "新的测试内容"
46+
self.post.save()
47+
self.post.refresh_from_db()
48+
self.assertTrue(self.post.modified_time > old_post_modified_time)
49+
50+
def test_auto_populate_excerpt(self):
51+
self.assertIsNotNone(self.post.excerpt)
52+
self.assertTrue(0 < len(self.post.excerpt) <= 54)
53+
54+
def test_get_absolute_url(self):
55+
expected_url = reverse("blog:detail", kwargs={"pk": self.post.pk})
56+
self.assertEqual(self.post.get_absolute_url(), expected_url)
57+
58+
def test_increase_views(self):
59+
self.post.increase_views()
60+
self.post.refresh_from_db()
61+
self.assertEqual(self.post.views, 1)
62+
63+
self.post.increase_views()
64+
self.post.refresh_from_db()
65+
self.assertEqual(self.post.views, 2)
66+
67+
68+
class SearchIndexesTestCase(TestCase):
69+
def setUp(self):
70+
apps.get_app_config("haystack").signal_processor.teardown()
71+
user = User.objects.create_superuser(
72+
username="admin", email="[email protected]", password="admin"
73+
)
74+
cate = Category.objects.create(name="测试")
75+
Post.objects.create(
76+
title="测试标题", body="测试内容", category=cate, author=user,
77+
)
78+
another_cate = Category.objects.create(name="另一个测试")
79+
Post.objects.create(
80+
title="另一个测试标题", body="另一个测试内容", category=another_cate, author=user,
81+
)
82+
self.index_instance = PostIndex()
83+
84+
def test_get_model(self):
85+
self.assertTrue(issubclass(self.index_instance.get_model(), Post))
86+
87+
def test_index_queryset(self):
88+
expected_qs = Post.objects.all()
89+
self.assertQuerysetEqual(
90+
self.index_instance.index_queryset(), [repr(p) for p in expected_qs]
91+
)

blog/tests/test_smoke.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.test import TestCase
2+
3+
4+
class SmokeTestCase(TestCase):
5+
def test_smoke(self):
6+
self.assertEqual(1 + 1, 2)

0 commit comments

Comments
 (0)