Back to index

plone3  3.1.7
test_annotation.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 Testing import ZopeTestCase
00029 
00030 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00031 from Products.Archetypes.atapi import *
00032 from Products.Archetypes.tests.test_classgen import Dummy
00033 from Products.Archetypes.tests.test_classgen import gen_class
00034 from Products.Archetypes.tests.test_classgen import gen_dummy
00035 from Acquisition import aq_base
00036 
00037 class AnnDummy(Dummy): pass
00038 
00039 annschema = BaseSchema + Schema((
00040      StringField('string',
00041          default = u'stringdefault',
00042          storage = AnnotationStorage(),
00043          ),
00044      StringField('meta',
00045          default = 'metadefault',
00046          storage = MetadataAnnotationStorage(),
00047          ),
00048     ))
00049 
00050 
00051 def gen_anndummy():
00052     gen_class(AnnDummy, annschema)
00053 
00054 class AnnotationTest(ATSiteTestCase):
00055 
00056     def afterSetUp(self):
00057         gen_dummy()
00058         dummy = Dummy(oid='dummy')
00059         self.folder._setObject('dummy', dummy)
00060         self.folder.dummy.initializeArchetype()
00061         self.dummy = self.folder.dummy
00062         self.ann = getAnnotation(self.dummy)
00063         
00064     def test_empty(self):
00065         ann = self.ann
00066         self.failUnlessEqual(bool(ann), False)
00067         self.failUnlessEqual(getattr(self.dummy, '__annotations__', None), None)
00068         
00069     def test_create(self):
00070         ann = self.ann
00071         ann['test'] = 'test'
00072         self.failUnlessEqual(bool(ann), True)
00073         self.failIfEqual(getattr(self.dummy, '__annotations__', None), None)
00074     
00075     def test_set(self):
00076         ann = self.ann
00077         ann['test'] = 'test1'
00078         self.failUnlessEqual(ann['test'], 'test1')
00079         ann.set('test', 'test2')
00080         self.failUnlessEqual(ann['test'], 'test2')
00081         ann.setSubkey('test', 'test3', subkey='testsub')
00082         self.failUnlessEqual(ann['test-testsub'], 'test3')
00083         self.failUnlessEqual(ann.getSubkey('test', subkey='testsub'), 'test3')
00084         
00085     def test_get(self):
00086         ann = self.ann
00087         ann['test'] = 'test1'
00088         self.failUnlessEqual(ann['test'], 'test1')
00089         self.failUnlessEqual(ann.get('test'), 'test1')
00090         self.failUnless(ann.has_key('test'))
00091         self.failUnlessEqual(ann.get('none', default='default'), 'default')
00092         
00093     def test_del(self):
00094         ann = self.ann
00095         ann['test'] = 'test1'
00096         del ann['test']
00097         self.failIf(ann.has_key('test'))
00098         ann.setSubkey('test', 'test3', subkey='testsub')
00099         ann.delSubkey('test', subkey='testsub')
00100         self.failIf(ann.has_key('test-testsub'))
00101         self.failIf(ann.hasSubkey('test', subkey='testsub'))
00102 
00103 
00104 class MetadataAnnotationStorageTest(ATSiteTestCase):
00105 
00106     def afterSetUp(self):
00107         gen_anndummy()
00108         dummy = AnnDummy(oid='dummy')
00109         self.folder._setObject('dummy', dummy)
00110         self.dummy = self.folder.dummy
00111         self.dummy.initializeArchetype()
00112         self.ann = getAnnotation(self.dummy)
00113         
00114     def test_setup(self):
00115         dummy = self.dummy
00116         field = dummy.getField('meta')
00117         self.failUnless(isinstance(field.storage, MetadataAnnotationStorage))
00118         self.failUnless(self.ann.hasSubkey(AT_MD_STORAGE, 'meta'))
00119         self.failUnlessEqual(self.ann.getSubkey(AT_MD_STORAGE, subkey='meta'), 'metadefault')
00120 
00121     def test_gestset(self):
00122         dummy = self.dummy
00123         ann = self.ann
00124         dummy.setMeta('egg')
00125         self.failUnlessEqual(dummy.getMeta(), 'egg')
00126         self.failUnlessEqual(ann.getSubkey(AT_MD_STORAGE, subkey='meta'), 'egg')
00127 
00128 class AnnotationStorageTest(ATSiteTestCase):
00129     
00130     def afterSetUp(self):
00131         gen_anndummy()
00132         dummy = AnnDummy(oid='dummy')
00133         self.folder._setObject('dummy', dummy)
00134         self.dummy = self.folder.dummy
00135         self.dummy.initializeArchetype()
00136         self.ann = getAnnotation(self.dummy)
00137         
00138     def test_setup(self):
00139         dummy = self.dummy
00140         field = dummy.getField('string')
00141         self.failUnless(isinstance(field.storage, AnnotationStorage))
00142         self.failUnless(self.ann.hasSubkey(AT_ANN_STORAGE, 'string'))
00143         self.failUnlessEqual(self.ann.getSubkey(AT_ANN_STORAGE, subkey='string'), 'stringdefault')
00144         
00145     def test_gestset(self):
00146         dummy = self.dummy
00147         ann = self.ann
00148         dummy.setString('egg')
00149         self.failUnlessEqual(dummy.getString(), 'egg')
00150         self.failUnlessEqual(ann.getSubkey(AT_ANN_STORAGE, subkey='string'), 'egg')
00151         
00152     def test_storageGetSetDel(self):
00153         dummy = self.dummy
00154         ann = self.ann
00155         field = dummy.getField('string')
00156         storage = field.storage
00157         
00158         dummy.setString('egg')
00159         self.failUnlessEqual(storage.get('string', dummy), 'egg')
00160         
00161         storage.set('string', dummy, 'spam')
00162         self.failUnlessEqual(storage.get('string', dummy), 'spam')
00163         
00164         storage.unset('string', dummy)
00165         self.failIf(ann.hasSubkey(AT_ANN_STORAGE, 'string'))
00166 
00167     def test_storageNonExisting(self):
00168         dummy = self.dummy
00169         ann = self.ann
00170         storage = AnnotationStorage()
00171         
00172         self.failUnlessRaises(AttributeError, storage.get, 'nonexisting', dummy)
00173         
00174         # del shouldn't raise anything
00175         storage.unset('nonexisting', dummy)
00176         
00177         # set should create an entry
00178         storage.set('nonexisting', dummy,  value='bar')
00179         self.failUnlessEqual(storage.get('nonexisting', dummy), 'bar')
00180         
00181     def test_migration(self):
00182         dummy = self.dummy
00183         ann = self.ann
00184         field = dummy.getField('string')
00185         storage = field.storage
00186         
00187         # migration mode on
00188         self.failUnlessEqual(storage._migrate, False)
00189         storage._migrate = True
00190         
00191         # test clean up
00192         dummy.string = 'spam'
00193         self.failUnless(hasattr(aq_base(dummy), 'string'))
00194         
00195         self.failIfEqual(storage.get('string', dummy), 'spam')
00196         storage.set('string', dummy, 'bar')
00197         self.failIf(hasattr(aq_base(dummy), 'string'))
00198         self.failUnlessEqual(storage.get('string', dummy), 'bar')
00199         
00200         # test migration
00201         ann.delSubkey(AT_ANN_STORAGE, subkey='string')
00202         dummy.string = 'spam'
00203         self.failUnlessEqual(storage.get('string', dummy), 'spam')
00204         self.failIf(hasattr(aq_base(dummy), 'string'))
00205                
00206 
00207 def test_suite():
00208     from unittest import TestSuite, makeSuite
00209     suite = TestSuite()
00210     suite.addTest(makeSuite(AnnotationTest))
00211     suite.addTest(makeSuite(MetadataAnnotationStorageTest))
00212     suite.addTest(makeSuite(AnnotationStorageTest))
00213     return suite