aboutsummaryrefslogtreecommitdiffstats
path: root/examples/pygments_api/tests.py
blob: 76f1afba810e3ef3f2472c31948f9b1fd8e791e8 (plain)
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
41
42
from django.test import TestCase
from djangorestframework.compat import RequestFactory
from pygments_api import views
import os, tempfile, shutil, time, json

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 = []
        for snippet in 'abcdefghijk':
            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])
        request = self.factory.get('/pygments')
        view = views.PygmentsRoot.as_view()
        response = view(request)
        response_locations = json.loads(response.content)
        self.assertEquals(locations, response_locations)