Skip to content

Commit

Permalink
自动化测试
Browse files Browse the repository at this point in the history
  • Loading branch information
stacklens committed Jul 6, 2019
1 parent 0e0a930 commit fac8949
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
33. [消息通知](https://www.dusaiphoto.com/article/detail/64/)
34. [锚点定位](https://www.dusaiphoto.com/article/detail/65/)
35. [第三方登录](https://www.dusaiphoto.com/article/detail/66/)
36. [自动化测试](https://www.dusaiphoto.com/article/detail/67/)

以及:
- [小功能集合](https://www.dusaiphoto.com/article/detail/53/)
Expand Down
Binary file modified article/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file modified article/__pycache__/tests.cpython-37.pyc
Binary file not shown.
12 changes: 11 additions & 1 deletion article/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ def save(self, *args, **kwargs):
resized_image = image.resize((new_x, new_y), Image.ANTIALIAS)
resized_image.save(self.avatar.path)

return article
return article

def was_created_recently(self):
# 若文章是 1 分钟内发表的,则返回 True
diff = timezone.now() - self.created

# if diff.days <= 0 and diff.seconds < 60:
if diff.days == 0 and diff.seconds >= 0 and diff.seconds < 60:
return True
else:
return False
100 changes: 99 additions & 1 deletion article/tests.py
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 modified comment/__pycache__/tests.cpython-37.pyc
Binary file not shown.
Binary file modified db.sqlite3
Binary file not shown.
Binary file added notice/__pycache__/tests.cpython-37.pyc
Binary file not shown.
Binary file modified userprofile/__pycache__/tests.cpython-37.pyc
Binary file not shown.

0 comments on commit fac8949

Please sign in to comment.