Changeset 6266 for main/waeup.sirp/trunk/src/waeup/sirp/utils
- Timestamp:
- 2 Jun 2011, 10:51:24 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py
r6265 r6266 28 28 import tempfile 29 29 import unittest 30 from zope import schema 30 31 from zope.app.testing.functional import FunctionalTestCase 32 from zope.component import provideUtility 33 from zope.component.factory import Factory 31 34 from zope.component.hooks import setSite, clearSite 35 from zope.component.interfaces import IFactory 32 36 from zope.formlib import form 37 from zope.interface import ( 38 Interface, implements, invariant, Invalid, implementedBy) 33 39 from zope.interface.verify import verifyClass, verifyObject 34 40 … … 36 42 from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module 37 43 from waeup.sirp.utils.converters import IObjectConverter 38 39 from zope import schema40 from zope.interface import Interface, implements, invariant, Invalid41 44 from waeup.sirp.utils.helpers import attrs_to_fields 42 45 43 46 class IContact(Interface): 47 """Sample interface for sample content type used here in tests. 48 """ 44 49 name = schema.TextLine( 45 50 title = u'Name', … … 70 75 71 76 class Contact(object): 77 """Sample content type. 78 """ 72 79 implements(IContact) 73 name = None74 age = None75 80 Contact = attrs_to_fields(Contact) 76 81 77 82 form_fields_select = form.Fields(IContact).select('name', 'vip') 78 83 form_fields_omit = form.Fields(IContact).omit('name', 'vip') 84 85 class ContactFactory(object): 86 """A factory for faculty containers. 87 """ 88 implements(IContact) 89 90 def __call__(self, *args, **kw): 91 return Faculty() 92 93 def getInterfaces(self): 94 return implementedBy(Faculty) 79 95 80 96 class ConverterTests(FunctionalTestCase): … … 95 111 96 112 self.workdir = tempfile.mkdtemp() 113 114 # Create a factory for contacts and register it as global utility 115 factory = Factory(Contact) 116 provideUtility(factory, IFactory, 'contact') 97 117 return 98 118 … … 220 240 self.assertEqual(contact.age, 99) 221 241 self.assertEqual(contact.vip, False) 242 return 243 244 def test_factory(self): 245 # We can use factories to create a new object 246 # 247 # This way we turn a dict of strings and some string denoting 248 # the kind of object to be created (factory name) into a real object. 249 converter = IObjectConverter(IContact) # a converter to IContact 250 # pass string ``contact`` instead of a real object 251 err, inv_err, contact = converter.applyRowData( 252 dict(name='Gabi'), 'contact') 253 # we get an object... 254 assert isinstance(contact, Contact) 255 # ...with the values from the dict set. 256 self.assertEqual(contact.name, 'Gabi') 222 257 return 223 258
Note: See TracChangeset for help on using the changeset viewer.