This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
143 lines (109 loc) · 4.58 KB
/
tests.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Unit tests for Dependable class.
"""
import unittest
from zope.annotation.attribute import AttributeAnnotations
from zope.location.interfaces import ILocationInfo
from zope.interface import implementer, verify, directlyProvides
from zope.lifecycleevent import ObjectRemovedEvent
from zope.location.interfaces import ILocationInfo
from zope.testing.cleanup import CleanUp
import zope.app.dependable
from zope.app.dependable.dependency import CheckDependency
from zope.app.dependable.interfaces import IDependable, DependencyError
class C(object):
pass
@implementer(IDependable, ILocationInfo)
class DummyObject(object):
def dependents(self):
return ['dependency1', 'dependency2']
def getPath(self):
return '/dummy-object'
class TestDependable(unittest.TestCase):
def factory(self, obj=None):
from zope.app.dependable import Dependable
return Dependable(obj if obj is not None else AttributeAnnotations(C()))
def testVerifyInterface(self):
object = self.factory()
verify.verifyObject(IDependable, object)
def testBasic(self):
dependable = self.factory()
self.failIf(dependable.dependents())
dependable.addDependent('/a/b')
dependable.addDependent('/c/d')
dependable.addDependent('/c/e')
dependable.addDependent('/c/d')
dependents = list(dependable.dependents())
dependents.sort()
self.assertEqual(dependents, ['/a/b', '/c/d', '/c/e'])
dependable.removeDependent('/c/d')
dependents = list(dependable.dependents())
dependents.sort()
self.assertEqual(dependents, ['/a/b', '/c/e'])
dependable.removeDependent('/c/d')
dependents = list(dependable.dependents())
dependents.sort()
self.assertEqual(dependents, ['/a/b', '/c/e'])
def testRelativeAbsolute(self):
obj = self.factory()
# Hack the object to have a parent path
obj.pp = "/a/"
obj.pplen = len(obj.pp)
obj.addDependent("foo")
self.assertEqual(obj.dependents(), ("/a/foo",))
obj.removeDependent("/a/foo")
self.assertEqual(obj.dependents(), ())
obj.addDependent("/a/bar")
self.assertEqual(obj.dependents(), ("/a/bar",))
obj.removeDependent("bar")
self.assertEqual(obj.dependents(), ())
def testCheckDependency(self):
obj = DummyObject()
parent = object()
event = ObjectRemovedEvent(obj, parent, 'oldName')
self.assertRaises(DependencyError, CheckDependency, event)
def testWithParent(self):
grandparent = C()
grandparent.__name__ = 'root'
parent = C()
parent.__name__ = 'parent'
parent.__parent__ = grandparent
obj = AttributeAnnotations(C())
obj.__parent__ = parent
obj.__name__ = 'obj'
from zope.traversing.api import getPath
# If we can't get the parent path, it's just /
dependable = self.factory(obj)
self.assertEqual('/', dependable.pp)
# If we can, it's whatever it reported, always with a
# trailing /
directlyProvides(parent, ILocationInfo)
parent.getPath = lambda: '/root/parent'
dependable = self.factory(obj)
self.assertEqual('/root/parent/', dependable.pp)
parent.getPath = lambda: '/root/parent/'
dependable = self.factory(obj)
self.assertEqual('/root/parent/', dependable.pp)
dependable.addDependent('/root/parent/sibling/nephew')
dependents = list(dependable.dependents())
self.assertEqual(dependents, ['/root/parent/sibling/nephew'])
class TestConfiguration(CleanUp, unittest.TestCase):
def test_configuration(self):
from zope.configuration import xmlconfig
xmlconfig.file('configure.zcml', package=zope.app.dependable)
def test_suite():
return unittest.defaultTestLoader.loadTestsFromName(__name__)
if __name__=='__main__': # pragma: no cover
unittest.main(defaultTest='test_suite')