test_sync.py
105 lines
| 3.5 KiB
| text/x-python
|
PythonLexer
neko259
|
r1586 | from django.test import TestCase | ||
neko259
|
r1239 | from boards.models import KeyPair, Post, Tag | ||
neko259
|
r1229 | from boards.models.post.sync import SyncManager | ||
neko259
|
r836 | from boards.tests.mocks import MockRequest | ||
neko259
|
r1227 | from boards.views.sync import response_get | ||
neko259
|
r836 | |||
__author__ = 'neko259' | ||||
class SyncTest(TestCase): | ||||
def test_get(self): | ||||
""" | ||||
Forms a GET request of a post and checks the response. | ||||
""" | ||||
neko259
|
r1503 | key = KeyPair.objects.generate_key(primary=True) | ||
neko259
|
r1239 | tag = Tag.objects.create(name='tag1') | ||
neko259
|
r1504 | post = Post.objects.create_post(title='test_title', | ||
text='test_text\rline two', | ||||
neko259
|
r1239 | tags=[tag]) | ||
neko259
|
r836 | |||
request = MockRequest() | ||||
neko259
|
r1227 | request.body = ( | ||
neko259
|
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) | ||||
) | ||||
neko259
|
r1229 | response = response_get(request).content.decode() | ||
neko259
|
r836 | self.assertTrue( | ||
'<status>success</status>' | ||||
'<models>' | ||||
neko259
|
r838 | '<model name="post">' | ||
'<content>' | ||||
neko259
|
r836 | '<id key="%s" local-id="%d" type="%s" />' | ||
'<title>%s</title>' | ||||
'<text>%s</text>' | ||||
neko259
|
r1239 | '<tags><tag>%s</tag></tags>' | ||
neko259
|
r1229 | '<pub-time>%s</pub-time>' | ||
neko259
|
r1571 | '<version>%s</version>' | ||
neko259
|
r838 | '</content>' % ( | ||
neko259
|
r836 | post.global_id.key, | ||
neko259
|
r1503 | post.global_id.local_id, | ||
neko259
|
r836 | post.global_id.key_type, | ||
post.title, | ||||
neko259
|
r1504 | post.get_sync_text(), | ||
neko259
|
r1239 | post.get_thread().get_tags().first().name, | ||
neko259
|
r1229 | post.get_pub_time_str(), | ||
neko259
|
r1571 | post.version, | ||
neko259
|
r1503 | ) in response, | ||
neko259
|
r935 | 'Wrong response generated for the GET request.') | ||
neko259
|
r1229 | |||
post.delete() | ||||
neko259
|
r1503 | key.delete() | ||
KeyPair.objects.generate_key(primary=True) | ||||
neko259
|
r1229 | |||
neko259
|
r1511 | SyncManager.parse_response_get(response, None) | ||
neko259
|
r1229 | self.assertEqual(1, Post.objects.count(), | ||
'Post was not created from XML response.') | ||||
neko259
|
r1232 | |||
neko259
|
r1239 | parsed_post = Post.objects.first() | ||
self.assertEqual('tag1', | ||||
parsed_post.get_thread().get_tags().first().name, | ||||
'Invalid tag was parsed.') | ||||
neko259
|
r1511 | SyncManager.parse_response_get(response, None) | ||
neko259
|
r1232 | self.assertEqual(1, Post.objects.count(), | ||
'The same post was imported twice.') | ||||
neko259
|
r1244 | |||
self.assertEqual(1, parsed_post.global_id.signature_set.count(), | ||||
'Signature was not saved.') | ||||
neko259
|
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>' | ||||
neko259
|
r1571 | '<version>%s</version>' | ||
neko259
|
r1503 | '</content>' % ( | ||
post.global_id.key, | ||||
post.global_id.local_id, | ||||
post.global_id.key_type, | ||||
post.title, | ||||
neko259
|
r1504 | post.get_sync_text(), | ||
neko259
|
r1503 | post.get_thread().get_tags().first().name, | ||
post.get_pub_time_str(), | ||||
neko259
|
r1571 | post.version, | ||
neko259
|
r1503 | ) in response, | ||
'Wrong response generated for the GET request.') | ||||