forked from stacklens/django_blog_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
111 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,101 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. | ||
import datetime | ||
from django.utils import timezone | ||
from article.models import ArticlePost | ||
from django.contrib.auth.models import User | ||
|
||
from time import sleep | ||
from django.urls import reverse | ||
|
||
|
||
class ArticlePostModelTests(TestCase): | ||
|
||
def test_was_created_recently_with_future_article(self): | ||
# 若文章创建时间为未来,返回 False | ||
author = User(username='user', password='test_password') | ||
author.save() | ||
|
||
future_article = ArticlePost( | ||
author=author, | ||
title='test', | ||
body='test', | ||
created=timezone.now() + datetime.timedelta(days=30) | ||
) | ||
self.assertIs(future_article.was_created_recently(), False) | ||
|
||
def test_was_created_recently_with_seconds_before_article(self): | ||
# 若文章创建时间为 1 分钟内,返回 True | ||
author = User(username='user1', password='test_password') | ||
author.save() | ||
seconds_before_article = ArticlePost( | ||
author=author, | ||
title='test1', | ||
body='test1', | ||
created=timezone.now() - datetime.timedelta(seconds=45) | ||
) | ||
self.assertIs(seconds_before_article.was_created_recently(), True) | ||
|
||
def test_was_created_recently_with_hours_before_article(self): | ||
# 若文章创建时间为几小时前,返回 False | ||
author = User(username='user2', password='test_password') | ||
author.save() | ||
hours_before_article = ArticlePost( | ||
author=author, | ||
title='test2', | ||
body='test2', | ||
created=timezone.now() - datetime.timedelta(hours=3) | ||
) | ||
self.assertIs(hours_before_article.was_created_recently(), False) | ||
|
||
def test_was_created_recently_with_days_before_article(self): | ||
# 若文章创建时间为几天前,返回 False | ||
author = User(username='user3', password='test_password') | ||
author.save() | ||
months_before_article = ArticlePost( | ||
author=author, | ||
title='test3', | ||
body='test3', | ||
created=timezone.now() - datetime.timedelta(days=5) | ||
) | ||
self.assertIs(months_before_article.was_created_recently(), False) | ||
|
||
|
||
class ArtitclePostViewTests(TestCase): | ||
|
||
def test_increase_views(self): | ||
# 请求详情视图时,阅读量 +1 | ||
author = User(username='user4', password='test_password') | ||
author.save() | ||
article = ArticlePost( | ||
author=author, | ||
title='test4', | ||
body='test4', | ||
) | ||
article.save() | ||
self.assertIs(article.total_views, 0) | ||
|
||
url = reverse('article:article_detail', args=(article.id,)) | ||
response = self.client.get(url) | ||
|
||
viewed_article = ArticlePost.objects.get(id=article.id) | ||
self.assertIs(viewed_article.total_views, 1) | ||
|
||
def test_increase_views_but_not_change_updated_field(self): | ||
# 请求详情视图时,不改变 updated 字段 | ||
author = User(username='user5', password='test_password') | ||
author.save() | ||
article = ArticlePost( | ||
author=author, | ||
title='test5', | ||
body='test5', | ||
) | ||
article.save() | ||
|
||
sleep(0.5) | ||
|
||
url = reverse('article:article_detail', args=(article.id,)) | ||
response = self.client.get(url) | ||
|
||
viewed_article = ArticlePost.objects.get(id=article.id) | ||
self.assertIs(viewed_article.updated - viewed_article.created < timezone.timedelta(seconds=0.1), True) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.