test_sync.py
214 lines
| 7.6 KiB
| text/x-python
|
PythonLexer
|
r1586 | from django.test import TestCase | ||
|
r1239 | from boards.models import KeyPair, Post, Tag | ||
|
r1229 | from boards.models.post.sync import SyncManager | ||
|
r836 | from boards.tests.mocks import MockRequest | ||
|
r1834 | from boards.views.sync import response_get, response_list | ||
|
r836 | |||
__author__ = 'neko259' | ||||
class SyncTest(TestCase): | ||||
def test_get(self): | ||||
""" | ||||
Forms a GET request of a post and checks the response. | ||||
""" | ||||
|
r1503 | key = KeyPair.objects.generate_key(primary=True) | ||
|
r1239 | tag = Tag.objects.create(name='tag1') | ||
|
r1504 | post = Post.objects.create_post(title='test_title', | ||
text='test_text\rline two', | ||||
|
r1239 | tags=[tag]) | ||
|
r836 | |||
request = MockRequest() | ||||
|
r1227 | request.body = ( | ||
|
r836 | '<request type="get" version="1.0">' | ||
'<model name="post" version="1.0">' | ||||
'<id key="%s" local-id="%d" type="%s" />' | ||||
'</model>' | ||||
'</request>' % (post.global_id.key, | ||||
post.id, | ||||
post.global_id.key_type) | ||||
) | ||||
|
r1229 | response = response_get(request).content.decode() | ||
|
r836 | self.assertTrue( | ||
'<status>success</status>' | ||||
'<models>' | ||||
|
r838 | '<model name="post">' | ||
'<content>' | ||||
|
r836 | '<id key="%s" local-id="%d" type="%s" />' | ||
'<title>%s</title>' | ||||
'<text>%s</text>' | ||||
|
r1239 | '<tags><tag>%s</tag></tags>' | ||
|
r1229 | '<pub-time>%s</pub-time>' | ||
|
r1571 | '<version>%s</version>' | ||
|
r838 | '</content>' % ( | ||
|
r836 | post.global_id.key, | ||
|
r1503 | post.global_id.local_id, | ||
|
r836 | post.global_id.key_type, | ||
post.title, | ||||
|
r1504 | post.get_sync_text(), | ||
|
r1239 | post.get_thread().get_tags().first().name, | ||
|
r1229 | post.get_pub_time_str(), | ||
|
r1571 | post.version, | ||
|
r1503 | ) in response, | ||
|
r935 | 'Wrong response generated for the GET request.') | ||
|
r1229 | |||
post.delete() | ||||
|
r1503 | key.delete() | ||
KeyPair.objects.generate_key(primary=True) | ||||
|
r1229 | |||
|
r1511 | SyncManager.parse_response_get(response, None) | ||
|
r1229 | self.assertEqual(1, Post.objects.count(), | ||
'Post was not created from XML response.') | ||||
|
r1232 | |||
|
r1239 | parsed_post = Post.objects.first() | ||
self.assertEqual('tag1', | ||||
parsed_post.get_thread().get_tags().first().name, | ||||
'Invalid tag was parsed.') | ||||
|
r1511 | SyncManager.parse_response_get(response, None) | ||
|
r1232 | self.assertEqual(1, Post.objects.count(), | ||
'The same post was imported twice.') | ||||
|
r1244 | |||
self.assertEqual(1, parsed_post.global_id.signature_set.count(), | ||||
'Signature was not saved.') | ||||
|
r1503 | |||
post = parsed_post | ||||
# Trying to sync the same once more | ||||
response = response_get(request).content.decode() | ||||
self.assertTrue( | ||||
'<status>success</status>' | ||||
'<models>' | ||||
'<model name="post">' | ||||
'<content>' | ||||
'<id key="%s" local-id="%d" type="%s" />' | ||||
'<title>%s</title>' | ||||
'<text>%s</text>' | ||||
'<tags><tag>%s</tag></tags>' | ||||
'<pub-time>%s</pub-time>' | ||||
|
r1571 | '<version>%s</version>' | ||
|
r1503 | '</content>' % ( | ||
post.global_id.key, | ||||
post.global_id.local_id, | ||||
post.global_id.key_type, | ||||
post.title, | ||||
|
r1504 | post.get_sync_text(), | ||
|
r1503 | post.get_thread().get_tags().first().name, | ||
post.get_pub_time_str(), | ||||
|
r1571 | post.version, | ||
|
r1503 | ) in response, | ||
'Wrong response generated for the GET request.') | ||||
|
r1834 | |||
def test_list_all(self): | ||||
key = KeyPair.objects.generate_key(primary=True) | ||||
tag = Tag.objects.create(name='tag1') | ||||
post = Post.objects.create_post(title='test_title', | ||||
text='test_text\rline two', | ||||
tags=[tag]) | ||||
post2 = Post.objects.create_post(title='test title 2', | ||||
text='test text 2', | ||||
tags=[tag]) | ||||
request_all = MockRequest() | ||||
request_all.body = ( | ||||
'<request type="list" version="1.0">' | ||||
'<model name="post" version="1.0">' | ||||
'</model>' | ||||
'</request>' | ||||
) | ||||
response_all = response_list(request_all).content.decode() | ||||
self.assertTrue( | ||||
'<status>success</status>' | ||||
'<models>' | ||||
'<model>' | ||||
'<id key="{}" local-id="{}" type="{}" />' | ||||
'<version>{}</version>' | ||||
'</model>' | ||||
'<model>' | ||||
'<id key="{}" local-id="{}" type="{}" />' | ||||
'<version>{}</version>' | ||||
'</model>' | ||||
'</models>'.format( | ||||
post.global_id.key, | ||||
post.global_id.local_id, | ||||
post.global_id.key_type, | ||||
post.version, | ||||
post2.global_id.key, | ||||
post2.global_id.local_id, | ||||
post2.global_id.key_type, | ||||
post2.version, | ||||
) in response_all, | ||||
'Wrong response generated for the LIST request for all posts.') | ||||
def test_list_existing_thread(self): | ||||
key = KeyPair.objects.generate_key(primary=True) | ||||
tag = Tag.objects.create(name='tag1') | ||||
post = Post.objects.create_post(title='test_title', | ||||
text='test_text\rline two', | ||||
tags=[tag]) | ||||
post2 = Post.objects.create_post(title='test title 2', | ||||
text='test text 2', | ||||
tags=[tag]) | ||||
request_thread = MockRequest() | ||||
request_thread.body = ( | ||||
'<request type="list" version="1.0">' | ||||
'<model name="post" version="1.0">' | ||||
'<thread>{}</thread>' | ||||
'</model>' | ||||
'</request>'.format( | ||||
post.id, | ||||
) | ||||
) | ||||
response_thread = response_list(request_thread).content.decode() | ||||
self.assertTrue( | ||||
'<status>success</status>' | ||||
'<models>' | ||||
'<model>' | ||||
'<id key="{}" local-id="{}" type="{}" />' | ||||
'<version>{}</version>' | ||||
'</model>' | ||||
'</models>'.format( | ||||
post.global_id.key, | ||||
post.global_id.local_id, | ||||
post.global_id.key_type, | ||||
post.version, | ||||
) in response_thread, | ||||
'Wrong response generated for the LIST request for posts of ' | ||||
'existing thread.') | ||||
def test_list_non_existing_thread(self): | ||||
key = KeyPair.objects.generate_key(primary=True) | ||||
tag = Tag.objects.create(name='tag1') | ||||
post = Post.objects.create_post(title='test_title', | ||||
text='test_text\rline two', | ||||
tags=[tag]) | ||||
post2 = Post.objects.create_post(title='test title 2', | ||||
text='test text 2', | ||||
tags=[tag]) | ||||
request_thread = MockRequest() | ||||
request_thread.body = ( | ||||
'<request type="list" version="1.0">' | ||||
'<model name="post" version="1.0">' | ||||
'<thread>{}</thread>' | ||||
'</model>' | ||||
'</request>'.format( | ||||
0, | ||||
) | ||||
) | ||||
response_thread = response_list(request_thread).content.decode() | ||||
self.assertTrue( | ||||
'<status>success</status>' | ||||
'<models />' | ||||
in response_thread, | ||||
'Wrong response generated for the LIST request for posts of ' | ||||
'non-existing thread.') | ||||