cubicweb/server/test/unittest_edition.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Tue, 06 Aug 2019 14:26:17 +0200
branch3.26
changeset 12719 9fb4a71f119d
parent 12430 5b6f54f6033a
permissions -rw-r--r--
[py3] Pass bytes as "msg" to smtplib.SMTP.sendmail() When passing a unicode string to smtplib.SMTP.sendmail() as "msg" argument, there is an implicit bytes encoding using "ascii" encoding in python3. Of course this does not work if the string contains non-ASCII characters. In fact, config's sendmails method intent to pass bytes to smtplib.SMTP.sendmail() as it uses msg.as_string() method. Unfortunately, in python3, this method returns a unicode string whereas it returns a bytes string in python2; we thus fix this by calling as_bytes() method on python3. As there is no "as_bytes" method in python2, we need to handle python2 compatibility by hand and either call as_string() or as_bytes(). In testlib, where we mock smtplib.SMTP, we need to keep the "msg" argument of Email class (defined in testlib as well) a unicode string. Otherwise, it fails to be parsed by email.message_from_string() (from stdlib) if it is bytes on python3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12430
5b6f54f6033a [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.
5b6f54f6033a [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
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     3
#
5b6f54f6033a [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.
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     5
#
5b6f54f6033a [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
5b6f54f6033a [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
5b6f54f6033a [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)
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
     9
# any later version.
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    10
#
5b6f54f6033a [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
5b6f54f6033a [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
5b6f54f6033a [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
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    14
# details.
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    15
#
5b6f54f6033a [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
5b6f54f6033a [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/>.
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    18
5b6f54f6033a [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"""
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    20
5b6f54f6033a [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
5b6f54f6033a [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
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    23
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    24
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    25
class EditedEntityTC(CubicWebTC):
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    26
    """
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    27
    Test cases for EditedEntity
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    28
    """
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    29
5b6f54f6033a [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):
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    31
        """
5b6f54f6033a [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
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    33
        :return: Nothing
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    34
        """
5b6f54f6033a [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
5b6f54f6033a [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:
5b6f54f6033a [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")
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    38
            ee = EditedEntity(affaire)
5b6f54f6033a [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")
5b6f54f6033a [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)
5b6f54f6033a [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)
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    42
            the_clone = ee.clone()
5b6f54f6033a [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)
5b6f54f6033a [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)
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    45
            cnx.rollback()
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    46
        # Check the attributes
5b6f54f6033a [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:
5b6f54f6033a [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
5b6f54f6033a [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)
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    50
            # Use the new connection
5b6f54f6033a [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
5b6f54f6033a [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)
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    53
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    54
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    55
if __name__ == '__main__':
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    56
    import unittest
5b6f54f6033a [server] Fixed issue with the adapters cache of a cloned EditedEntity
Laurent Wouters <lwouters@cenotelie.fr>
parents:
diff changeset
    57
    unittest.main()