Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.test import TestCase, Client
from django.core.paginator import EmptyPage
from bs4 import BeautifulSoup
from .models import News, NewsSEO, NewsExpired, NewsComplete, NewsExtended
class NewsboxAbstractModelsTests(TestCase):
fixtures = ['tests_data.json',]
def test_01_manager_published(self):
"""
Test if we only get really published news via the manager
"""
newsClasses = [
News, NewsSEO, NewsExpired, NewsComplete, NewsExtended,
]
for cls in newsClasses:
returned_ids = list(cls.objects.published().values_list(
'id', flat=True).order_by('id'))
self.assertEqual(
(cls.__name__,returned_ids),
(cls.__name__,[1, 4, 6, 7,]))
def test_02_view_archive_200(self):
"""
Test if generic archive view return right informations.
"""
c = Client()
r = c.get('/news/')
self.assertEqual(r.status_code, 200)
self.assertIn('newsset', r.context)
self.assertEqual(r.context['newsset'][0].pk, 7)
self.assertEqual(r.context['newsset'].number, 1)
self.assertEqual(r.context['newsset'].paginator.num_pages, 4)
self.assertFalse(r.context['newsset'].has_previous())
self.assertTrue(r.context['newsset'].has_next())
self.assertRaises(EmptyPage, r.context['newsset'].previous_page_number)
self.assertEqual(r.context['newsset'].next_page_number(), 2)
def test_03_view_year_archive_200(self):
"""
Test if generic year views return right informations.
"""
c = Client()
r = c.get('/news/2005/')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.context['newsset'][0].pk, 6)
self.assertFalse(r.context['with_pager'])
r = c.get('/news/2014/')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.context['newsset'].paginator.num_pages, 2)
r = c.get('/news/2020/')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.context['newsset'].paginator.num_pages, 1)
def test_04_view_year_archive_404(self):
"""
Test if generic year views return a 404 with an URL with a year without
any news.
"""
c = Client()
r = c.get('/newsbox/myapp/news/1985/')
self.assertEqual(r.status_code, 404)
def test_05_view_month_archive_200(self):
"""
Test if generic month views return right informations.
"""
c = Client()
r = c.get('/newsbox/myapp/news/2005/07/')
self.assertEqual(r.status_code, 200)
ref = c.get('/newsbox/myapp/news/2005/')
self.assertEqual(r.content, ref.content)
def test_06_view_month_archive_404(self):
"""
Test if generic month views return a 404 with an URL with a year + month
without any news.
"""
c = Client()
r = c.get('/newsbox/myapp/news/1985/07/')
self.assertEqual(r.status_code, 404)
def test_07_view_day_archive_200(self):
"""
Test if generic day views return right informations.
"""
c = Client()
r = c.get('/newsbox/myapp/news/2005/07/15/')
self.assertEqual(r.status_code, 200)
ref = c.get('/newsbox/myapp/news/2005/07/')
self.assertEqual(r.content, ref.content)
def test_08_view_month_archive_404(self):
"""
Test if generic day views return a 404 with an URL with a date without
any news.
"""
c = Client()
r = c.get('/newsbox/myapp/news/1985/07/02/')
self.assertEqual(r.status_code, 404)
def test_09_view_detail_200(self):
"""
Test if generic day views return right informations.
"""
c = Client()
r = c.get('/newsbox/myapp/news/2005/07/15/news-6/')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.context['news'].pk, 6)
def test_10_view_detail_404(self):
"""
Test if generic day views return a 404 with an URL with a date without
any news.
"""
c = Client()
r = c.get('/newsbox/myapp/news/1985/07/02/my-birthday/')
self.assertEqual(r.status_code, 404)
def test_11_view_detail_404_not_published(self):
"""
Test if generic day views return a 404 with an URL with a not published
news
"""
c = Client()
r = c.get('/newsbox/myapp/news/2014/02/22/news-2/')
self.assertEqual(r.status_code, 404)
def has_readmore_link(self, news):
readmore_p = news.find('p', class_='readmore')
if readmore_p:
readmore_a = readmore_p.find('a')
if readmore_a:
return True
return False
def get_news_in_list_by_summary(self, response, summary):
soup = BeautifulSoup(response.content)
for news in soup.find_all('div', class_='news'):
readmore_summary = news.find('p', text=summary)
if readmore_summary:
return news
return None
def test_news_4_has_no_readmore_link(self):
"""
Test if readmore link is not present in news 4
"""
c = Client()
r = c.get('/news/?page=2')
news = self.get_news_in_list_by_summary(r, 'Summary of the news 4')
self.assertIsNotNone(news)
found = self.has_readmore_link(news)
self.assertFalse(found)
def test_news_6_has_readmore_link(self):
"""
Test if readmore link is present in news 6
"""
c = Client()
r = c.get('/news/2005/')
news = self.get_news_in_list_by_summary(r, 'Summary of the news 6')
self.assertIsNotNone(news)
found = self.has_readmore_link(news)
self.assertTrue(found)