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

Last change on this file since 2423 was 2423, checked in by mylan, 11 years ago

#131: Updated upgrade tests

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