pyramid_cubicweb/tests/test_tools.py
author Christophe de Vienne <christophe@unlish.com>
Mon, 26 Jan 2015 17:59:10 +0100
changeset 11550 38ed4c3ac3de
child 11611 9d2bb6bdb5c8
permissions -rw-r--r--
[tools] Provide a faster build_user The main trick is to use a cache of user entities. To do so, a few tools are needed since the entities are not supposed to be copied around between connexions. Related to #4870347

from pyramid_cubicweb.tests import PyramidCWTest
from pyramid_cubicweb import tools


class ToolsTest(PyramidCWTest):
    anonymous_allowed = True

    def test_clone_user(self):
        with self.admin_access.repo_cnx() as cnx:
            user = cnx.find('CWUser', login='anon').one()
            user.login  # fill the cache
            clone = tools.clone_user(self.repo, user)

            self.assertEqual(clone.eid, user.eid)
            self.assertEqual(clone.login, user.login)

            self.assertEqual(clone.cw_rset.rows, user.cw_rset.rows)
            self.assertEqual(clone.cw_rset.rql, user.cw_rset.rql)

    def test_cnx_attach_entity(self):
        with self.admin_access.repo_cnx() as cnx:
            user = cnx.find('CWUser', login='anon').one()

        with self.admin_access.repo_cnx() as cnx:
            tools.cnx_attach_entity(cnx, user)
            self.assertEqual(user.login, 'anon')