2011-03-09 02:26:20 +03:00
|
|
|
from django.test import TestCase
|
2011-04-25 07:50:28 +04:00
|
|
|
from django.utils import simplejson as json
|
|
|
|
|
2011-03-09 02:26:20 +03:00
|
|
|
from djangorestframework.compat import RequestFactory
|
2011-04-25 07:50:28 +04:00
|
|
|
|
2011-03-09 02:26:20 +03:00
|
|
|
from pygments_api import views
|
2011-03-20 12:37:38 +03:00
|
|
|
import tempfile, shutil
|
|
|
|
|
2011-04-25 07:50:28 +04:00
|
|
|
|
2011-03-09 02:26:20 +03:00
|
|
|
|
|
|
|
class TestPygmentsExample(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.factory = RequestFactory()
|
|
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
|
|
views.HIGHLIGHTED_CODE_DIR = self.temp_dir
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_get_to_root(self):
|
|
|
|
'''Just do a get on the base url'''
|
|
|
|
request = self.factory.get('/pygments')
|
|
|
|
view = views.PygmentsRoot.as_view()
|
|
|
|
response = view(request)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_snippets_datetime_sorted(self):
|
|
|
|
'''Pygments examples should be datetime sorted'''
|
|
|
|
locations = []
|
2011-04-27 16:29:59 +04:00
|
|
|
for snippet in 'abcdefghij': # String length must not exceed views.MAX_FILES, otherwise test fails
|
2011-03-09 02:26:20 +03:00
|
|
|
form_data = {'code': '%s' % snippet, 'style':'friendly', 'lexer':'python'}
|
|
|
|
request = self.factory.post('/pygments', data=form_data)
|
|
|
|
view = views.PygmentsRoot.as_view()
|
|
|
|
response = view(request)
|
|
|
|
locations.append(response.items()[2][1])
|
2011-04-27 16:29:59 +04:00
|
|
|
import time
|
|
|
|
time.sleep(.1)
|
2011-03-09 02:26:20 +03:00
|
|
|
request = self.factory.get('/pygments')
|
|
|
|
view = views.PygmentsRoot.as_view()
|
|
|
|
response = view(request)
|
2011-03-09 12:39:45 +03:00
|
|
|
response_locations = json.loads(response.content)
|
|
|
|
self.assertEquals(locations, response_locations)
|
2011-03-09 02:26:20 +03:00
|
|
|
|
|
|
|
|
2011-12-29 17:31:12 +04:00
|
|
|
|