zerok / django-zsutils

Some Django utilities I share between some of my sites (like Pagination, OOPViews ...)

zerok (author)
Mon Oct 27 14:39:55 -0700 2008
commit  41f9a879df30aa2935d53d3a02f3d807ffbeb8c2
tree    9b4749772862be79e849dbbda9dba401ce03131f
parent  13e561a513cab1cd1224de26735f02f728947a6d
django-zsutils / tests / test_ctn.py
100644 71 lines (59 sloc) 2.373 kb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Test module for oopviews.ctn related stuff
"""
 
from django.http import HttpResponse
 
import sys
from django_zsutils.utils.oopviews import ctn
from . import utils
 
class DummyView(ctn.AbstractCTNView):
    ctn_accept_binding = {
        'text/plain': (0.9, 'text_plain'),
        'text/html': 'text_html',
        'text/*': 'text_default',
        '*/*': 'default',
    }
 
    def default(self, request, *args, **kwargs):
        return HttpResponse('default')
    def text_plain(self, request, *args, **kwargs):
        return HttpResponse('text_plain')
    def text_html(self, request, *args, **kwargs):
        return HttpResponse('text_html')
    def text_default(self, request, *args, **kwargs):
        return HttpResponse('text_default')
 
def testAcceptOrdering():
    request = utils.RequestFactory().get('/',
        HTTP_ACCEPT='text/plain, text/html;q=0.5, text/*')
    vo = DummyView(request)
    priorities = vo._ctn_build_request_priorities(request)
    assert priorities == [('text/plain', 1), ('text/html', 0.5), ('text/*', 1)]
 
def testInvalidPriority():
    request = utils.RequestFactory().get('/',
        HTTP_ACCEPT='text/plain, text/html;q=2, text/*')
    vo = DummyView(request)
    priorities = vo._ctn_build_request_priorities(request)
    assert priorities == [('text/plain', 1), ('text/*', 1)]
 
def testFallback():
    request = utils.RequestFactory().get('/')
    vo = DummyView(request)
    priorities = vo._ctn_build_request_priorities(request)
    assert priorities == [('*/*', 1),]
 
def testFallbackInvalid():
    request = utils.RequestFactory().get('/',
            HTTP_ACCEPT = 'text/plain;q=2')
    vo = DummyView(request)
    priorities = vo._ctn_build_request_priorities(request)
    assert priorities == [('*/*', 1),]
 
def testHandlerPriorities():
    request = utils.RequestFactory().get('/')
    vo = DummyView(request)
    priorities = vo._ctn_build_provides_priorities()
    assert [x[0] for x in priorities] == ['text/html', 'text/plain', 'text/*', '*/*']
 
def testHandlerLookupFamilyPresent():
    request = utils.RequestFactory().get('/',
            HTTP_ACCEPT='text/somethingelse, */*')
    vo = DummyView(request)
    assert vo(request).content == 'text_default'
 
def testHandlerLookupDefault():
    request = utils.RequestFactory().get('/')
    vo = DummyView(request)
    assert vo(request).content == 'default'