|
| 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 | + ) |
0 commit comments