##// END OF EJS Templates
Added test for reflinks. Added management command to get posts from other node...
Added test for reflinks. Added management command to get posts from other node (incomplete). Added ability to parse global reflinks in the text

File last commit:

r841:c295c39c decentral
r841:c295c39c decentral
Show More
test_post.py
142 lines | 4.7 KiB | text/x-python | PythonLexer
from django.core.paginator import Paginator
from django.test import TestCase
from boards import settings
from boards.models import Tag, Post, Thread, KeyPair
class PostTests(TestCase):
def _create_post(self):
tag = Tag.objects.create(name='test_tag')
return Post.objects.create_post(title='title', text='text',
tags=[tag])
def test_post_add(self):
"""Test adding post"""
post = self._create_post()
self.assertIsNotNone(post, 'No post was created.')
self.assertEqual('test_tag', post.get_thread().tags.all()[0].name,
'No tags were added to the post.')
def test_delete_post(self):
"""Test post deletion"""
post = self._create_post()
post_id = post.id
Post.objects.delete_post(post)
self.assertFalse(Post.objects.filter(id=post_id).exists())
def test_delete_thread(self):
"""Test thread deletion"""
opening_post = self._create_post()
thread = opening_post.get_thread()
reply = Post.objects.create_post("", "", thread=thread)
thread.delete()
self.assertFalse(Post.objects.filter(id=reply.id).exists())
def test_post_to_thread(self):
"""Test adding post to a thread"""
op = self._create_post()
post = Post.objects.create_post("", "", thread=op.get_thread())
self.assertIsNotNone(post, 'Reply to thread wasn\'t created')
self.assertEqual(op.get_thread().last_edit_time, post.pub_time,
'Post\'s create time doesn\'t match thread last edit'
' time')
def test_delete_posts_by_ip(self):
"""Test deleting posts with the given ip"""
post = self._create_post()
post_id = post.id
Post.objects.delete_posts_by_ip('0.0.0.0')
self.assertFalse(Post.objects.filter(id=post_id).exists())
def test_get_thread(self):
"""Test getting all posts of a thread"""
opening_post = self._create_post()
for i in range(2):
Post.objects.create_post('title', 'text',
thread=opening_post.get_thread())
thread = opening_post.get_thread()
self.assertEqual(3, thread.replies.count())
def test_create_post_with_tag(self):
"""Test adding tag to post"""
tag = Tag.objects.create(name='test_tag')
post = Post.objects.create_post(title='title', text='text', tags=[tag])
thread = post.get_thread()
self.assertIsNotNone(post, 'Post not created')
self.assertTrue(tag in thread.tags.all(), 'Tag not added to thread')
self.assertTrue(thread in tag.threads.all(), 'Thread not added to tag')
def test_thread_max_count(self):
"""Test deletion of old posts when the max thread count is reached"""
for i in range(settings.MAX_THREAD_COUNT + 1):
self._create_post()
self.assertEqual(settings.MAX_THREAD_COUNT,
len(Thread.objects.filter(archived=False)))
def test_pages(self):
"""Test that the thread list is properly split into pages"""
for i in range(settings.MAX_THREAD_COUNT):
self._create_post()
all_threads = Thread.objects.filter(archived=False)
paginator = Paginator(Thread.objects.filter(archived=False),
settings.THREADS_PER_PAGE)
posts_in_second_page = paginator.page(2).object_list
first_post = posts_in_second_page[0]
self.assertEqual(all_threads[settings.THREADS_PER_PAGE].id,
first_post.id)
def test_reflinks(self):
"""
Tests that reflinks are parsed within post and connecting replies
to the replied posts.
Local reflink example: [post]123[/post]
Global reflink example: [post]key_type::key::123[/post]
"""
key = KeyPair.objects.generate_key(primary=True)
tag = Tag.objects.create(name='test_tag')
post = Post.objects.create_post(title='', text='', tags=[tag])
post_local_reflink = Post.objects.create_post(title='',
text='[post]%d[/post]' % post.id, thread=post.get_thread())
self.assertTrue(post_local_reflink in post.referenced_posts.all(),
'Local reflink not connecting posts.')
post_global_reflink = Post.objects.create_post(title='',
text='[post]%s::%s::%d[/post]' % (
post.global_id.key_type, post.global_id.key, post.id),
thread=post.get_thread())
self.assertTrue(post_global_reflink in post.referenced_posts.all(),
'Global reflink not connecting posts.')
# TODO Check that links are parsed into the rendered text