Show More
@@ -1,200 +1,191 b'' | |||||
1 | from django.core.paginator import Paginator |
|
1 | from django.core.paginator import Paginator | |
2 | from django.test import TestCase |
|
2 | from django.test import TestCase | |
3 |
|
3 | |||
4 | from boards import settings |
|
4 | from boards import settings | |
5 | from boards.models import Tag, Post, Thread, KeyPair |
|
5 | from boards.models import Tag, Post, Thread, KeyPair | |
6 | from boards.models.thread import STATUS_ARCHIVE |
|
6 | from boards.models.thread import STATUS_ARCHIVE | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | class PostTests(TestCase): |
|
9 | class PostTests(TestCase): | |
10 |
|
10 | |||
11 | def _create_post(self): |
|
11 | def _create_post(self): | |
12 | tag, created = Tag.objects.get_or_create(name='test_tag') |
|
12 | tag, created = Tag.objects.get_or_create(name='test_tag') | |
13 | return Post.objects.create_post(title='title', text='text', |
|
13 | return Post.objects.create_post(title='title', text='text', | |
14 | tags=[tag]) |
|
14 | tags=[tag]) | |
15 |
|
15 | |||
16 | def test_post_add(self): |
|
16 | def test_post_add(self): | |
17 | """Test adding post""" |
|
17 | """Test adding post""" | |
18 |
|
18 | |||
19 | post = self._create_post() |
|
19 | post = self._create_post() | |
20 |
|
20 | |||
21 | self.assertIsNotNone(post, 'No post was created.') |
|
21 | self.assertIsNotNone(post, 'No post was created.') | |
22 | self.assertEqual('test_tag', post.get_thread().tags.all()[0].name, |
|
22 | self.assertEqual('test_tag', post.get_thread().tags.all()[0].name, | |
23 | 'No tags were added to the post.') |
|
23 | 'No tags were added to the post.') | |
24 |
|
24 | |||
25 | def test_delete_post(self): |
|
25 | def test_delete_post(self): | |
26 | """Test post deletion""" |
|
26 | """Test post deletion""" | |
27 |
|
27 | |||
28 | post = self._create_post() |
|
28 | post = self._create_post() | |
29 | post_id = post.id |
|
29 | post_id = post.id | |
30 |
|
30 | |||
31 | post.delete() |
|
31 | post.delete() | |
32 |
|
32 | |||
33 | self.assertFalse(Post.objects.filter(id=post_id).exists()) |
|
33 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | |
34 |
|
34 | |||
35 | def test_delete_thread(self): |
|
35 | def test_delete_thread(self): | |
36 | """Test thread deletion""" |
|
36 | """Test thread deletion""" | |
37 |
|
37 | |||
38 | opening_post = self._create_post() |
|
38 | opening_post = self._create_post() | |
39 | thread = opening_post.get_thread() |
|
39 | thread = opening_post.get_thread() | |
40 | reply = Post.objects.create_post("", "", thread=thread) |
|
40 | reply = Post.objects.create_post("", "", thread=thread) | |
41 |
|
41 | |||
42 | thread.delete() |
|
42 | thread.delete() | |
43 |
|
43 | |||
44 | self.assertFalse(Post.objects.filter(id=reply.id).exists(), |
|
44 | self.assertFalse(Post.objects.filter(id=reply.id).exists(), | |
45 | 'Reply was not deleted with the thread.') |
|
45 | 'Reply was not deleted with the thread.') | |
46 | self.assertFalse(Post.objects.filter(id=opening_post.id).exists(), |
|
46 | self.assertFalse(Post.objects.filter(id=opening_post.id).exists(), | |
47 | 'Opening post was not deleted with the thread.') |
|
47 | 'Opening post was not deleted with the thread.') | |
48 |
|
48 | |||
49 | def test_post_to_thread(self): |
|
49 | def test_post_to_thread(self): | |
50 | """Test adding post to a thread""" |
|
50 | """Test adding post to a thread""" | |
51 |
|
51 | |||
52 | op = self._create_post() |
|
52 | op = self._create_post() | |
53 | post = Post.objects.create_post("", "", thread=op.get_thread()) |
|
53 | post = Post.objects.create_post("", "", thread=op.get_thread()) | |
54 |
|
54 | |||
55 | self.assertIsNotNone(post, 'Reply to thread wasn\'t created') |
|
55 | self.assertIsNotNone(post, 'Reply to thread wasn\'t created') | |
56 | self.assertEqual(op.get_thread().last_edit_time, post.pub_time, |
|
56 | self.assertEqual(op.get_thread().last_edit_time, post.pub_time, | |
57 | 'Post\'s create time doesn\'t match thread last edit' |
|
57 | 'Post\'s create time doesn\'t match thread last edit' | |
58 | ' time') |
|
58 | ' time') | |
59 |
|
59 | |||
60 | def test_delete_posts_by_ip(self): |
|
60 | def test_delete_posts_by_ip(self): | |
61 | """Test deleting posts with the given ip""" |
|
61 | """Test deleting posts with the given ip""" | |
62 |
|
62 | |||
63 | post = self._create_post() |
|
63 | post = self._create_post() | |
64 | post_id = post.id |
|
64 | post_id = post.id | |
65 |
|
65 | |||
66 | Post.objects.delete_posts_by_ip('0.0.0.0') |
|
66 | Post.objects.delete_posts_by_ip('0.0.0.0') | |
67 |
|
67 | |||
68 | self.assertFalse(Post.objects.filter(id=post_id).exists()) |
|
68 | self.assertFalse(Post.objects.filter(id=post_id).exists()) | |
69 |
|
69 | |||
70 | def test_get_thread(self): |
|
70 | def test_get_thread(self): | |
71 | """Test getting all posts of a thread""" |
|
71 | """Test getting all posts of a thread""" | |
72 |
|
72 | |||
73 | opening_post = self._create_post() |
|
73 | opening_post = self._create_post() | |
74 |
|
74 | |||
75 | for i in range(2): |
|
75 | for i in range(2): | |
76 | Post.objects.create_post('title', 'text', |
|
76 | Post.objects.create_post('title', 'text', | |
77 | thread=opening_post.get_thread()) |
|
77 | thread=opening_post.get_thread()) | |
78 |
|
78 | |||
79 | thread = opening_post.get_thread() |
|
79 | thread = opening_post.get_thread() | |
80 |
|
80 | |||
81 | self.assertEqual(3, thread.get_replies().count()) |
|
81 | self.assertEqual(3, thread.get_replies().count()) | |
82 |
|
82 | |||
83 | def test_create_post_with_tag(self): |
|
83 | def test_create_post_with_tag(self): | |
84 | """Test adding tag to post""" |
|
84 | """Test adding tag to post""" | |
85 |
|
85 | |||
86 | tag = Tag.objects.create(name='test_tag') |
|
86 | tag = Tag.objects.create(name='test_tag') | |
87 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) |
|
87 | post = Post.objects.create_post(title='title', text='text', tags=[tag]) | |
88 |
|
88 | |||
89 | thread = post.get_thread() |
|
89 | thread = post.get_thread() | |
90 | self.assertIsNotNone(post, 'Post not created') |
|
90 | self.assertIsNotNone(post, 'Post not created') | |
91 | self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread') |
|
91 | self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread') | |
92 |
|
92 | |||
93 | def test_thread_max_count(self): |
|
|||
94 | """Test deletion of old posts when the max thread count is reached""" |
|
|||
95 |
|
||||
96 | for i in range(settings.get_int('Messages', 'MaxThreadCount') + 1): |
|
|||
97 | self._create_post() |
|
|||
98 |
|
||||
99 | self.assertEqual(settings.get_int('Messages', 'MaxThreadCount'), |
|
|||
100 | len(Thread.objects.exclude(status=STATUS_ARCHIVE))) |
|
|||
101 |
|
||||
102 | def test_pages(self): |
|
93 | def test_pages(self): | |
103 | """Test that the thread list is properly split into pages""" |
|
94 | """Test that the thread list is properly split into pages""" | |
104 |
|
95 | |||
105 |
for i in range(settings.get_int(' |
|
96 | for i in range(settings.get_int('View', 'ThreadsPerPage') * 2): | |
106 | self._create_post() |
|
97 | self._create_post() | |
107 |
|
98 | |||
108 | all_threads = Thread.objects.exclude(status=STATUS_ARCHIVE) |
|
99 | all_threads = Thread.objects.exclude(status=STATUS_ARCHIVE) | |
109 |
|
100 | |||
110 | paginator = Paginator(Thread.objects.exclude(status=STATUS_ARCHIVE), |
|
101 | paginator = Paginator(Thread.objects.exclude(status=STATUS_ARCHIVE), | |
111 | settings.get_int('View', 'ThreadsPerPage')) |
|
102 | settings.get_int('View', 'ThreadsPerPage')) | |
112 | posts_in_second_page = paginator.page(2).object_list |
|
103 | posts_in_second_page = paginator.page(2).object_list | |
113 | first_post = posts_in_second_page[0] |
|
104 | first_post = posts_in_second_page[0] | |
114 |
|
105 | |||
115 | self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id, |
|
106 | self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id, | |
116 | first_post.id) |
|
107 | first_post.id) | |
117 |
|
108 | |||
118 | def test_reflinks(self): |
|
109 | def test_reflinks(self): | |
119 | """ |
|
110 | """ | |
120 | Tests that reflinks are parsed within post and connecting replies |
|
111 | Tests that reflinks are parsed within post and connecting replies | |
121 | to the replied posts. |
|
112 | to the replied posts. | |
122 |
|
113 | |||
123 | Local reflink example: [post]123[/post] |
|
114 | Local reflink example: [post]123[/post] | |
124 | Global reflink example: [post]key_type::key::123[/post] |
|
115 | Global reflink example: [post]key_type::key::123[/post] | |
125 | """ |
|
116 | """ | |
126 |
|
117 | |||
127 | key = KeyPair.objects.generate_key(primary=True) |
|
118 | key = KeyPair.objects.generate_key(primary=True) | |
128 |
|
119 | |||
129 | tag = Tag.objects.create(name='test_tag') |
|
120 | tag = Tag.objects.create(name='test_tag') | |
130 |
|
121 | |||
131 | post = Post.objects.create_post(title='', text='', tags=[tag]) |
|
122 | post = Post.objects.create_post(title='', text='', tags=[tag]) | |
132 | post_local_reflink = Post.objects.create_post(title='', |
|
123 | post_local_reflink = Post.objects.create_post(title='', | |
133 | text='[post]%d[/post]' % post.id, thread=post.get_thread()) |
|
124 | text='[post]%d[/post]' % post.id, thread=post.get_thread()) | |
134 |
|
125 | |||
135 | self.assertTrue(post_local_reflink in post.referenced_posts.all(), |
|
126 | self.assertTrue(post_local_reflink in post.referenced_posts.all(), | |
136 | 'Local reflink not connecting posts.') |
|
127 | 'Local reflink not connecting posts.') | |
137 |
|
128 | |||
138 |
|
129 | |||
139 | def test_thread_replies(self): |
|
130 | def test_thread_replies(self): | |
140 | """ |
|
131 | """ | |
141 | Tests that the replies can be queried from a thread in all possible |
|
132 | Tests that the replies can be queried from a thread in all possible | |
142 | ways. |
|
133 | ways. | |
143 | """ |
|
134 | """ | |
144 |
|
135 | |||
145 | tag = Tag.objects.create(name='test_tag') |
|
136 | tag = Tag.objects.create(name='test_tag') | |
146 | opening_post = Post.objects.create_post(title='title', text='text', |
|
137 | opening_post = Post.objects.create_post(title='title', text='text', | |
147 | tags=[tag]) |
|
138 | tags=[tag]) | |
148 | thread = opening_post.get_thread() |
|
139 | thread = opening_post.get_thread() | |
149 |
|
140 | |||
150 | Post.objects.create_post(title='title', text='text', thread=thread) |
|
141 | Post.objects.create_post(title='title', text='text', thread=thread) | |
151 | Post.objects.create_post(title='title', text='text', thread=thread) |
|
142 | Post.objects.create_post(title='title', text='text', thread=thread) | |
152 |
|
143 | |||
153 | replies = thread.get_replies() |
|
144 | replies = thread.get_replies() | |
154 | self.assertTrue(len(replies) > 0, 'No replies found for thread.') |
|
145 | self.assertTrue(len(replies) > 0, 'No replies found for thread.') | |
155 |
|
146 | |||
156 | replies = thread.get_replies(view_fields_only=True) |
|
147 | replies = thread.get_replies(view_fields_only=True) | |
157 | self.assertTrue(len(replies) > 0, |
|
148 | self.assertTrue(len(replies) > 0, | |
158 | 'No replies found for thread with view fields only.') |
|
149 | 'No replies found for thread with view fields only.') | |
159 |
|
150 | |||
160 | def test_bumplimit(self): |
|
151 | def test_bumplimit(self): | |
161 | """ |
|
152 | """ | |
162 | Tests that the thread bumpable status is changed and post uids and |
|
153 | Tests that the thread bumpable status is changed and post uids and | |
163 | last update times are updated across all post threads. |
|
154 | last update times are updated across all post threads. | |
164 | """ |
|
155 | """ | |
165 |
|
156 | |||
166 | op1 = Post.objects.create_post(title='title', text='text') |
|
157 | op1 = Post.objects.create_post(title='title', text='text') | |
167 | op2 = Post.objects.create_post(title='title', text='text') |
|
158 | op2 = Post.objects.create_post(title='title', text='text') | |
168 |
|
159 | |||
169 | thread1 = op1.get_thread() |
|
160 | thread1 = op1.get_thread() | |
170 | thread1.max_posts = 5 |
|
161 | thread1.max_posts = 5 | |
171 | thread1.save() |
|
162 | thread1.save() | |
172 |
|
163 | |||
173 | uid_1 = op1.uid |
|
164 | uid_1 = op1.uid | |
174 | uid_2 = op2.uid |
|
165 | uid_2 = op2.uid | |
175 |
|
166 | |||
176 | # Create multi reply |
|
167 | # Create multi reply | |
177 | Post.objects.create_post( |
|
168 | Post.objects.create_post( | |
178 | title='title', text='text', thread=thread1, |
|
169 | title='title', text='text', thread=thread1, | |
179 | opening_posts=[op1, op2]) |
|
170 | opening_posts=[op1, op2]) | |
180 | thread_update_time_2 = op2.get_thread().last_edit_time |
|
171 | thread_update_time_2 = op2.get_thread().last_edit_time | |
181 | for i in range(6): |
|
172 | for i in range(6): | |
182 | Post.objects.create_post(title='title', text='text', |
|
173 | Post.objects.create_post(title='title', text='text', | |
183 | thread=thread1) |
|
174 | thread=thread1) | |
184 |
|
175 | |||
185 | self.assertFalse(op1.get_thread().can_bump(), |
|
176 | self.assertFalse(op1.get_thread().can_bump(), | |
186 | 'Thread is bumpable when it should not be.') |
|
177 | 'Thread is bumpable when it should not be.') | |
187 | self.assertTrue(op2.get_thread().can_bump(), |
|
178 | self.assertTrue(op2.get_thread().can_bump(), | |
188 | 'Thread is not bumpable when it should be.') |
|
179 | 'Thread is not bumpable when it should be.') | |
189 | self.assertNotEqual( |
|
180 | self.assertNotEqual( | |
190 | uid_1, Post.objects.get(id=op1.id).uid, |
|
181 | uid_1, Post.objects.get(id=op1.id).uid, | |
191 | 'UID of the first OP should be changed but it is not.') |
|
182 | 'UID of the first OP should be changed but it is not.') | |
192 | self.assertEqual( |
|
183 | self.assertEqual( | |
193 | uid_2, Post.objects.get(id=op2.id).uid, |
|
184 | uid_2, Post.objects.get(id=op2.id).uid, | |
194 | 'UID of the first OP should not be changed but it is.') |
|
185 | 'UID of the first OP should not be changed but it is.') | |
195 |
|
186 | |||
196 | self.assertNotEqual( |
|
187 | self.assertNotEqual( | |
197 | thread_update_time_2, |
|
188 | thread_update_time_2, | |
198 | Thread.objects.get(id=op2.get_thread().id).last_edit_time, |
|
189 | Thread.objects.get(id=op2.get_thread().id).last_edit_time, | |
199 | 'Thread last update time should change when the other thread ' |
|
190 | 'Thread last update time should change when the other thread ' | |
200 | 'changes status.') |
|
191 | 'changes status.') |
General Comments 0
You need to be logged in to leave comments.
Login now