Show More
@@ -1,191 +1,176 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_pages(self): |
|
93 | def test_pages(self): | |
94 | """Test that the thread list is properly split into pages""" |
|
94 | """Test that the thread list is properly split into pages""" | |
95 |
|
95 | |||
96 | for i in range(settings.get_int('View', 'ThreadsPerPage') * 2): |
|
96 | for i in range(settings.get_int('View', 'ThreadsPerPage') * 2): | |
97 | self._create_post() |
|
97 | self._create_post() | |
98 |
|
98 | |||
99 | all_threads = Thread.objects.exclude(status=STATUS_ARCHIVE) |
|
99 | all_threads = Thread.objects.exclude(status=STATUS_ARCHIVE) | |
100 |
|
100 | |||
101 | paginator = Paginator(Thread.objects.exclude(status=STATUS_ARCHIVE), |
|
101 | paginator = Paginator(Thread.objects.exclude(status=STATUS_ARCHIVE), | |
102 | settings.get_int('View', 'ThreadsPerPage')) |
|
102 | settings.get_int('View', 'ThreadsPerPage')) | |
103 | posts_in_second_page = paginator.page(2).object_list |
|
103 | posts_in_second_page = paginator.page(2).object_list | |
104 | first_post = posts_in_second_page[0] |
|
104 | first_post = posts_in_second_page[0] | |
105 |
|
105 | |||
106 | self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id, |
|
106 | self.assertEqual(all_threads[settings.get_int('View', 'ThreadsPerPage')].id, | |
107 | first_post.id) |
|
107 | first_post.id) | |
108 |
|
108 | |||
109 | def test_reflinks(self): |
|
109 | def test_reflinks(self): | |
110 | """ |
|
110 | """ | |
111 | Tests that reflinks are parsed within post and connecting replies |
|
111 | Tests that reflinks are parsed within post and connecting replies | |
112 | to the replied posts. |
|
112 | to the replied posts. | |
113 |
|
113 | |||
114 | Local reflink example: [post]123[/post] |
|
114 | Local reflink example: [post]123[/post] | |
115 | Global reflink example: [post]key_type::key::123[/post] |
|
115 | Global reflink example: [post]key_type::key::123[/post] | |
116 | """ |
|
116 | """ | |
117 |
|
117 | |||
118 | key = KeyPair.objects.generate_key(primary=True) |
|
118 | key = KeyPair.objects.generate_key(primary=True) | |
119 |
|
119 | |||
120 | tag = Tag.objects.create(name='test_tag') |
|
120 | tag = Tag.objects.create(name='test_tag') | |
121 |
|
121 | |||
122 | post = Post.objects.create_post(title='', text='', tags=[tag]) |
|
122 | post = Post.objects.create_post(title='', text='', tags=[tag]) | |
123 | post_local_reflink = Post.objects.create_post(title='', |
|
123 | post_local_reflink = Post.objects.create_post(title='', | |
124 | text='[post]%d[/post]' % post.id, thread=post.get_thread()) |
|
124 | text='[post]%d[/post]' % post.id, thread=post.get_thread()) | |
125 |
|
125 | |||
126 | self.assertTrue(post_local_reflink in post.referenced_posts.all(), |
|
126 | self.assertTrue(post_local_reflink in post.referenced_posts.all(), | |
127 | 'Local reflink not connecting posts.') |
|
127 | 'Local reflink not connecting posts.') | |
128 |
|
128 | |||
129 |
|
129 | |||
130 | def test_thread_replies(self): |
|
130 | def test_thread_replies(self): | |
131 | """ |
|
131 | """ | |
132 | 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 | |
133 | ways. |
|
133 | ways. | |
134 | """ |
|
134 | """ | |
135 |
|
135 | |||
136 | tag = Tag.objects.create(name='test_tag') |
|
136 | tag = Tag.objects.create(name='test_tag') | |
137 | opening_post = Post.objects.create_post(title='title', text='text', |
|
137 | opening_post = Post.objects.create_post(title='title', text='text', | |
138 | tags=[tag]) |
|
138 | tags=[tag]) | |
139 | thread = opening_post.get_thread() |
|
139 | thread = opening_post.get_thread() | |
140 |
|
140 | |||
141 | Post.objects.create_post(title='title', text='text', thread=thread) |
|
141 | Post.objects.create_post(title='title', text='text', thread=thread) | |
142 | Post.objects.create_post(title='title', text='text', thread=thread) |
|
142 | Post.objects.create_post(title='title', text='text', thread=thread) | |
143 |
|
143 | |||
144 | replies = thread.get_replies() |
|
144 | replies = thread.get_replies() | |
145 | self.assertTrue(len(replies) > 0, 'No replies found for thread.') |
|
145 | self.assertTrue(len(replies) > 0, 'No replies found for thread.') | |
146 |
|
146 | |||
147 | replies = thread.get_replies(view_fields_only=True) |
|
147 | replies = thread.get_replies(view_fields_only=True) | |
148 | self.assertTrue(len(replies) > 0, |
|
148 | self.assertTrue(len(replies) > 0, | |
149 | 'No replies found for thread with view fields only.') |
|
149 | 'No replies found for thread with view fields only.') | |
150 |
|
150 | |||
151 | def test_bumplimit(self): |
|
151 | def test_bumplimit(self): | |
152 | """ |
|
152 | """ | |
153 | 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 | |
154 | last update times are updated across all post threads. |
|
154 | last update times are updated across all post threads. | |
155 | """ |
|
155 | """ | |
156 |
|
156 | |||
157 | op1 = Post.objects.create_post(title='title', text='text') |
|
157 | op1 = Post.objects.create_post(title='title', text='text') | |
158 | op2 = Post.objects.create_post(title='title', text='text') |
|
|||
159 |
|
158 | |||
160 | thread1 = op1.get_thread() |
|
159 | thread1 = op1.get_thread() | |
161 | thread1.max_posts = 5 |
|
160 | thread1.max_posts = 5 | |
162 | thread1.save() |
|
161 | thread1.save() | |
163 |
|
162 | |||
164 | uid_1 = op1.uid |
|
163 | uid_1 = op1.uid | |
165 | uid_2 = op2.uid |
|
|||
166 |
|
164 | |||
167 | # Create multi reply |
|
165 | # Create multi reply | |
168 | Post.objects.create_post( |
|
166 | Post.objects.create_post( | |
169 |
title='title', text='text', thread=thread1 |
|
167 | title='title', text='text', thread=thread1) | |
170 | opening_posts=[op1, op2]) |
|
|||
171 | thread_update_time_2 = op2.get_thread().last_edit_time |
|
|||
172 | for i in range(6): |
|
168 | for i in range(6): | |
173 | Post.objects.create_post(title='title', text='text', |
|
169 | Post.objects.create_post(title='title', text='text', | |
174 | thread=thread1) |
|
170 | thread=thread1) | |
175 |
|
171 | |||
176 | self.assertFalse(op1.get_thread().can_bump(), |
|
172 | self.assertFalse(op1.get_thread().can_bump(), | |
177 | 'Thread is bumpable when it should not be.') |
|
173 | 'Thread is bumpable when it should not be.') | |
178 | self.assertTrue(op2.get_thread().can_bump(), |
|
|||
179 | 'Thread is not bumpable when it should be.') |
|
|||
180 | self.assertNotEqual( |
|
174 | self.assertNotEqual( | |
181 | uid_1, Post.objects.get(id=op1.id).uid, |
|
175 | uid_1, Post.objects.get(id=op1.id).uid, | |
182 | 'UID of the first OP should be changed but it is not.') |
|
176 | 'UID of the first OP should be changed but it is not.') | |
183 | self.assertEqual( |
|
|||
184 | uid_2, Post.objects.get(id=op2.id).uid, |
|
|||
185 | 'UID of the first OP should not be changed but it is.') |
|
|||
186 |
|
||||
187 | self.assertNotEqual( |
|
|||
188 | thread_update_time_2, |
|
|||
189 | Thread.objects.get(id=op2.get_thread().id).last_edit_time, |
|
|||
190 | 'Thread last update time should change when the other thread ' |
|
|||
191 | 'changes status.') |
|
General Comments 0
You need to be logged in to leave comments.
Login now