source: products/quintagroup.plonegooglesitemaps/trunk/quintagroup/plonegooglesitemaps/tests/testUpgrade.py @ 2390

Last change on this file since 2390 was 2390, checked in by mylan, 9 years ago

#205: Fix bug with upgrading only root level objects

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1#
2# Tests for quintagroup.plonegooglesitemaps upgrading
3#
4
5from base import *
6from Products.CMFPlone.utils import _createObjectByType
7from Products.GenericSetup.upgrade import _upgrade_registry
8from quintagroup.plonegooglesitemaps import config
9from quintagroup.plonegooglesitemaps import upgrades as gsm_upgrades
10from quintagroup.canonicalpath.interfaces import ICanonicalPath
11from quintagroup.canonicalpath.interfaces import ICanonicalLink
12
13class TestUpgrade(TestCase):
14
15    def afterSetUp(self):
16        self.setup = self.portal.portal_setup
17        self.profile = "quintagroup.plonegooglesitemaps:default"
18
19    def getUpgradeStep(self, sortkey):
20        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
21        upgrade_id = upgrades[sortkey-1]["id"]
22        step = _upgrade_registry.getUpgradeStep(self.profile, upgrade_id)
23        return step
24
25    def test_upgradeStepsRegistration(self):
26        # Test upgrade steps
27        upgrades = self.setup.listUpgrades(self.profile, show_old=True)
28        self.assertEqual(len(upgrades), 2)
29        self.assertEqual(upgrades[0]["title"].endswith("1.0 to 1.1"), True)
30        self.assertEqual(upgrades[1]["title"].endswith("1.1 to 1.2"), True)
31
32    def test_upgradeSetupRegistration(self):
33        # Test registered upgrade profiles
34        pids = [i['id'] for i in self.setup.listProfileInfo()]
35        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_0_to_1_1" in pids, True)
36        self.assertEqual("quintagroup.plonegooglesitemaps:upgrade_1_1_to_1_2" in pids, True)
37
38    def test_step_1_0_to_1_1(self):
39        # Prepare testing data
40        catalog = self.portal.portal_catalog
41        if "canonical_path" in catalog._catalog.names:
42            catalog.delColumn("canonical_path")
43        # Upgrade to 1.1 version
44        step = self.getUpgradeStep(1)
45        if step is not None:
46            step.doStep(self.setup)
47        # Canonical_path column must be added to portal_catalog
48        self.assertEqual("canonical_path" in catalog._catalog.names, True)
49
50    def test_step_1_1_to_1_2(self):
51        # Prepare testing data
52        catalog = self.portal.portal_catalog
53        # Create container folder, update its canonical path
54        folder = _createObjectByType('Folder', self.portal, id='test_folder')
55        fldr_cpath = "/my_test_home_folder"
56        fldr_clink = self.portal.absolute_url() + fldr_cpath
57        ICanonicalPath(folder).canonical_path = fldr_cpath
58        # Create inner document, update its canonical_path
59        doc = _createObjectByType('Document', folder, id='test_doc')
60        doc_cpath = "/test_folder/my_test_doc"
61        doc_clink = self.portal.absolute_url() + doc_cpath
62        ICanonicalPath(doc).canonical_path = doc_cpath
63        # Add canonical_path column in catalog
64        if not "canonical_path" in catalog._catalog.names:
65            catalog.addColumn("canonical_path")
66        # Upgrade to 1.2 versionb
67        step = self.getUpgradeStep(2)
68        if step is not None:
69            step.doStep(self.setup)
70        # canonical_link column replace canonical_path one in the portal_catalog
71        self.assertEqual("canonical_link" in catalog._catalog.names, True)
72        self.assertEqual("canonical_path" in catalog._catalog.names, False)
73        # canonical_link property refactored from canonical_path one for inner doc
74        self.assertNotEqual(ICanonicalPath(doc).canonical_path, doc_cpath)
75        self.assertEqual(ICanonicalLink(doc).canonical_link, doc_clink)
76        # canonical_link property refactored from canonical_path one for home folder
77        self.assertNotEqual(ICanonicalPath(folder).canonical_path, fldr_cpath)
78        self.assertEqual(ICanonicalLink(folder).canonical_link, fldr_clink)
79        # canonical_link brain must contains updated canonical_link data
80        brain = catalog(id="test_doc")[0]
81        self.assertEqual(brain.canonical_link, doc_clink)
82        brain = catalog(id="test_folder")[0]
83        self.assertEqual(brain.canonical_link, fldr_clink)
84
85    def testUpgradeCallOnReinstall(self):
86        # Get upgrade steps
87        upgrades = _upgrade_registry.getUpgradeStepsForProfile(self.profile)
88        upgrades = dict([(u.sortkey, u) for u in upgrades.values()])
89        try:
90            # Replace original handlers with patched ones for test calls
91            called = []
92            upgrades[1].handler = lambda st:called.append("1.0 to 1.1")
93            upgrades[2].handler = lambda st:called.append("1.1 to 1.2")
94            # Run reinstallation
95            self.portal.portal_quickinstaller.reinstallProducts(products=config.PROJECTNAME)
96            # Test upgrades call
97            self.assertEqual("1.0 to 1.1" in called, True)
98            self.assertEqual("1.1 to 1.2" in called, True)
99        finally:
100            # Restore original upgrade handlers
101            upgrades[1].handler = gsm_upgrades.upgrade_1_0_to_1_1
102            upgrades[2].handler = gsm_upgrades.upgrade_1_1_to_1_2
103
104       
105
106def test_suite():
107    from unittest import TestSuite, makeSuite
108    suite = TestSuite()
109    suite.addTest(makeSuite(TestUpgrade))
110    return suite
111
112if __name__ == '__main__':
113    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the repository browser.