server/test/unittest_hookhelper.py
author sylvain.thenault@logilab.fr
Fri, 17 Apr 2009 16:55:37 +0200
branchtls-sprint
changeset 1398 5fe84a5f7035
parent 0 b97547f5f1fa
child 1787 71c143c0ada3
permissions -rw-r--r--
rename internal entity types to have CW prefix instead of E

# -*- coding: utf-8 -*-
"""unit/functional tests for cubicweb.server.hookhelper"""

from logilab.common.testlib import unittest_main
from cubicweb.devtools.apptest import RepositoryBasedTC

from cubicweb.server.pool import LateOperation
from cubicweb.server.hookhelper import *


class HookHelpersTC(RepositoryBasedTC):
    
    def setUp(self):
        RepositoryBasedTC.setUp(self)
        self.hm = self.repo.hm
    
    def test_late_operation(self):
        session = self.session
        l1 = LateOperation(session)
        l2 = LateOperation(session)
        l3 = Operation(session)
        self.assertEquals(session.pending_operations, [l3, l1, l2])
        
    def test_single_last_operation(self):
        session = self.session
        l0 = SingleLastOperation(session)
        l1 = LateOperation(session)
        l2 = LateOperation(session)
        l3 = Operation(session)
        self.assertEquals(session.pending_operations, [l3, l1, l2, l0])
        l4 = SingleLastOperation(session)
        self.assertEquals(session.pending_operations, [l3, l1, l2, l4])
        
    def test_global_operation_order(self):
        from cubicweb.server import hooks, schemahooks
        session = self.session
        op1 = hooks.DelayedDeleteOp(session)
        op2 = schemahooks.DelErdefOp(session)
        # equivalent operation generated by op2 but replace it here by op3 so we
        # can check the result...
        op3 = schemahooks.UpdateSchemaOp(session)
        op4 = hooks.DelayedDeleteOp(session)
        op5 = hooks.CheckORelationOp(session)
        self.assertEquals(session.pending_operations, [op1, op2, op4, op5, op3])
                          
       
    def test_in_state_notification(self):
        result = []
        # test both email notification and transition_information
        # whatever if we can connect to the default stmp server, transaction
        # should not fail
        def in_state_changed(session, eidfrom, rtype, eidto):
            tr = previous_state(session, eidfrom)
            if tr is None:
                result.append(tr)
                return
            content = u'trÀnsition from %s to %s' % (tr.name, entity_name(session, eidto))
            result.append(content)
            SendMailOp(session, msg=content, recipients=['test@logilab.fr'])
        self.hm.register_hook(in_state_changed,
                             'before_add_relation', 'in_state')
        self.execute('INSERT CWUser X: X login "paf", X upassword "wouf", X in_state S, X in_group G WHERE S name "activated", G name "users"')
        self.assertEquals(result, [None])
        searchedops = [op for op in self.session.pending_operations
                       if isinstance(op, SendMailOp)]
        self.assertEquals(len(searchedops), 0,
                          self.session.pending_operations)
        self.commit()
        self.execute('SET X in_state S WHERE X login "paf", S name "deactivated"')
        self.assertEquals(result, [None, u'trÀnsition from activated to deactivated'])
        # one to send the mail, one to close the smtp connection
        searchedops = [op for op in self.session.pending_operations
                       if isinstance(op, SendMailOp)]
        self.assertEquals(len(searchedops), 1,
                          self.session.pending_operations)
        self.commit()
        searchedops = [op for op in self.session.pending_operations
                       if isinstance(op, SendMailOp)]
        self.assertEquals(len(searchedops), 0,
                          self.session.pending_operations)
        
if __name__ == '__main__':
    unittest_main()