Created
June 15, 2012 16:18
-
-
Save demux/2937361 to your computer and use it in GitHub Desktop.
Unit test for liveproject upload api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django.utils import unittest | |
from django.test.client import Client | |
import simplejson as json | |
import hashlib | |
from settings import PROJECT_PATH | |
from main.models import Event | |
from pprint import * | |
class UploadTestCase(unittest.TestCase): | |
def setUp(self): | |
self.client = Client() | |
e = Event(slug='unittest', name='Unittest Event') | |
e.save() | |
def test_create_upload(self): | |
""" | |
Open the test file and split in into chunks: | |
""" | |
string = open(PROJECT_PATH + '/uploads/tests/test.jpg', 'rb').read() | |
size = len(string) | |
chunk_size = size / 4 | |
chunks = [] | |
_chunks = [] | |
pointer = 0 | |
while pointer < size: | |
chunk = string[pointer:pointer+chunk_size] | |
_chunks.append(chunk) | |
chunks.append({ | |
'size': len(chunk), | |
'checksum': hashlib.md5(chunk).hexdigest() | |
}) | |
pointer += chunk_size | |
pprint(chunks) | |
self.assertEqual(string, ''.join(_chunks)) | |
""" | |
Create a new Upload object and UploadChunk objects: | |
(Send information about the created chunks) | |
""" | |
res = self.client.post('/upload_api/upload/', json.dumps({ | |
'event': "unittest", | |
'email': "arnar1@gmail.com", | |
'nickname': "demux", | |
'user_id': "foobar", | |
'caption': "test caption áæöíð", | |
'type': "image", | |
'checksum': hashlib.md5(string).hexdigest(), | |
'chunks': chunks | |
}), content_type="text/json") | |
print res.status_code | |
print res.content | |
upload = json.loads(res.content) | |
self.assertEqual(upload['status'], 'success') | |
""" | |
Send the data: | |
""" | |
i = 0 | |
for chunk in upload['chunks']: | |
data = _chunks[i] | |
res = self.client.post( | |
'/upload_api/upload_chunk/%s/' % chunk['id'], | |
data, content_type="application/octet-stream") | |
print res.status_code | |
print res.content | |
result = json.loads(res.content) | |
self.assertNotEqual(result['status'], 'fail') | |
i += 1 | |
""" | |
Get upload status: | |
""" | |
# Using checksum: | |
res = self.client.get('/upload_api/upload/%s/' % upload['checksum']) | |
print res.status_code | |
print res.content | |
# Using id: | |
res = self.client.get('/upload_api/upload/%s/' % upload['id']) | |
print res.status_code | |
print res.content | |
""" | |
Update the upload: | |
""" | |
print upload['secret'] | |
res = self.client.post( | |
'/upload_api/update_upload/%s/' % upload['id'], | |
json.dumps({ | |
'secret': upload['secret'], | |
'caption': 'test caption áæöíð', | |
'email': 'arnar@liveproject.me', | |
'nickname': 'updated tester' | |
} | |
), content_type="text/json") | |
print res.status_code | |
print res.content | |
result = json.loads(res.content) | |
self.assertNotEqual(result['status'], 'fail') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment