Back to index

plone3  3.1.7
test_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 Testing import ZopeTestCase
00029 
00030 from Products.Archetypes.tests.attestcase import ATTestCase
00031 from Products.Archetypes.tests.atsitetestcase import ATSiteTestCase
00032 from Products.Archetypes.atapi import *
00033 from Products.Archetypes.tests.test_classgen import Dummy
00034 from Products.Archetypes.tests.test_classgen import gen_dummy
00035 
00036 from DateTime import DateTime
00037 
00038 
00039 class ChangeStorageTest(ATSiteTestCase):
00040 
00041     def afterSetUp(self):
00042         gen_dummy()
00043         self._dummy = dummy = Dummy(oid='dummy')
00044         self._dummy.initializeArchetype()
00045         self._old_storages = {}
00046 
00047     def test_changestorage(self):
00048         dummy = self._dummy
00049         dummy.setAtextfield('sometext', mimetype="text/plain")
00050         dummy.setAdatefield('2003-01-01')
00051         dummy.setAlinesfield(['bla','bla','bla'])
00052         dummy.setAnobjectfield('someothertext')
00053 
00054         out = ('bla','bla','bla')
00055 
00056         self.failUnlessEqual(str(dummy.getAtextfield()), 'sometext')
00057         self.failUnlessEqual(dummy.getAdatefield(), DateTime('2003-01-01'))
00058         self.failUnlessEqual(dummy.getAlinesfield(), out)
00059         self.failUnlessEqual(dummy.getAnobjectfield(), 'someothertext')
00060 
00061         for field in dummy.schema.fields():
00062             if field.getName() in ['atextfield', 'adatefield', 'alinesfield', 'anobjectfield']:
00063                 self._old_storages[field.getName()] = field.getStorage()
00064                 field.setStorage(dummy, AttributeStorage())
00065                 self.failUnlessEqual(field.getStorage().getName(), 'AttributeStorage')
00066                 field.setStorage(dummy, MetadataStorage())
00067                 self.failUnlessEqual(field.getStorage().getName(), 'MetadataStorage')
00068 
00069         self.failUnlessEqual(str(dummy.getAtextfield()), 'sometext')
00070         self.failUnlessEqual(dummy.getAdatefield(), DateTime('2003-01-01'))
00071         self.failUnlessEqual(dummy.getAlinesfield(), out)
00072         self.failUnlessEqual(dummy.getAnobjectfield(), 'someothertext')
00073 
00074     def test_unset(self):
00075         dummy = self._dummy
00076         dummy.setAtextfield('sometext')
00077         field = dummy.getField('atextfield')
00078         field.setStorage(dummy, AttributeStorage())
00079         self.failUnless(hasattr(dummy, 'atextfield'))
00080         field.setStorage(dummy, MetadataStorage())
00081         self.failIf(hasattr(dummy, 'atextfield'))
00082         self.failUnless(dummy._md.has_key('atextfield'))
00083         field.setStorage(dummy, AttributeStorage())
00084         self.failIf(dummy._md.has_key('atextfield'))
00085         self.failUnless(hasattr(dummy, 'atextfield'))
00086 
00087 
00088 class MetadataStorageTest(ATTestCase):
00089 
00090     def afterSetUp(self):
00091         gen_dummy()
00092         self._dummy = dummy = Dummy(oid='dummy')
00093         self._dummy.initializeArchetype()
00094         for field in dummy.schema.fields():
00095             if field.getName() in ['atextfield', 'adatefield', 'alinesfield', 'anobjectfield']:
00096                 field.setStorage(dummy, MetadataStorage())
00097 
00098 
00099 class AttributeStorageTest(ATTestCase):
00100 
00101     def afterSetUp(self):
00102         gen_dummy()
00103         self._dummy = dummy = Dummy(oid='dummy')
00104         self._dummy.initializeArchetype()
00105         for field in dummy.schema.fields():
00106             if field.getName() in ['atextfield', 'adatefield', 'alinesfield', 'anobjectfield']:
00107                 field.setStorage(dummy, AttributeStorage())
00108 
00109 
00110 def test_suite():
00111     from unittest import TestSuite, makeSuite
00112     suite = TestSuite()
00113     suite.addTest(makeSuite(ChangeStorageTest))
00114     suite.addTest(makeSuite(MetadataStorageTest))
00115     suite.addTest(makeSuite(AttributeStorageTest))
00116     return suite