from django test import TestCase from django core urlresolvers import

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.test import TestCase
from django.core.urlresolvers import reverse
from django.utils import simplejson
from django.conf import settings
from code.util import generate_styles
from code.views import API_STYLE_YES, API_STYLE_NO
from code.models import Code
class DumpzTestCase(TestCase):
def test_api_upload(self):
upload_url = reverse('api_upload')
def upload(args):
resp = self.client.post(upload_url, args)
resp = simplejson.loads(resp.content)
return resp
resp = upload({})
self.assertEqual(resp['error'], 'Empty code field')
TEST_ANSWER = '<table class="highlighttable"><tr><td class="linenos"><pre>1</pre></td><td class="code"><div class="highlight"><pre>test\n</pre></div>\n</td></tr></table>'
STYLES = generate_styles()
resp = upload({'code': 'test'})
self.assertEqual('<style type="text/css">%s</style>\n\n%s' % (STYLES, TEST_ANSWER), resp['html'])
resp = upload({'code': 'test', 'style': API_STYLE_NO})
code = Code.objects.order_by('-id')[0]
self.assertEqual(TEST_ANSWER, resp['html'])
self.assertEqual('http://%s%s' % (settings.DOMAIN, reverse('show_dump', args=[code.id])), resp['url'])
self.assertEqual('http://%s%s' % (settings.DOMAIN, reverse('show_dump_text', args=[code.id])), resp['text_url'])
self.assertEqual('text', code.lexer)
self.assertEqual(settings.DUMPZ_PYGMENTS_CSS_LINK, resp['css_link'])
resp = upload({'code': 'test', 'style': API_STYLE_NO, 'lexer': 'python'})
code = Code.objects.order_by('-id')[0]
self.assertEqual('python', code.lexer)