testIdToolUpgrade.py 5.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2008 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.Globals import PersistentMapping
from BTrees.Length import Length
import transaction
from zLOG import LOG

class TestIdTool(ERP5TypeTestCase):
  """
  Automatic upgrade of id tool is really sensible to any change. Therefore,
  make sure that the upgrade is still working even if there changes.

  specific test is used, because here some really nasty things are done
  """

  def getTitle(self):
    """
      Return the title of test
    """
    return "Test Id Tool Upgrade"

50
  def testUpgradeIdToolDicts(self):
51 52 53 54
    # With old erp5_core, we have no generators, no IdTool_* zsql methods,
    # and we have a dictionary stored on id tool
    id_tool = self.getPortal().portal_ids
    # Rebuild a persistent mapping like it already existed in beginning 2010
55
    # First persistent mapping of generateNewLengthIdList
56 57
    id_tool.dict_length_ids = PersistentMapping()
    id_tool.dict_length_ids['foo'] = Length(5)
58
    id_tool.dict_length_ids['bar'] = Length(5)
59
    id_tool.IdTool_zSetLastId(id_group='foo', last_id=5)
60
    id_tool.IdTool_zSetLastId(id_group='bar', last_id=10)
61 62 63 64 65 66
    # Then persistent mapping of generateNewId
    id_tool.dict_ids = PersistentMapping()
    id_tool.dict_ids['foo'] = 3
    # it was unfortunately possible to define something else
    # than strings
    id_tool.dict_ids[('bar','baz')] = 2
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    # Delete new zsql methods which are used by new code
    skin_folder = self.getPortal().portal_skins.erp5_core
    custom_skin_folder = self.getPortal().portal_skins.custom
    script_id_list = [x for x in skin_folder.objectIds() 
                      if x.startswith('IdTool')]
    self.assertTrue(len(script_id_list)>0)
    cp_data = skin_folder.manage_cutObjects(ids=script_id_list)
    custom_skin_folder.manage_pasteObjects(cp_data)
    # Set old revision for erp5_core bt, because the id tool decide which code
    # to run depending on this revision
    template_tool = self.getPortal().portal_templates
    erp5_core_bt_list = [x for x in template_tool.objectValues()
                         if x.getTitle()=='erp5_core']
    self.assertEquals(len(erp5_core_bt_list), 1)
    erp5_core_bt = erp5_core_bt_list[0]
    erp5_core_bt.setRevision(1561)
    # Delete all new generators
    generator_id_list = [x for x in id_tool.objectIds()]
    id_tool.manage_delObjects(ids=generator_id_list)
    id_list = id_tool.generateNewLengthIdList(id_group='foo', store=1)
    self.assertEquals(id_list, [5])
    self.assertEquals(int(id_tool.dict_length_ids['foo'].value), 6)
    # Now, reinstall erp5_core, and make sure we still have the possibility
    # to continue generating ids
    cp_data = template_tool.manage_copyObjects(ids=(erp5_core_bt.getId(),))
    new_id = template_tool.manage_pasteObjects(cp_data)[0]['new_id']
    new_bt = template_tool[new_id]
    transaction.commit()
    self.tic()
    transaction.commit()
    new_bt.install(force=1)
    erp5_core_bt.setRevision(1562)
    cp_data = custom_skin_folder.manage_cutObjects(ids=script_id_list)
    skin_folder.manage_pasteObjects(cp_data)
    id_list = id_tool.generateNewLengthIdList(id_group='foo')
    # it is known that with current upgrade there is a whole
    self.assertEquals(id_list, [7])
104 105 106 107
    new_id = id_tool.generateNewId(id_group='foo')
    self.assertEquals(new_id, 4)
    new_id = id_tool.generateNewId(id_group=('bar','baz'))
    self.assertEquals(new_id, 3)
108
    # Make sure that the old code is not used any more, so the dic on
109
    # id tool should not change, checking for length_dict
110
    self.assertEquals(int(id_tool.dict_length_ids['foo'].value), 6)
111 112 113 114 115 116 117 118
    id_list = id_tool.generateNewLengthIdList(id_group='bar')
    self.assertEquals(id_list, [11])
    generator_list = [x for x in id_tool.objectValues()
                      if x.getReference()=='mysql_non_continuous_increasing']
    self.assertEquals(len(generator_list), 1)
    generator = generator_list[0]
    self.assertEquals(generator.last_max_id_dict['foo'].value, 7)
    self.assertEquals(generator.last_max_id_dict['bar'].value, 11)
119 120 121 122 123 124 125 126 127
    # Make sure that the old code is not used any more, so the dic on
    # id tool should not change, checking for dict
    self.assertEquals(id_tool.dict_ids['foo'], 3)
    generator_list = [x for x in id_tool.objectValues()
                      if x.getReference()=='zodb_continuous_increasing']
    self.assertEquals(len(generator_list), 1)
    generator = generator_list[0]
    self.assertEquals(generator.last_id_dict['foo'], 4)
    self.assertEquals(generator.last_id_dict["('bar', 'baz')"], 3)