summaryrefslogtreecommitdiff
path: root/repoze/bfg/tests/test_zodb.py
blob: d286229530469d4b401e335a776783ebf10c54fd (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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import unittest

class ZODBGetitemPolicyTests(unittest.TestCase):
    def _getTargetClass(self):
        from repoze.bfg.zodb import ZODBGetitemPolicy
        return ZODBGetitemPolicy

    def _makeOne(self, *arg, **kw):
        klass = self._getTargetClass()
        return klass(*arg, **kw)

    def test_class_conforms_to_IPolicy(self):
        from zope.interface.verify import verifyClass
        from repoze.bfg.interfaces import IPolicy
        verifyClass(IPolicy, self._getTargetClass())

    def test_instance_conforms_to_IPolicy(self):
        from zope.interface.verify import verifyObject
        from repoze.bfg.interfaces import IPolicy
        verifyObject(IPolicy, self._makeOne('dbname'))

    def test_call_noconn(self):
        mw = self._makeOne('dbname')
        environ = {}
        self.assertRaises(ValueError, mw, environ)

    def test_call_withconn_attributeerror(self):
        mw = self._makeOne('dbname')
        environ = {'repoze.zodbconn.dbname': DummyConnection(DummyNoGetitem()),
                   'PATH_INFO':''}
        self.assertRaises(AttributeError, mw, environ)

    def test_call_withconn_getitem_emptypath_nosubpath(self):
        mw = self._makeOne('dbname')
        context = DummyContext()
        environ = {'repoze.zodbconn.dbname': DummyConnection(context),
                   'PATH_INFO':''}
        ctx, name, subpath = mw(environ)
        self.assertEqual(context, ctx)
        self.assertEqual(name, '')
        self.assertEqual(subpath, [])

    def test_call_withconn_getitem_withpath_nosubpath(self):
        mw = self._makeOne('dbname')
        context = DummyContext()
        context2 = DummyContext(context)
        environ = {'repoze.zodbconn.dbname': DummyConnection(context2),
                   'PATH_INFO':'/foo/bar'}
        ctx, name, subpath = mw(environ)
        self.assertEqual(context, ctx)
        self.assertEqual(name, 'bar')
        self.assertEqual(subpath, [])

    def test_call_withconn_getitem_withpath_withsubpath(self):
        mw = self._makeOne('dbname')
        context = DummyContext()
        context2 = DummyContext(context)
        environ = {'repoze.zodbconn.dbname': DummyConnection(context2),
                   'PATH_INFO':'/foo/bar/baz/buz'}
        ctx, name, subpath = mw(environ)
        self.assertEqual(context, ctx)
        self.assertEqual(name, 'bar')
        self.assertEqual(subpath, ['baz', 'buz'])

    def test_call_withprefix(self):
        mw = self._makeOne('dbname', ['a', 'b'])
        context = DummyContext()
        context2 = DummyContext(context)
        context3 = DummyContext(context2)
        environ = {'repoze.zodbconn.dbname': DummyConnection(context3),
                   'PATH_INFO':'/foo/bar/baz/buz'}
        ctx, name, subpath = mw(environ)
        self.assertEqual(context, ctx)
        self.assertEqual(name, 'foo')
        self.assertEqual(subpath, ['bar', 'baz', 'buz'])

class DummyNoGetitem:
    pass

class DummyContext:
    def __init__(self, next=None):
        self.next = next
        
    def __getitem__(self, name):
        if self.next is None:
            raise KeyError, name
        return self.next
    
class DummyConnection:
    def __init__(self, result):
        self.result = result
    def open(self):
        return self.result