Changeset 6266 for main


Ignore:
Timestamp:
2 Jun 2011, 10:51:24 (14 years ago)
Author:
uli
Message:

Show that the converter can create new instances when given a factory
name, this way really turning a set of strings into new objects as we
expect it from an importer in 'create' mode.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/tests/test_converters.py

    r6265 r6266  
    2828import tempfile
    2929import unittest
     30from zope import schema
    3031from zope.app.testing.functional import FunctionalTestCase
     32from zope.component import provideUtility
     33from zope.component.factory import Factory
    3134from zope.component.hooks import setSite, clearSite
     35from zope.component.interfaces import IFactory
    3236from zope.formlib import form
     37from zope.interface import (
     38    Interface, implements, invariant, Invalid, implementedBy)
    3339from zope.interface.verify import verifyClass, verifyObject
    3440
     
    3642from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module
    3743from waeup.sirp.utils.converters import IObjectConverter
    38 
    39 from zope import schema
    40 from zope.interface import Interface, implements, invariant, Invalid
    4144from waeup.sirp.utils.helpers import attrs_to_fields
    4245
    4346class IContact(Interface):
     47    """Sample interface for sample content type used here in tests.
     48    """
    4449    name = schema.TextLine(
    4550        title = u'Name',
     
    7075
    7176class Contact(object):
     77    """Sample content type.
     78    """
    7279    implements(IContact)
    73     name = None
    74     age = None
    7580Contact = attrs_to_fields(Contact)
    7681
    7782form_fields_select = form.Fields(IContact).select('name', 'vip')
    7883form_fields_omit = form.Fields(IContact).omit('name', 'vip')
     84
     85class 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)
    7995
    8096class ConverterTests(FunctionalTestCase):
     
    95111
    96112        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')
    97117        return
    98118
     
    220240        self.assertEqual(contact.age, 99)
    221241        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')
    222257        return
    223258
Note: See TracChangeset for help on using the changeset viewer.