cubicweb/server/test/unittest_edition.py
author Laurent Wouters <lwouters@cenotelie.fr>
Tue, 24 Apr 2018 10:19:42 +0200
changeset 12297 38058ce2a9ec
permissions -rw-r--r--
[server] Fixed issue with the adapters cache of a cloned EditedEntity The adapters' cache of the entity within an EditedEntity used to be copied along the entity itself during a cloning operation of the EditedEntity. This raises an issue when the adapters' cache is not empty and contains cached values linking to transaction-specific data (or the transaction itself). This prevents the clone EditedEntity being used in a transaction outside the one that created it. This change resets the adapters' cache of the new copy so that no cached value remain. The new copy can then be used in a new transaction afterwards.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12297
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     1
# copyright 2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     3
#
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     5
#
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     9
# any later version.
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    10
#
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    14
# details.
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    15
#
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    18
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    19
"""Tests for the entity edition"""
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    20
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    21
from cubicweb.devtools.testlib import CubicWebTC
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    22
from cubicweb.server.edition import EditedEntity
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    23
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    24
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    25
class EditedEntityTC(CubicWebTC):
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    26
    """
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    27
    Test cases for EditedEntity
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    28
    """
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    29
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    30
    def test_clone_cache_reset(self):
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    31
        """
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    32
        Tests that when an EditedEntity is cloned the caches are reset in the cloned instance
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    33
        :return: Nothing
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    34
        """
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    35
        # Create an entity, create the EditedEntity and clone it
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    36
        with self.admin_access.cnx() as cnx:
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    37
            affaire = cnx.create_entity("Affaire", sujet=u"toto")
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    38
            ee = EditedEntity(affaire)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    39
            ee.entity.cw_adapt_to("IWorkflowable")
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    40
            self.assertTrue(ee.entity._cw_related_cache)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    41
            self.assertTrue(ee.entity._cw_adapters_cache)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    42
            the_clone = ee.clone()
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    43
            self.assertFalse(the_clone.entity._cw_related_cache)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    44
            self.assertFalse(the_clone.entity._cw_adapters_cache)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    45
            cnx.rollback()
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    46
        # Check the attributes
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    47
        with self.admin_access.cnx() as cnx:
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    48
            # Assume a different connection set on the entity
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    49
            self.assertNotEqual(the_clone.entity._cw, cnx)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    50
            # Use the new connection
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    51
            the_clone.entity._cw = cnx
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    52
            self.assertEqual("toto", the_clone.entity.sujet)
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    53
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    54
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    55
if __name__ == '__main__':
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    56
    import unittest
38058ce2a9ec [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    57
    unittest.main()