testArchive.py 15.9 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
##############################################################################
Aurel's avatar
Aurel committed
3 4
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
5
#          Aurélien Calonne <aurel@nexedi.com>
Aurel's avatar
Aurel committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#
# 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.
#
##############################################################################

Jérome Perrin's avatar
Jérome Perrin committed
30
import unittest
Aurel's avatar
Aurel committed
31 32 33 34 35

from Testing import ZopeTestCase
from AccessControl.SecurityManagement import newSecurityManager
from zLOG import LOG
from DateTime import DateTime
36
from Products.ERP5Type.tests.utils import getExtraSqlConnectionStringList
Aurel's avatar
Aurel committed
37
from Products.ERP5.tests.testInventoryAPI import InventoryAPITestCase
38
from Products.ERP5Type.tests.utils import reindex
Aurel's avatar
Aurel committed
39 40 41 42 43 44 45 46 47 48 49


class TestArchive(InventoryAPITestCase):
  """
    Tests for Archive.
  """

  def getTitle(self):
    return "ERP5Archive"

  def getBusinessTemplateList(self):
50 51
    return InventoryAPITestCase.getBusinessTemplateList(self) + (
      'erp5_archive',
52
      'erp5_full_text_mroonga_catalog',
53
    )
Aurel's avatar
Aurel committed
54 55 56 57 58 59 60 61 62

  # Different variables used for this test
  run_all_test = 0
  quiet = 1

  def afterSetUp(self):
    self.login()
    InventoryAPITestCase.afterSetUp(self)
    # make sure there is no message any more
63

Aurel's avatar
Aurel committed
64 65 66 67 68 69 70 71

  def beforeTearDown(self):
    for module in [ self.getPersonModule(),
                    self.getOrganisationModule(),
                    self.getCategoryTool().region,
                    self.getCategoryTool().group ]:
      module.manage_delObjects(list(module.objectIds()))
    self.getPortal().portal_activities.manageClearActivities()
72
    self.tic()
Aurel's avatar
Aurel committed
73 74 75 76 77 78 79

  def login(self):
    uf = self.getPortal().acl_users
    uf._doAddUser('seb', '', ['Manager'], [])
    user = uf.getUserById('seb').__of__(uf)
    newSecurityManager(None, user)

80
  def getSQLPathList(self,connection_id='erp5_sql_connection'):
Aurel's avatar
Aurel committed
81 82 83
    """
    Give the full list of path in the catalog
    """
84 85
    portal = self.getPortal()
    zsql_method_id = "Base_zGetTestPath"
86
    portal_skins_custom = portal.portal_skins.custom
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    zsql_method = getattr(portal_skins_custom, zsql_method_id, None)
    if zsql_method is None:
      portal_skins_custom.manage_addProduct['ZSQLMethods']\
               .manage_addZSQLMethod(
          id = zsql_method_id,
          title = '',
          connection_id = connection_id,
          arguments = "",
          template = "select path from catalog")
      zsql_method = portal_skins_custom[zsql_method_id]
      zsql_method.max_rows_ = 0
    # it is mandatory to provide connection_id, or the
    # zsql method will look at preference and use the one
    # defined by the archive
    result = zsql_method(connection_id=connection_id)
Aurel's avatar
Aurel committed
102 103 104 105 106 107 108 109 110
    path_list = map(lambda x: x['path'],result)
    return path_list

  def checkRelativeUrlInSQLPathList(self,url_list,connection_id=None):
    path_list = self.getSQLPathList(connection_id=connection_id)
    portal_id = self.getPortalId()
    for url in url_list:
      path = '/' + portal_id + '/' + url
      #LOG('checkRelativeUrlInSQLPathList found path:',0,path)
111
      self.assertTrue(path in path_list)
