/usr/share/pyshared/zope/schema/tests/test_objectfield.py is in python-zope.schema 3.7.1-2.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | from zope.schema import List
##############################################################################
#
# 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.
#
##############################################################################
"""This set of tests exercises Object fields.
"""
from unittest import TestSuite, main, makeSuite
import zope.event
from zope.interface import Attribute, Interface, implements
from zope.schema import Object, TextLine
from zope.schema.fieldproperty import FieldProperty
from zope.schema.interfaces import ValidationError
from zope.schema.interfaces import RequiredMissing, WrongContainedType
from zope.schema.interfaces import WrongType, SchemaNotFullyImplemented
from zope.schema.tests.test_field import FieldTestBase
from zope.schema.interfaces import IBeforeObjectAssignedEvent
from zope.testing.cleanup import CleanUp
from zope.schema._messageid import _
class ITestSchema(Interface):
"""A test schema"""
foo = TextLine(
title=_(u"Foo"),
description=_(u"Foo description"),
default=u"",
required=True)
bar = TextLine(
title=_(u"Bar"),
description=_(u"Bar description"),
default=u"",
required=False)
attribute = Attribute("Test attribute, an attribute can't be validated.")
class TestClass(object):
implements(ITestSchema)
_foo = u''
_bar = u''
_attribute = u''
def getfoo(self):
return self._foo
def setfoo(self, value):
self._foo = value
foo = property(getfoo, setfoo, None, u'foo')
def getbar(self):
return self._bar
def setbar(self, value):
self._bar = value
bar = property(getbar, setbar, None, u'foo')
def getattribute(self):
return self._attribute
def setattribute(self, value):
self._attribute = value
attribute = property(getattribute, setattribute, None, u'attribute')
class FieldPropertyTestClass(object):
implements(ITestSchema)
foo = FieldProperty(ITestSchema['foo'])
bar = FieldProperty(ITestSchema['bar'])
attribute = FieldProperty(ITestSchema['attribute'])
class NotFullyImplementedTestClass(object):
implements(ITestSchema)
foo = FieldProperty(ITestSchema['foo'])
# bar = FieldProperty(ITestSchema['bar']): bar is not implemented
# attribute
class ISchemaWithObjectFieldAsInterface(Interface):
obj = Object(
schema=Interface,
title=_(u"Object"),
description=_(u"object description"),
required=False)
class ClassWithObjectFieldAsInterface(object):
implements(ISchemaWithObjectFieldAsInterface)
_obj = None
def getobj(self):
return self._obj
def setobj(self, value):
self._obj = value
obj = property(getobj, setobj, None, u'obj')
class IUnit(Interface):
"""A schema that participate to a cycle"""
boss = Object(
schema=Interface,
title=_(u"Boss"),
description=_(u"Boss description"),
required=False,
)
members = List(
value_type=Object(schema=Interface),
title=_(u"Member List"),
description=_(u"Member list description"),
required=False,
)
class IPerson(Interface):
"""A schema that participate to a cycle"""
unit = Object(
schema=IUnit,
title=_(u"Unit"),
description=_(u"Unit description"),
required=False,
)
IUnit['boss'].schema = IPerson
IUnit['members'].value_type.schema = IPerson
class Unit(object):
implements(IUnit)
def __init__(self, person, person_list):
self.boss = person
self.members = person_list
class Person(object):
implements(IPerson)
def __init__(self, unit):
self.unit = unit
class ObjectTest(CleanUp, FieldTestBase):
"""Test the Object Field."""
def getErrors(self, f, *args, **kw):
try:
f(*args, **kw)
except WrongContainedType, e:
try:
return e[0]
except:
return []
self.fail('Expected WrongContainedType Error')
def makeTestObject(self, **kw):
kw['schema'] = kw.get('schema', Interface)
return Object(**kw)
_Field_Factory = makeTestObject
def makeTestData(self):
return TestClass()
def makeFieldPropertyTestClass(self):
return FieldPropertyTestClass()
def makeNotFullyImplementedTestData(self):
return NotFullyImplementedTestClass()
def invalidSchemas(self):
return ['foo', 1, 0, {}, [], None]
def validSchemas(self):
return [Interface, ITestSchema]
def test_init(self):
for schema in self.validSchemas():
Object(schema=schema)
for schema in self.invalidSchemas():
self.assertRaises(ValidationError, Object, schema=schema)
self.assertRaises(WrongType, Object, schema=schema)
def testValidate(self):
# this test of the base class is not applicable
pass
def testValidateRequired(self):
# this test of the base class is not applicable
pass
def test_validate_required(self):
field = self._Field_Factory(
title=u'Required field', description=u'',
readonly=False, required=True)
self.assertRaises(RequiredMissing, field.validate, None)
def test_validate_TestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeTestData()
field.validate(data)
field = self.makeTestObject(schema=ITestSchema)
field.validate(data)
data.foo = None
self.assertRaises(ValidationError, field.validate, data)
self.assertRaises(WrongContainedType, field.validate, data)
errors = self.getErrors(field.validate, data)
self.assertEquals(errors[0], RequiredMissing('foo'))
def test_validate_FieldPropertyTestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeFieldPropertyTestClass()
field.validate(data)
field = self.makeTestObject(schema=ITestSchema)
field.validate(data)
self.assertRaises(ValidationError, setattr, data, 'foo', None)
self.assertRaises(RequiredMissing, setattr, data, 'foo', None)
def test_validate_NotFullyImplementedTestData(self):
field = self.makeTestObject(schema=ITestSchema, required=False)
data = self.makeNotFullyImplementedTestData()
self.assertRaises(ValidationError, field.validate, data)
self.assertRaises(WrongContainedType, field.validate, data)
errors = self.getErrors(field.validate, data)
self.assert_(isinstance(errors[0], SchemaNotFullyImplemented))
def test_validate_with_non_object_value(self):
field = self.makeTestObject(
schema=ISchemaWithObjectFieldAsInterface,
required=False)
instance = ClassWithObjectFieldAsInterface()
instance.obj = (1, 1)
field.validate(instance)
def test_beforeAssignEvent(self):
field = self.makeTestObject(schema=ITestSchema, required=False,
__name__='object_field')
data = self.makeTestData()
events = []
def register_event(event):
events.append(event)
zope.event.subscribers.append(register_event)
class Dummy(object):
pass
context = Dummy()
field.set(context, data)
self.assertEquals(1, len(events))
event = events[0]
self.failUnless(IBeforeObjectAssignedEvent.providedBy(event))
self.assertEquals(data, event.object)
self.assertEquals('object_field', event.name)
self.assertEquals(context, event.context)
# cycles
def test_with_cycles_validate(self):
field = self.makeTestObject(schema=IUnit)
person1 = Person(None)
person2 = Person(None)
unit = Unit(person1, [person1, person2])
person1.unit = unit
person2.unit = unit
field.validate(unit)
def test_with_cycles_object_not_valid(self):
field = self.makeTestObject(schema=IUnit)
data = self.makeTestData()
person1 = Person(None)
person2 = Person(None)
person3 = Person(data)
unit = Unit(person3, [person1, person2])
person1.unit = unit
person2.unit = unit
self.assertRaises(WrongContainedType, field.validate, unit)
def test_with_cycles_collection_not_valid(self):
field = self.makeTestObject(schema=IUnit)
data = self.makeTestData()
person1 = Person(None)
person2 = Person(None)
person3 = Person(data)
unit = Unit(person1, [person2, person3])
person1.unit = unit
person2.unit = unit
self.assertRaises(WrongContainedType, field.validate, unit)
def test_suite():
suite = TestSuite()
suite.addTest(makeSuite(ObjectTest))
return suite
if __name__ == '__main__':
main(defaultTest='test_suite')
|