testPersonERP5Synchronization.py 16.7 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1
# -*- coding: utf-8 -*-
Aurel's avatar
Aurel committed
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
##############################################################################
#
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
#               Hervé Poulain <herve@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.
#
##############################################################################

import transaction
from DateTime import DateTime
from Products.ERP5TioSafe.tests.testPrestashopMixin import testPrestashopMixin

class testPersonERP5Synchronization(testPrestashopMixin):
  """ This class allows to check different cases of Person's sync. """
  def afterSetUp(self):
    """ This method is called after the SetUp method. """
    # Shortcut for modules and tools
    self.person_module = self.portal.person_module
    self.connection = self.portal.erp5_sql_connection
    self.prestashop = self.portal.portal_integrations.prestashop
    self.root_xml = '<directory>\n%s\n</directory>'

  def test_PrestashopSimplestXMLSync(self):
    """ This test checks the person sync with the simplest XML. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_01.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    person = self.person_module.contentValues()[0]
    self.assertEqual(person.getTitle(), 'John DOE')
    self.assertEqual(person.getFirstName(), 'John')
    self.assertEqual(person.getLastName(), 'DOE')
    self.assertEqual(person.getDefaultEmailText(), 'john@doe.com')
    # Check the XML schema and the fixed point
    self.checkTioSafeXML(
        plugin_xml=self.root_xml % person.Node_asTioSafeXML(),
        tiosafe_xml=self.root_xml % self.prestashop.person_module()[0].asXML(),
        xsd_path='../XSD/nodes.xsd',
    )

  def test_PrestashopPersonWithSingleAddressSync(self):
    """ This test checks the person sync with an address. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.initMapping(self.prestashop)
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_02.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    person = self.person_module.contentValues()[0]
    self.assertEqual(person.getTitle(), 'Jean GRAY')
    self.assertEqual(person.getFirstName(), 'Jean')
    self.assertEqual(person.getLastName(), 'GRAY')
    self.assertEqual(person.getDefaultEmailText(), 'jean@gray.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('1985-04-05')))
    self.assertEqual(len(person.searchFolder(portal_type='Address')), 1)
    self.assertEqual(
        person.getDefaultAddressStreetAddress(),
        '22 rue des Peupliers',
    )
    self.assertEqual(person.getDefaultAddressZipCode(), '75000')
    self.assertEqual(person.getDefaultAddressCity(), 'Paris')
    self.assertEqual(person.getDefaultAddressRegionTitle(), 'France')
    # Check the XML schema and the fixed point
    self.checkTioSafeXML(
        plugin_xml=self.root_xml % person.Node_asTioSafeXML(),
        tiosafe_xml=self.root_xml % self.prestashop.person_module()[0].asXML(),
        xsd_path='../XSD/nodes.xsd',
    )

  def test_PrestashopPersonWithMultipleAddressSync(self):
    """ This test checks the person sync with the simplest XML. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.initMapping(self.prestashop)
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_03.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    person = self.person_module.contentValues()[0]
    self.assertEqual(person.getTitle(), 'Jenifer-Dylan COX')
    self.assertEqual(person.getFirstName(), 'Jenifer-Dylan')
    self.assertEqual(person.getLastName(), 'COX')
    self.assertEqual(person.getDefaultEmailText(), 'jenifer-dylan@cox.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('2008-06-06')))
    self.assertEqual(len(person.searchFolder(portal_type='Address')), 3)
    for address in person.searchFolder(portal_type='Address'):
      index = address.getId()
      if index == 'default_address':
        self.assertEqual(
            address.getStreetAddress(),
            '123 boulevard des Capucines',
        )
        self.assertEqual(address.getZipCode(), '72160')
        self.assertEqual(address.getCity(), 'Stuttgart')
        self.assertEqual(address.getRegionTitle(), 'Allemagne')
      elif index == '2':
        self.assertEqual(
            address.getStreetAddress(),
            '234 rue de la Paix',
        )
        self.assertEqual(address.getZipCode(), '75000')
        self.assertEqual(address.getCity(), 'Paris')
        self.assertEqual(address.getRegionTitle(), 'France')
      elif index == '3':
        self.assertEqual(address.getStreetAddress(), '345 avenue des Fleurs')
        self.assertEqual(address.getZipCode(), '44001')
        self.assertEqual(address.getCity(), 'Dortmund')
        self.assertEqual(address.getRegionTitle(), 'Allemagne')
      else:
        raise ValueError, 'Can not check the address: %s of the person: %s' % \
            (address.getId(), person.getTitle())
    # Check the XML schema and the fixed point
    self.checkTioSafeXML(
        plugin_xml=self.root_xml % person.Node_asTioSafeXML(),
        tiosafe_xml=self.root_xml % self.prestashop.person_module()[0].asXML(),
        xsd_path='../XSD/nodes.xsd',
    )

  def test_PrestashopPersonWithAddressNoMappingSync(self):
    """ This test checks the person sync with an address and no mapping. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_04.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Check that an empty mapping is created in integration site
    self.assertTrue(self.prestashop.get('Country', None) is None)
    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 0)

  def test_PrestashopDeletePerson(self):
    """ Check that the delete during a person's sync invalidate the person. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_05.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 3)
    # Move the persons as draft, validated and invalidated state
    john_doe = self.person_module.searchFolder(
        portal_type='Person',
        title='John DOE',
    )[0].getObject()
    jane_doe = self.person_module.searchFolder(
        portal_type='Person',
        title='Jane DOE',
    )[0].getObject()
    if jane_doe.getValidationState() != "validated":
      jane_doe.validate()
    dan_doe = self.person_module.searchFolder(
        portal_type='Person',
        title='Dan DOE',
    )[0].getObject()
    if dan_doe.getValidationState() != "validated":
      dan_doe.validate()
    dan_doe.invalidate()
    # Remove the persons in prestashop and check that after sync the state
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_06.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()
    self.assertEqual(john_doe.getValidationState(), 'validated')
    self.assertEqual(jane_doe.getValidationState(), 'validated')
    self.assertEqual(dan_doe.getValidationState(), 'invalidated')
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 3)
    self.assertEqual(john_doe.getValidationState(), 'invalidated')
    self.assertEqual(jane_doe.getValidationState(), 'invalidated')
    self.assertEqual(dan_doe.getValidationState(), 'invalidated')

  def test_PrestashopUpdateSimpleElement(self):
    """ This test checks the simple update after sync of persons. """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.initMapping(self.prestashop)
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_07.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    person = self.person_module.contentValues()[0]
    self.assertEqual(person.getTitle(), 'Chris TURK')
    self.assertEqual(person.getFirstName(), 'Chris')
    self.assertEqual(person.getLastName(), 'TURK')
    self.assertEqual(person.getDefaultEmailText(), 'chris@turk.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('1980-06-22')))
    self.assertEqual(len(person.searchFolder(portal_type='Address')), 1)
    self.assertEqual(
        person.getDefaultAddressStreetAddress(),
        '22 rue des Peupliers',
    )
    self.assertEqual(person.getDefaultAddressZipCode(), '75000')
    self.assertEqual(person.getDefaultAddressCity(), 'Paris')
    self.assertEqual(person.getDefaultAddressRegionTitle(), 'France')
    # Update the data, run the sync and check the data after the update
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_08.sql' % self.ps_dump_path,
    )
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    self.assertEqual(person.getTitle(), 'Chris TURK')
    self.assertEqual(person.getFirstName(), 'Chris')
    self.assertEqual(person.getLastName(), 'TURK')
    self.assertEqual(person.getDefaultEmailText(), 'chris@turk.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('1974-06-22')))
    self.assertEqual(len(person.searchFolder(portal_type='Address')), 1)
    self.assertEqual(
        person.getDefaultAddressStreetAddress(),
        '22 rue des Peupliers',
    )
    self.assertEqual(person.getDefaultAddressZipCode(), '75000')
    self.assertEqual(person.getDefaultAddressCity(), 'Paris')
    self.assertEqual(person.getDefaultAddressRegionTitle(), 'France')
    # Check the XML schema and the fixed point
    self.checkTioSafeXML(
        plugin_xml=self.root_xml % person.Node_asTioSafeXML(),
        tiosafe_xml=self.root_xml % self.prestashop.person_module()[0].asXML(),
        xsd_path='../XSD/nodes.xsd',
    )

  def test_PrestashopComplexeUpdateElement(self):
    """
      This test checks the complexe update after sync of persons.
      It updates some element, adds others and removes the last.
    """
    # Initialize the instance and prestashop
    self.initPrestashopTest()
    self.initMapping(self.prestashop)
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_09.sql' %  self.ps_dump_path,
    )
    transaction.commit()
    self.tic()

    # Run the sync of persons and check person's data after sync
    self.assertEqual(len(self.person_module.contentValues()), 0)
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(len(self.person_module.contentValues()), 1)
    person = self.person_module.contentValues()[0]
    self.assertEqual(person.getTitle(), 'Perry COX')
    self.assertEqual(person.getFirstName(), 'Perry')
    self.assertEqual(person.getLastName(), 'COX')
    self.assertEqual(person.getDefaultEmailText(), 'perry@cox.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('1959-08-03')))
    address_list = person.searchFolder(
        portal_type='Address',
        sort_on=(['id', 'ASC'], ),
    )
    self.assertEqual(len(address_list), 2)
    for index, address in enumerate(address_list):
      if index == 0:
        self.assertEqual(address.getStreetAddress(), '789 avenue des Fleurs')
        self.assertEqual(address.getZipCode(), '44001')
        self.assertEqual(address.getCity(), 'Dortmund')
        self.assertEqual(address.getRegionTitle(), 'Allemagne')
      elif index == 1:
        self.assertEqual(address.getStreetAddress(), '456 rue de la Paix')
        self.assertEqual(address.getZipCode(), '75000')
        self.assertEqual(address.getCity(), 'Paris')
        self.assertEqual(address.getRegionTitle(), 'France')
      else:
        raise ValueError, 'Can not check the address: %s of the person: %s' % \
            (index, person.getTitle())
    # The first update check remove of simple element, remove an address
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_10.sql' % self.ps_dump_path,
    )
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(person.getTitle(), 'Perry COX')
    self.assertEqual(person.getFirstName(), 'Perry')
    self.assertEqual(person.getLastName(), 'COX')
    self.assertEqual(person.getDefaultEmailText(), 'perry@cox.com')
    self.assertEqual(person.getStartDate(), None)
    self.assertEqual(len(person.searchFolder(portal_type='Address')), 1)
    self.assertEqual(
        person.getDefaultAddressStreetAddress(),
        '123 boulevard des Capucines',
    )
    self.assertEqual(person.getDefaultAddressZipCode(), '72160')
    self.assertEqual(person.getDefaultAddressCity(), 'Stuttgart')
    self.assertEqual(person.getDefaultAddressRegionTitle(), 'Allemagne')
    # The second update check the add of a simple element and the add of
    # address
    self.loadSQLDump(
        self.connection,
        '%s/dump_person_sync_11.sql' % self.ps_dump_path,
    )
    self.loadSync([self.prestashop.person_module, ])
    self.assertEqual(person.getTitle(), 'Perry COX')
    self.assertEqual(person.getFirstName(), 'Perry')
    self.assertEqual(person.getLastName(), 'COX')
    self.assertEqual(person.getDefaultEmailText(), 'perry@cox.com')
    self.assertEqual(str(person.getStartDate()), str(DateTime('1959-08-03')))
    address_list = person.searchFolder(
        portal_type='Address',
        sort_on=(['id', 'ASC'], ),
    )
    self.assertEqual(len(address_list), 2)
    for index, address in enumerate(address_list):
      if index == 0:
        self.assertEqual(address.getStreetAddress(), '789 avenue des Fleurs')
        self.assertEqual(address.getZipCode(), '44001')
        self.assertEqual(address.getCity(), 'Dortmund')
        self.assertEqual(address.getRegionTitle(), 'Allemagne')
      elif index == 1:
        self.assertEqual(
            address.getStreetAddress(),
            '123 boulevard des Capucines',
        )
        self.assertEqual(address.getZipCode(), '72160')
        self.assertEqual(address.getCity(), 'Stuttgart')
        self.assertEqual(address.getRegionTitle(), 'Allemagne')
      else:
        raise ValueError, 'Can not check the address: %s of the person: %s' % \
            (index, person.getTitle())
    # Check the XML schema and the fixed point
    self.checkTioSafeXML(
        plugin_xml=self.root_xml % person.Node_asTioSafeXML(),
        tiosafe_xml=self.root_xml % self.prestashop.person_module()[0].asXML(),
        xsd_path='../XSD/nodes.xsd',
    )