Aurel's avatar
Aurel committed
112 113 114 115 116 117 118

  def checkRelativeUrlNotInSQLPathList(self,url_list,connection_id=None):
    path_list = self.getSQLPathList(connection_id=connection_id)
    portal_id = self.getPortalId()
    for url in url_list:
      path = '/' + portal_id + '/' + url
      #LOG('checkRelativeUrlInSQLPathList not found path:',0,path)
119
      self.assertTrue(path not in  path_list)
Aurel's avatar
Aurel committed
120

121 122 123 124 125 126 127 128 129 130 131
  @reindex
  def _makeInventory(self, date):
    """
    Create inventory, use to check if they goes to the right catalog
    """
    portal = self.getPortal()
    inventory_module = portal.getDefaultModule(portal_type = "Inventory Module")
    inventory = inventory_module.newContent(portal_type = "Inventory")
    inventory.edit(stop_date = date,)
    return inventory

Aurel's avatar
Aurel committed
132 133 134 135 136 137 138 139 140 141 142
  def test_Archive(self, quiet=quiet, run=1): #run_all_test):
    if not run: return
    if not quiet:
      message = 'Archive'
      ZopeTestCase._print('\n%s ' % message)
      LOG('Testing... ',0,message)

    portal = self.getPortal()
    portal_category = self.getCategoryTool()
    portal_archive = self.getArchiveTool()
    portal_catalog = self.getCatalogTool()
143
    inventory_module = portal.getDefaultModule(portal_type = "Inventory Module")
Aurel's avatar
Aurel committed
144 145 146 147 148 149 150 151 152
    # Create some objects
    self.base_category = portal_category.newContent(portal_type='Base Category',
                                               title="GreatTitle1")
    module = portal.getDefaultModule('Organisation')
    self.organisation = module.newContent(portal_type='Organisation',
                                     title="GreatTitle2")
    getInventory = self.getSimulationTool().getInventory
    self.mvt = self._makeMovement(quantity=100, stop_date=DateTime("2006/06/06"),
                                  simulation_state='delivered',)
153
    self.assertEqual(100, getInventory(node_uid=self.node.getUid()))
Aurel's avatar
Aurel committed
154
    self.assertEqual(len(self.folder.searchFolder(portal_type="Dummy Movement")), 1)
155 156 157

    # Create an inventory object
    self.inventory = self._makeInventory(date=DateTime("2006/06/15"))
158
    self.assertEqual(len(inventory_module.searchFolder(portal_type="Inventory")), 1)
159

Aurel's avatar
Aurel committed
160
    # Flush message queue
161
    self.tic()
Aurel's avatar
Aurel committed
162 163 164 165

    # Check well in catalog
    self.original_connection_id = 'erp5_sql_connection'
    self.original_deferred_connection_id = 'erp5_sql_deferred_connection'
166
    path_list = [self.organisation.getRelativeUrl(), self.inventory.getRelativeUrl()]
Aurel's avatar
Aurel committed
167
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
168

Aurel's avatar
Aurel committed
169
    # Create new connectors for destination
170 171
    addSQLConnection = portal.manage_addProduct['ZMySQLDA'] \
      .manage_addZMySQLConnection
Aurel's avatar
Aurel committed
172
    self.new_connection_id = 'erp5_sql_connection1'
173
    db1, db2 = getExtraSqlConnectionStringList()[:2]
174
    addSQLConnection(self.new_connection_id,'', db1)
Aurel's avatar
Aurel committed
175 176 177 178
    new_connection = portal[self.new_connection_id]
    new_connection.manage_open_connection()
    # the deferred one
    self.new_deferred_connection_id = 'erp5_sql_connection2'
179
    addSQLConnection(self.new_deferred_connection_id,'', db1)
Aurel's avatar
Aurel committed
180 181 182 183 184
    new_deferred_connection = portal[self.new_deferred_connection_id]
    new_deferred_connection.manage_open_connection()

    # Create new connectors for archive
    self.archive_connection_id = 'erp5_sql_connection3'
185
    addSQLConnection(self.archive_connection_id,'', db2)
Aurel's avatar
Aurel committed
186 187 188 189
    archive_connection = portal[self.archive_connection_id]
    archive_connection.manage_open_connection()
    # the deferred one
    self.archive_deferred_connection_id = 'erp5_sql_connection4'
190
    addSQLConnection(self.archive_deferred_connection_id,'', db2)
Aurel's avatar
Aurel committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    archive_deferred_connection = portal[self.archive_deferred_connection_id]
    archive_deferred_connection.manage_open_connection()

    # Create new catalog for destination
    self.original_catalog_id = 'erp5_mysql_innodb'
    self.new_catalog_id = self.original_catalog_id + '_2'
    cp_data = portal_catalog.manage_copyObjects(ids=('erp5_mysql_innodb',))
    new_id = portal_catalog.manage_pasteObjects(cp_data)[0]['new_id']
    new_catalog_id = 'erp5_mysql_innodb_2'
    portal_catalog.manage_renameObject(id=new_id,new_id=new_catalog_id)

    # Create new catalog for archive
    self.archive_catalog_id = self.original_catalog_id + '_archive'
    cp_data = portal_catalog.manage_copyObjects(ids=('erp5_mysql_innodb',))
    archive_id = portal_catalog.manage_pasteObjects(cp_data)[0]['new_id']
    archive_catalog_id = 'erp5_mysql_innodb_archive'
    portal_catalog.manage_renameObject(id=archive_id,new_id=archive_catalog_id)

    # Create an archive
Sebastien Robin's avatar
Sebastien Robin committed
210
    archive = portal_archive.newContent(portal_type="Archive",
Aurel's avatar
Aurel committed
211 212 213 214 215 216 217 218 219 220 221
                                        catalog_id=self.archive_catalog_id,
                                        connection_id=self.archive_connection_id,
                                        deferred_connection_id=self.archive_deferred_connection_id,
                                        priority=3,
                                        inventory_method_id='Archive_createAllInventory',
                                        test_method_id='Archive_test',
                                        stop_date_range_min=DateTime("2006/06/01"),
                                        stop_date_range_max=DateTime("2006/07/01"),
                                        )
    archive.ready()
    # Create an archive for destination catalog
Sebastien Robin's avatar
Sebastien Robin committed
222
    dest = portal_archive.newContent(portal_type="Archive",
Aurel's avatar
Aurel committed
223 224 225 226 227 228 229 230 231
                                     catalog_id=self.new_catalog_id,
                                     connection_id=self.new_connection_id,
                                     deferred_connection_id=self.new_deferred_connection_id,
                                     priority=1,
                                     test_method_id='Archive_test',
                                     stop_date_range_min=DateTime("2006/07/01"),
                                     )
    dest.ready()

232
    # make sure to commit to release any lock on tables
233
    self.commit()
234

Aurel's avatar
Aurel committed
235 236 237 238 239 240 241 242
    # Do archive
    portal_archive.manage_archive(destination_archive_id=dest.getId(),
                                  archive_id=archive.getId(),
                                  update_destination_sql_catalog=True,
                                  update_archive_sql_catalog=True,
                                  clear_destination_sql_catalog=True,
                                  clear_archive_sql_catalog=True)

243
    self.tic()
Aurel's avatar
Aurel committed
244 245 246 247 248 249 250 251 252 253 254 255
    self.assertEqual(portal_catalog.getSQLCatalog().id, self.new_catalog_id)
    self.assertEqual(archive.getValidationState(), 'validated')
    self.assertEqual(dest.getValidationState(), 'validated')
    # Check objects organisation are indexed
    # in both archive and current catalog and old one
    path_list = [self.organisation.getRelativeUrl()]
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)
    # Create a new organisation and check it goes in both catalog and not old one
    self.organisation_1 = module.newContent(portal_type='Organisation',
                                            title="GreatTitle3")
256
    self.tic()
Aurel's avatar
Aurel committed
257 258 259 260 261 262 263 264 265 266 267 268
    path_list = [self.organisation_1.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)

    # Check objects movement are indexed
    # in archive and old one and not in current catalog
    path_list = [self.mvt.getRelativeUrl()]
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)

269 270 271 272 273 274 275
    # Check inventory are indexed
    # in archive and old one and not in current catalog
    path_list = [self.inventory.getRelativeUrl()]
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)

Aurel's avatar
Aurel committed
276 277
    # Create a new movement and check it goes only in new catalog
    self.assertEqual(len(self.folder.searchFolder(portal_type="Dummy Movement")), 0)
278
    self.assertEqual(100, getInventory(node_uid=self.node.getUid()))
Aurel's avatar
Aurel committed
279 280
    self.new_mvt = self._makeMovement(quantity=50, stop_date=DateTime("2006/08/06"),
                                      simulation_state='delivered',)
281
    self.tic()
Aurel's avatar
Aurel committed
282 283 284 285 286 287 288
    self.assertEqual(len(self.folder.searchFolder(portal_type="Dummy Movement")), 1)
    # Check objects movement are indexed
    # not in archive and old one but in current catalog
    path_list = [self.new_mvt.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.archive_connection_id)
289
    self.assertEqual(150, getInventory(node_uid=self.node.getUid()))
Aurel's avatar
Aurel committed
290 291 292 293 294 295

    # now play with preference to select to view document from archive
    portal_preferences = self.getPreferenceTool()
    self.pref = portal_preferences.newContent(id='user_pref',
                                              portal_type='Preference',
                                              preferred_archive=archive.getRelativeUrl())
296
    self.tic()
Aurel's avatar
Aurel committed
297
    self.getPreferenceTool().recursiveReindexObject()
298

Aurel's avatar
Aurel committed
299 300 301
    self.portal.portal_workflow.doActionFor(self.pref,
                                            'enable_action',
                                            wf_id='preference_workflow')
302
    self.assertEqual(self.pref.getPreferenceState(),    'enabled')
Aurel's avatar
Aurel committed
303 304 305 306 307 308 309 310

    path_list = [self.pref.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.original_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)

    self.assertEqual(portal_catalog.getPreferredSQLCatalogId(), archive.getCatalogId())
    self.assertEqual(len(self.folder.searchFolder(portal_type="Dummy Movement")), 1)
311

Aurel's avatar
Aurel committed
312
    # As we only have first movement in archive, inventory must be 100
313
    self.assertEqual(100, getInventory(node=self.node.getRelativeUrl()))
Aurel's avatar
Aurel committed
314

315 316
    # go on current catalog
    self.pref.edit(preferred_archive=None)
317
    self.tic()
318

319
    # unindex and reindex an older movement and check it's well reindexed
320
    self.inventory.unindexObject()
321
    self.tic()
322 323 324 325
    path_list = [self.inventory.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.archive_connection_id)
    self.inventory.reindexObject()
326
    self.tic()
327 328 329
    path_list = [self.inventory.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlInSQLPathList(path_list, connection_id=self.archive_connection_id)
330 331
    # check inventory in archive now
    self.pref.edit(preferred_archive=archive.getRelativeUrl())
332
    self.tic()
333
    self.assertEqual(100, getInventory(node=self.node.getRelativeUrl()))
334

335 336
    # check if we unindex an object, it's remove in all catalog:
    module.manage_delObjects([self.organisation_1.id,])
337
    self.tic()
338 339 340
    path_list = [self.organisation_1.getRelativeUrl()]
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.new_connection_id)
    self.checkRelativeUrlNotInSQLPathList(path_list, connection_id=self.archive_connection_id)
Aurel's avatar
Aurel committed
341

342
    # check the current archive
343
    self.assertEqual(portal_archive.getCurrentArchive(), dest)
344

345

Jérome Perrin's avatar
Jérome Perrin committed
346 347 348 349
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestArchive))
  return suite
Aurel's avatar
Aurel committed
350