/usr/share/pyshared/zope/container/tests/test_find.py is in python-zope.container 3.12.0-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | ##############################################################################
#
# 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.
#
##############################################################################
"""Find functionality tests
"""
from unittest import TestCase, main, makeSuite
from zope.container.interfaces import IReadContainer
from zope.container.interfaces import IObjectFindFilter
from zope.container.find import FindAdapter, SimpleIdFindFilter
from zope.container.find import SimpleInterfacesFindFilter
from zope.interface import implements, Interface, directlyProvides
class FakeContainer(object):
implements(IReadContainer)
def __init__(self, id, objects):
self._id = id
self._objects = objects
def keys(self):
return [object._id for object in self._objects]
def values(self):
return self._objects
def items(self):
return [(object._id, object) for object in self._objects]
def __getitem__(self, id):
for object in self._objects:
if object._id == id:
return object
raise KeyError("Could not find %s" % id)
def get(self, id, default=None):
for object in self._objects:
if object._id == id:
return object
return default
def __contains__(self, id):
for object in self._objects:
if object.id == id:
return True
return False
def __len__(self):
return len(self._objects)
class FakeInterfaceFoo(Interface):
"""Test interface Foo"""
class FakeInterfaceBar(Interface):
"""Test interface Bar"""
class FakeInterfaceSpam(Interface):
"""Test interface Spam"""
class TestObjectFindFilter(object):
implements(IObjectFindFilter)
def __init__(self, count):
self._count = count
def matches(self, object):
if IReadContainer.providedBy(object):
return len(object) == self._count
else:
return False
class Test(TestCase):
def test_idFind(self):
alpha = FakeContainer('alpha', [])
delta = FakeContainer('delta', [])
beta = FakeContainer('beta', [delta])
gamma = FakeContainer('gamma', [])
tree = FakeContainer(
'tree',
[alpha, beta, gamma])
find = FindAdapter(tree)
# some simple searches
result = find.find([SimpleIdFindFilter(['beta'])])
self.assertEquals([beta], result)
result = find.find([SimpleIdFindFilter(['gamma'])])
self.assertEquals([gamma], result)
result = find.find([SimpleIdFindFilter(['delta'])])
self.assertEquals([delta], result)
# we should not find the container we search on
result = find.find([SimpleIdFindFilter(['tree'])])
self.assertEquals([], result)
# search for multiple ids
result = find.find([SimpleIdFindFilter(['alpha', 'beta'])])
self.assertEquals([alpha, beta], result)
result = find.find([SimpleIdFindFilter(['beta', 'delta'])])
self.assertEquals([beta, delta], result)
# search without any filters, find everything
result = find.find([])
self.assertEquals([alpha, beta, delta, gamma], result)
# search for something that doesn't exist
result = find.find([SimpleIdFindFilter(['foo'])])
self.assertEquals([], result)
# find for something that has two ids at the same time,
# can't ever be the case
result = find.find([SimpleIdFindFilter(['alpha']),
SimpleIdFindFilter(['beta'])])
self.assertEquals([], result)
def test_objectFind(self):
alpha = FakeContainer('alpha', [])
delta = FakeContainer('delta', [])
beta = FakeContainer('beta', [delta])
gamma = FakeContainer('gamma', [])
tree = FakeContainer(
'tree',
[alpha, beta, gamma])
find = FindAdapter(tree)
result = find.find(object_filters=[TestObjectFindFilter(0)])
self.assertEquals([alpha, delta, gamma], result)
result = find.find(object_filters=[TestObjectFindFilter(1)])
self.assertEquals([beta], result)
result = find.find(object_filters=[TestObjectFindFilter(2)])
self.assertEquals([], result)
def test_combinedFind(self):
alpha = FakeContainer('alpha', [])
delta = FakeContainer('delta', [])
beta = FakeContainer('beta', [delta])
gamma = FakeContainer('gamma', [])
tree = FakeContainer(
'tree',
[alpha, beta, gamma])
find = FindAdapter(tree)
result = find.find(id_filters=[SimpleIdFindFilter(['alpha'])],
object_filters=[TestObjectFindFilter(0)])
self.assertEquals([alpha], result)
result = find.find(id_filters=[SimpleIdFindFilter(['alpha'])],
object_filters=[TestObjectFindFilter(1)])
self.assertEquals([], result)
def test_interfaceFind(self):
alpha = FakeContainer('alpha', [])
directlyProvides(alpha, FakeInterfaceBar)
delta = FakeContainer('delta', [])
directlyProvides(delta, FakeInterfaceFoo)
beta = FakeContainer('beta', [delta])
directlyProvides(beta, FakeInterfaceSpam)
gamma = FakeContainer('gamma', [])
tree = FakeContainer(
'tree',
[alpha, beta, gamma])
find = FindAdapter(tree)
result = find.find(object_filters=[
SimpleInterfacesFindFilter(FakeInterfaceFoo, FakeInterfaceSpam)])
self.assertEqual([beta, delta], result)
def test_suite():
return makeSuite(Test)
if __name__=='__main__':
main(defaultTest='test_suite')
|