Back to index

plone3  3.1.7
test_aggregated_storage.py
Go to the documentation of this file.
00001 ################################################################################
00002 #
00003 # Copyright (c) 2002-2005, Benjamin Saller <bcsaller@ideasuite.com>, and
00004 #                              the respective authors. All rights reserved.
00005 # For a list of Archetypes contributors see docs/CREDITS.txt.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions are met:
00009 #
00010 # * Redistributions of source code must retain the above copyright notice, this
00011 #   list of conditions and the following disclaimer.
00012 # * Redistributions in binary form must reproduce the above copyright notice,
00013 #   this list of conditions and the following disclaimer in the documentation
00014 #   and/or other materials provided with the distribution.
00015 # * Neither the name of the author nor the names of its contributors may be used
00016 #   to endorse or promote products derived from this software without specific
00017 #   prior written permission.
00018 #
00019 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00020 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00022 # FOR A PARTICULAR PURPOSE.
00023 #
00024 ################################################################################
00025 """
00026 """
00027 
00028 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00029 from Products.Archetypes.tests.utils import mkDummyInContext
00030 from Products.Archetypes.atapi import AggregatedStorage
00031 from Products.Archetypes.atapi import Schema
00032 from Products.Archetypes.atapi import StringField
00033 from Products.Archetypes.atapi import BaseContent
00034 from Products.Archetypes.atapi import registerType
00035 
00036 
00037 class Dummy(BaseContent):
00038 
00039     def __init__(self, oid, **kwargs):
00040         BaseContent.__init__(self, oid, **kwargs)
00041         self.firstname = ''
00042         self.lastname = ''
00043 
00044     def get_name(self, name, instance, **kwargs):
00045         """ aggregator """
00046         return {'whole_name' : instance.firstname + " " + instance.lastname }
00047 
00048     def set_name(self, name, instance, value, **kwargs):
00049         """ disaggregator """
00050         try:
00051             firstname, lastname = value.split(' ')
00052         except:
00053             firstname = lastname = ''
00054         setattr(instance, 'firstname', firstname)
00055         setattr(instance, 'lastname', lastname)
00056 
00057 registerType(Dummy, 'Archetypes')
00058 
00059 
00060 class AggregatedStorageTestsNoCache(ATSiteTestCase):
00061 
00062     caching = 0
00063 
00064     def afterSetUp(self):
00065         self._storage = AggregatedStorage(caching=self.caching)
00066         self._storage.registerAggregator('whole_name', 'get_name')
00067         self._storage.registerDisaggregator('whole_name', 'set_name')
00068 
00069         schema = Schema( (StringField('whole_name', storage=self._storage),
00070                          ))
00071 
00072         # to enable overrideDiscussionFor
00073         self.setRoles(['Manager'])        
00074 
00075         self._instance = mkDummyInContext(klass=Dummy, oid='dummy',
00076                                           context=self.portal, schema=schema)
00077 
00078     def test_basetest(self):
00079         field = self._instance.Schema()['whole_name']
00080 
00081         self.assertEqual(field.get(self._instance).strip(), '')
00082         field.set(self._instance, 'Donald Duck')
00083         self.assertEqual(self._instance.firstname, 'Donald')
00084         self.assertEqual(self._instance.lastname, 'Duck')
00085         self.assertEqual(field.get(self._instance).strip(), 'Donald Duck')
00086 
00087         self._instance.firstname = 'Daniel'
00088         self._instance.lastname = 'Dosentrieb'
00089         self.assertEqual(field.get(self._instance).strip(), 'Daniel Dosentrieb')
00090 
00091         field.set(self._instance, 'Bingo Gringo')
00092         self.assertEqual(self._instance.firstname, 'Bingo')
00093         self.assertEqual(self._instance.lastname, 'Gringo')
00094 
00095 
00096 class AggregatedStorageTestsWithCache(AggregatedStorageTestsNoCache):
00097 
00098     caching = 1
00099 
00100 
00101 def test_suite():
00102     from unittest import TestSuite, makeSuite
00103     suite = TestSuite()
00104     suite.addTest(makeSuite(AggregatedStorageTestsNoCache))
00105     suite.addTest(makeSuite(AggregatedStorageTestsWithCache))
00106     return suite