|
|
import simplejson
|
|
|
|
|
|
from django.test import TestCase
|
|
|
from boards.views import api
|
|
|
|
|
|
from boards.models import Tag, Post
|
|
|
from boards.tests.mocks import MockRequest
|
|
|
from boards.utils import datetime_to_epoch
|
|
|
|
|
|
|
|
|
class ApiTest(TestCase):
|
|
|
def test_thread_diff(self):
|
|
|
tag = Tag.objects.create(name='test_tag')
|
|
|
opening_post = Post.objects.create_post(title='title', text='text',
|
|
|
tags=[tag])
|
|
|
|
|
|
last_edit_time = str(opening_post.last_edit_time)
|
|
|
|
|
|
req = MockRequest()
|
|
|
req.GET['thread'] = opening_post.id
|
|
|
req.GET['last_update'] = last_edit_time
|
|
|
req.GET['last_post'] = opening_post.id
|
|
|
# Check the exact timestamp post was added
|
|
|
empty_response = api.api_get_threaddiff(req)
|
|
|
diff = simplejson.loads(empty_response.content)
|
|
|
self.assertEqual(0, len(diff['added']),
|
|
|
'There must be no added posts in the diff.')
|
|
|
self.assertEqual(0, len(diff['updated']),
|
|
|
'There must be no updated posts in the diff.')
|
|
|
|
|
|
reply = Post.objects.create_post(title='',
|
|
|
text='[post]%d[/post]\ntext' % opening_post.id,
|
|
|
thread=opening_post.get_thread())
|
|
|
|
|
|
req = MockRequest()
|
|
|
req.GET['thread'] = opening_post.id
|
|
|
req.GET['last_update'] = last_edit_time
|
|
|
req.GET['last_post'] = opening_post.id
|
|
|
# Check the timestamp before post was added
|
|
|
response = api.api_get_threaddiff(req)
|
|
|
diff = simplejson.loads(response.content)
|
|
|
self.assertEqual(1, len(diff['added']),
|
|
|
'There must be 1 added posts in the diff.')
|
|
|
self.assertEqual(1, len(diff['updated']),
|
|
|
'There must be 1 updated posts in the diff.')
|
|
|
|
|
|
req = MockRequest()
|
|
|
req.GET['thread'] = opening_post.id
|
|
|
req.GET['last_update'] = str(reply.last_edit_time)
|
|
|
req.GET['last_post'] = reply.id
|
|
|
empty_response = api.api_get_threaddiff(req)
|
|
|
diff = simplejson.loads(empty_response.content)
|
|
|
self.assertEqual(0, len(diff['added']),
|
|
|
'There must be no added posts in the diff.')
|
|
|
self.assertEqual(0, len(diff['updated']),
|
|
|
'There must be no updated posts in the diff.')
|
|
|
|
|
|
def test_get_threads(self):
|
|
|
# Create 10 threads
|
|
|
tag = Tag.objects.create(name='test_tag')
|
|
|
for i in range(5):
|
|
|
Post.objects.create_post(title='title', text='text', tags=[tag])
|
|
|
|
|
|
# Get all threads
|
|
|
response = api.api_get_threads(MockRequest(), 5)
|
|
|
diff = simplejson.loads(response.content)
|
|
|
self.assertEqual(5, len(diff), 'Invalid thread list response.')
|
|
|
|
|
|
# Get less threads then exist
|
|
|
response = api.api_get_threads(MockRequest(), 3)
|
|
|
diff = simplejson.loads(response.content)
|
|
|
self.assertEqual(3, len(diff), 'Invalid thread list response.')
|
|
|
|
|
|
# Get more threads then exist
|
|
|
response = api.api_get_threads(MockRequest(), 10)
|
|
|
diff = simplejson.loads(response.content)
|
|
|
self.assertEqual(5, len(diff), 'Invalid thread list response.')
|
|
|
|