Back to index

plone3  3.1.7
test_resourcetypemapper.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
00004 #
00005 # This software is distributed under the terms of the Kupu
00006 # License. See LICENSE.txt for license text. For a list of Kupu
00007 # Contributors see CREDITS.txt.
00008 #
00009 ##############################################################################
00010 """Tests for the library tool
00011 
00012 $Id: test_resourcetypemapper.py 41232 2007-03-24 13:02:55Z hannosch $
00013 """
00014 
00015 import os, sys
00016 if __name__ == '__main__':
00017     execfile(os.path.join(sys.path[0], 'framework.py'))
00018 
00019 import Acquisition
00020 from Testing.ZopeTestCase import ZopeTestCase
00021 from Products.kupu.plone.librarytool import KupuLibraryTool
00022 
00023 class FakeType:
00024     def __init__(self, id, **kw):
00025         self.id = id
00026         for k in kw:
00027             setattr(self, k, kw[k])
00028 
00029 class FakeTypeTool:
00030     def listTypeInfo(self, container=None):
00031         return [ FakeType(f)
00032             for f in "Foo|Bar|Monkey|Ape|Nothing In Here Anymore|Bad Monkey|Bad Ape".split('|') ]
00033 
00034 class FakePortal(Acquisition.Implicit):
00035     absolute_url = lambda(self): None
00036     portal_types = FakeTypeTool()
00037 
00038 
00039 class TestIResourceTypeMapper(ZopeTestCase):
00040     """Test the implementation of IResourceMapper in KupuLibraryTool"""
00041 
00042     def afterSetUp(self):
00043         self.portal = FakePortal()
00044         self.type_map = self.prepare()
00045 
00046     def prepare(self):
00047         type_map = KupuLibraryTool()
00048         type_map = type_map.__of__(self.portal)
00049         type_map.addResourceType("foobar", ("Foo", "", "Bar"))
00050         type_map.addResourceType("bonobo", ("Monkey", " Ape\n"))
00051         return type_map
00052 
00053     def test_get_portal_types(self):
00054         type_map = self.type_map
00055         self.assertEqual(type_map.getPortalTypesForResourceType("foobar"),
00056                          ("Foo", "Bar"))
00057         self.assertEqual(type_map.getPortalTypesForResourceType("bonobo"),
00058                          ("Monkey", "Ape"))
00059 
00060     def test_update(self):
00061         type_map = self.type_map
00062         type_info = [
00063             dict(old_type='foobar', resource_type='foobar',
00064                  portal_types=("Nothing In Here Anymore",)),
00065             dict(old_type='bonobo', resource_type='chimpanse',
00066                  portal_types=("Bad Monkey", "Bad Ape")),
00067             ]
00068         type_map.updateResourceTypes(type_info)
00069         self.assertEqual(type_map.getPortalTypesForResourceType("foobar"),
00070                          ("Nothing In Here Anymore",))
00071         self.assertEqual(type_map.getPortalTypesForResourceType("chimpanse"),
00072                          ("Bad Monkey", "Bad Ape"))
00073         self.assertRaises(KeyError, type_map.getPortalTypesForResourceType,
00074                           "bonobo")
00075 
00076     def test_delete(self):
00077         type_map = self.type_map
00078         type_map.deleteResourceTypes(["foobar", "bonobo"])
00079         self.assertRaises(KeyError, type_map.getPortalTypesForResourceType,
00080                           "foobar")
00081         self.assertRaises(KeyError, type_map.getPortalTypesForResourceType,
00082                           "bonobo")
00083 
00084 if __name__ == '__main__':
00085     framework()
00086 else:
00087     # While framework.py provides its own test_suite()
00088     # method the testrunner utility does not.
00089     from unittest import TestSuite, makeSuite
00090     def test_suite():
00091         suite = TestSuite()
00092         suite.addTest(makeSuite(TestIResourceTypeMapper))
00093         return suite