1
|
|
|
from datetime import timedelta |
2
|
|
|
|
3
|
|
|
from django.contrib.auth import get_user_model |
4
|
|
|
from django.test import TestCase |
5
|
|
|
from django.utils import timezone |
6
|
|
|
|
7
|
|
|
from simple_forums import models |
8
|
|
|
from simple_forums.tests.testing_utils import ( |
9
|
|
|
create_message, |
10
|
|
|
create_thread, |
11
|
|
|
create_topic) |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
class TestMessageModel(TestCase): |
15
|
|
|
""" Tests for the message model """ |
16
|
|
|
|
17
|
|
|
def test_create_with_all_fields(self): |
18
|
|
|
""" Test creation of a message with all its fields. |
19
|
|
|
|
20
|
|
|
A message instance should be able to be created with a foreign |
21
|
|
|
key to a user instance, body text, and a time for its creation. |
22
|
|
|
""" |
23
|
|
|
user = get_user_model().objects.create_user( |
24
|
|
|
username='test', |
25
|
|
|
password='test') |
26
|
|
|
thread = create_thread() |
27
|
|
|
body = "Test body text" |
28
|
|
|
time = timezone.now() |
29
|
|
|
|
30
|
|
|
message = models.Message.objects.create( |
31
|
|
|
user=user, |
32
|
|
|
thread=thread, |
33
|
|
|
body=body, |
34
|
|
|
time_created=time) |
35
|
|
|
|
36
|
|
|
self.assertEqual(user, message.user) |
37
|
|
|
self.assertEqual(thread, message.thread) |
38
|
|
|
self.assertEqual(body, message.body) |
39
|
|
|
self.assertEqual(time, message.time_created) |
40
|
|
|
|
41
|
|
|
def test_default_time_created(self): |
42
|
|
|
""" Test the default for the 'time_created' field. |
43
|
|
|
|
44
|
|
|
The field should default to the current time. |
45
|
|
|
""" |
46
|
|
|
start_time = timezone.now() |
47
|
|
|
message = create_message() |
48
|
|
|
end_time = timezone.now() |
49
|
|
|
|
50
|
|
|
self.assertTrue(start_time <= message.time_created <= end_time) |
51
|
|
|
|
52
|
|
|
def test_string_conversion(self): |
53
|
|
|
""" Test the conversion of a message instance to a string. |
54
|
|
|
|
55
|
|
|
Converting a message instance to a string should return the |
56
|
|
|
message's body text. |
57
|
|
|
""" |
58
|
|
|
message = models.Message(body="Test body text") |
59
|
|
|
|
60
|
|
|
self.assertEqual(message.body, str(message)) |
61
|
|
|
|
62
|
|
|
def test_update_last_activity_time(self): |
63
|
|
|
""" Test if saving a message updates its parent thread. |
64
|
|
|
|
65
|
|
|
Saving a message should update the 'time_last_activity' field |
66
|
|
|
on its parent thread instance. |
67
|
|
|
""" |
68
|
|
|
past = timezone.now() - timedelta(days=1) |
69
|
|
|
thread = create_thread(time_created=past) |
70
|
|
|
message = create_message(thread=thread) |
71
|
|
|
|
72
|
|
|
self.assertEqual(message.time_created, thread.time_last_activity) |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
class TestThreadModel(TestCase): |
76
|
|
|
""" Tests for the thread model """ |
77
|
|
|
|
78
|
|
|
def test_create_with_all_fields(self): |
79
|
|
|
""" Test creation of a thread with all its fields. |
80
|
|
|
|
81
|
|
|
A thread instance should be able to be created with title text. |
82
|
|
|
""" |
83
|
|
|
topic = create_topic() |
84
|
|
|
time = timezone.now() - timedelta(days=1) |
85
|
|
|
|
86
|
|
|
thread = models.Thread.objects.create( |
87
|
|
|
topic=topic, |
88
|
|
|
title='test', |
89
|
|
|
time_created=time) |
90
|
|
|
|
91
|
|
|
self.assertEqual(topic, thread.topic) |
92
|
|
|
self.assertEqual('test', thread.title) |
93
|
|
|
self.assertEqual(time, thread.time_created) |
94
|
|
|
|
95
|
|
|
def test_default_time_created(self): |
96
|
|
|
""" Test the default for the 'time_created' field. |
97
|
|
|
|
98
|
|
|
If no parameter is passed to 'time_created', it should default |
99
|
|
|
to the current time. |
100
|
|
|
""" |
101
|
|
|
start_time = timezone.now() |
102
|
|
|
thread = create_thread() |
103
|
|
|
end_time = timezone.now() |
104
|
|
|
|
105
|
|
|
self.assertTrue(start_time <= thread.time_created <= end_time) |
106
|
|
|
|
107
|
|
|
def test_num_replies_with_no_replies(self): |
108
|
|
|
""" Test retrieving the number of replies for a thread. |
109
|
|
|
|
110
|
|
|
If there are no messages associated with the thread, the number |
111
|
|
|
of replies should be 0. |
112
|
|
|
""" |
113
|
|
|
thread = create_thread() |
114
|
|
|
|
115
|
|
|
self.assertEqual(0, thread.num_replies) |
116
|
|
|
|
117
|
|
|
def test_num_replies_with_initial_reply(self): |
118
|
|
|
""" Test retrieving the number of replies for a thread. |
119
|
|
|
|
120
|
|
|
If the only message associated with a thread is the initial |
121
|
|
|
message, then the property should return 0 replies. |
122
|
|
|
""" |
123
|
|
|
thread = create_thread() |
124
|
|
|
create_message(thread=thread) |
125
|
|
|
|
126
|
|
|
self.assertEqual(0, thread.num_replies) |
127
|
|
|
|
128
|
|
|
def test_num_replies_with_more_replies(self): |
129
|
|
|
""" Test retrieving the number of replies for a thread. |
130
|
|
|
|
131
|
|
|
If the thread has a message that is not the initial message, |
132
|
|
|
then the property should return the number of additional |
133
|
|
|
messages. |
134
|
|
|
""" |
135
|
|
|
thread = create_thread() |
136
|
|
|
# simulate inital message |
137
|
|
|
create_message(thread=thread) |
138
|
|
|
|
139
|
|
|
# create additional message |
140
|
|
|
create_message(thread=thread) |
141
|
|
|
|
142
|
|
|
self.assertEqual(1, thread.num_replies) |
143
|
|
|
|
144
|
|
|
def test_slug_generation(self): |
145
|
|
|
""" Test the automatic generation of a url slug. |
146
|
|
|
|
147
|
|
|
When creating a thread instance, the instance should generate a |
148
|
|
|
url slug based on its title. |
149
|
|
|
""" |
150
|
|
|
thread = create_thread(title='test title') |
151
|
|
|
|
152
|
|
|
self.assertEqual('test-title', thread.slug) |
153
|
|
|
|
154
|
|
|
def test_slug_generation_for_long_title(self): |
155
|
|
|
""" Test generating a slug when the title is really long. |
156
|
|
|
|
157
|
|
|
If the title is longer than 50 characters, the slug should be |
158
|
|
|
truncated to 50 chars. |
159
|
|
|
""" |
160
|
|
|
thread = create_thread(title='a' * 51) |
161
|
|
|
|
162
|
|
|
self.assertEqual('a' * 50, thread.slug) |
163
|
|
|
|
164
|
|
|
def test_sticky_default(self): |
165
|
|
|
""" Test default 'sticky' value. |
166
|
|
|
|
167
|
|
|
Threads should not be sticky by default. |
168
|
|
|
""" |
169
|
|
|
thread = create_thread() |
170
|
|
|
|
171
|
|
|
self.assertFalse(thread.sticky) |
172
|
|
|
|
173
|
|
|
def test_string_conversion(self): |
174
|
|
|
""" Test converting a thread instance to a string. |
175
|
|
|
|
176
|
|
|
Converting a thread instance to a string should return the |
177
|
|
|
thread's title. |
178
|
|
|
""" |
179
|
|
|
thread = models.Thread(title='test') |
180
|
|
|
|
181
|
|
|
self.assertEqual(thread.title, str(thread)) |
182
|
|
|
|
183
|
|
|
def test_time_last_activity_no_replies(self): |
184
|
|
|
""" Test the 'time_last_activity' property with no replies. |
185
|
|
|
|
186
|
|
|
If there are no replies, this property should return the time |
187
|
|
|
that the thread was created. |
188
|
|
|
""" |
189
|
|
|
thread = create_thread() |
190
|
|
|
|
191
|
|
|
self.assertEqual(thread.time_created, thread.time_last_activity) |
192
|
|
|
|
193
|
|
|
def test_time_last_activity_with_reply(self): |
194
|
|
|
""" Test the 'time_last_activity' property with a reply. |
195
|
|
|
|
196
|
|
|
If there is a reply, this property should return the time that |
197
|
|
|
the most recent reply was posted. |
198
|
|
|
""" |
199
|
|
|
past = timezone.now() - timedelta(days=1) |
200
|
|
|
thread = create_thread(time_created=past) |
201
|
|
|
message = create_message(thread=thread) |
202
|
|
|
|
203
|
|
|
self.assertEqual(message.time_created, thread.time_last_activity) |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
class TestTopicModel(TestCase): |
207
|
|
|
""" Tests for the topic model """ |
208
|
|
|
|
209
|
|
|
def test_create_with_all_fields(self): |
210
|
|
|
""" Test creating a topic with all of its fields specified. """ |
211
|
|
|
title = 'thread title' |
212
|
|
|
description = 'thread description' |
213
|
|
|
|
214
|
|
|
topic = models.Topic.objects.create( |
215
|
|
|
title=title, |
216
|
|
|
description=description) |
217
|
|
|
|
218
|
|
|
self.assertEqual(title, topic.title) |
219
|
|
|
self.assertEqual(description, topic.description) |
220
|
|
|
|
221
|
|
|
def test_slug_generation(self): |
222
|
|
|
""" Test the automatic generation of a url slug. |
223
|
|
|
|
224
|
|
|
When creating a topic instance, the instance should generate a |
225
|
|
|
url slug based on its title. |
226
|
|
|
""" |
227
|
|
|
topic = create_topic(title='test title') |
228
|
|
|
|
229
|
|
|
self.assertEqual('test-title', topic.slug) |
230
|
|
|
|
231
|
|
|
def test_slug_generation_for_long_title(self): |
232
|
|
|
""" Test generating a slug when the title is really long. |
233
|
|
|
|
234
|
|
|
If the title is longer than 50 characters, the slug should be |
235
|
|
|
truncated to 50 chars. |
236
|
|
|
""" |
237
|
|
|
topic = create_topic(title='a' * 51) |
238
|
|
|
|
239
|
|
|
self.assertEqual('a' * 50, topic.slug) |
240
|
|
|
|
241
|
|
|
def test_string_conversion(self): |
242
|
|
|
""" Test converting a topic instance to a string. |
243
|
|
|
|
244
|
|
|
Converting a topic instance to a string should return the |
245
|
|
|
topic's title. |
246
|
|
|
""" |
247
|
|
|
topic = models.Topic(title='test') |
248
|
|
|
|
249
|
|
|
self.assertEqual(topic.title, str(topic.title)) |
250
|
|
|
|