<s> import unittest <EOL> from pyramid import testing <EOL> class TestAction ( unittest . TestCase ) : <EOL> def _makeOne ( self ) : <EOL> from . . deferred import Action <EOL> return Action ( ) <EOL> def test___repr__ ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> result = repr ( inst ) <EOL> self . assertTrue ( <EOL> result . startswith ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> def test___hash__ ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> self . assertEqual ( hash ( inst ) , hash ( ( inst . oid , inst . index_oid ) ) ) <EOL> def test___eq__True ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> other = self . _makeOne ( ) <EOL> self . assertTrue ( inst == other ) <EOL> def test___eq__False ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> other = self . _makeOne ( ) <EOL> other . oid = <NUM_LIT> <EOL> self . assertFalse ( inst == other ) <EOL> def test___lt__ ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> other = self . _makeOne ( ) <EOL> other . oid = <NUM_LIT:2> <EOL> inst . oid = <NUM_LIT:1> <EOL> self . assertTrue ( inst < other ) <EOL> def test___gt__ ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> other = self . _makeOne ( ) <EOL> other . oid = <NUM_LIT:2> <EOL> inst . oid = <NUM_LIT:1> <EOL> self . assertTrue ( other . __gt__ ( inst ) ) <EOL> def test_find_resource_resource_cant_be_found ( self ) : <EOL> from . . deferred import ResourceNotFound <EOL> index = testing . DummyResource ( ) <EOL> index . __objectmap__ = DummyObjectmap ( None ) <EOL> inst = self . _makeOne ( ) <EOL> inst . index = index <EOL> inst . oid = <NUM_LIT:1> <EOL> self . assertRaises ( ResourceNotFound , inst . find_resource ) <EOL> def test_find_resource_objectmap_cant_be_found ( self ) : <EOL> from . . deferred import ObjectMapNotFound <EOL> index = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( ) <EOL> inst . index = index <EOL> inst . oid = <NUM_LIT:1> <EOL> self . assertRaises ( ObjectMapNotFound , inst . find_resource ) <EOL> def test_find_resource ( self ) : <EOL> index = testing . DummyResource ( ) <EOL> index . __objectmap__ = DummyObjectmap ( '<STR_LIT:abc>' ) <EOL> inst = self . _makeOne ( ) <EOL> inst . index = index <EOL> inst . oid = <NUM_LIT:1> <EOL> self . assertEqual ( inst . find_resource ( ) , '<STR_LIT:abc>' ) <EOL> class TestResourceNotFound ( unittest . TestCase ) : <EOL> def _makeOne ( self , action ) : <EOL> from . . deferred import ResourceNotFound <EOL> return ResourceNotFound ( action ) <EOL> def test___repr__ ( self ) : <EOL> inst = self . _makeOne ( <NUM_LIT:1> ) <EOL> self . assertEqual ( <EOL> repr ( inst ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> class TestIndexAction ( unittest . TestCase ) : <EOL> def _makeOne ( self , index , mode = '<STR_LIT>' , oid = '<STR_LIT>' ) : <EOL> from . . deferred import IndexAction <EOL> return IndexAction ( index , mode , oid ) <EOL> def test_index_oid_from_index ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . index_oid , index . __oid__ ) <EOL> def test_execute ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> resource = testing . DummyResource ( ) <EOL> inst . find_resource = lambda * arg : resource <EOL> inst . execute ( ) <EOL> self . assertEqual ( index . oid , '<STR_LIT>' ) <EOL> self . assertEqual ( index . resource , resource ) <EOL> def test_execute_objectmap_not_found ( self ) : <EOL> from . . deferred import ObjectMapNotFound <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> def find_resource ( ) : <EOL> raise ObjectMapNotFound ( None ) <EOL> inst . find_resource = find_resource <EOL> inst . execute ( ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( index . oid , None ) <EOL> def test_anti ( self ) : <EOL> from . . deferred import UnindexAction <EOL> index = testing . DummyResource ( ) <EOL> index . __oid__ = <NUM_LIT:1> <EOL> inst = self . _makeOne ( index ) <EOL> result = inst . anti ( ) <EOL> self . assertEqual ( result . __class__ , UnindexAction ) <EOL> self . assertEqual ( result . index , index ) <EOL> self . assertEqual ( result . index_oid , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . mode , '<STR_LIT>' ) <EOL> self . assertEqual ( result . oid , '<STR_LIT>' ) <EOL> class TestReindexAction ( unittest . TestCase ) : <EOL> def _makeOne ( self , index , mode = '<STR_LIT>' , oid = '<STR_LIT>' ) : <EOL> from . . deferred import ReindexAction <EOL> return ReindexAction ( index , mode , oid ) <EOL> def test_index_oid_from_index ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . index_oid , index . __oid__ ) <EOL> def test_execute ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> resource = testing . DummyResource ( ) <EOL> inst . find_resource = lambda * arg : resource <EOL> inst . execute ( ) <EOL> self . assertEqual ( index . oid , '<STR_LIT>' ) <EOL> self . assertEqual ( index . resource , resource ) <EOL> def test_execute_objectmap_not_found ( self ) : <EOL> from . . deferred import ObjectMapNotFound <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> def find_resource ( ) : <EOL> raise ObjectMapNotFound ( None ) <EOL> inst . find_resource = find_resource <EOL> inst . execute ( ) <EOL> self . assertEqual ( index . oid , None ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:1> ) <EOL> def test_anti ( self ) : <EOL> from . . deferred import ReindexAction <EOL> index = testing . DummyResource ( ) <EOL> index . __oid__ = <NUM_LIT:1> <EOL> inst = self . _makeOne ( index ) <EOL> result = inst . anti ( ) <EOL> self . assertEqual ( result . __class__ , ReindexAction ) <EOL> self . assertEqual ( result . index , index ) <EOL> self . assertEqual ( result . index_oid , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . mode , '<STR_LIT>' ) <EOL> self . assertEqual ( result . oid , '<STR_LIT>' ) <EOL> class TestUnindexAction ( unittest . TestCase ) : <EOL> def _makeOne ( self , index , mode = '<STR_LIT>' , oid = '<STR_LIT>' ) : <EOL> from . . deferred import UnindexAction <EOL> return UnindexAction ( index , mode , oid ) <EOL> def test_index_oid_from_index ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . index_oid , index . __oid__ ) <EOL> def test_execute ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> inst . execute ( ) <EOL> self . assertEqual ( index . oid , '<STR_LIT>' ) <EOL> def test_anti ( self ) : <EOL> from . . deferred import IndexAction <EOL> index = testing . DummyResource ( ) <EOL> index . __oid__ = <NUM_LIT:1> <EOL> inst = self . _makeOne ( index ) <EOL> result = inst . anti ( ) <EOL> self . assertEqual ( result . __class__ , IndexAction ) <EOL> self . assertEqual ( result . index , index ) <EOL> self . assertEqual ( result . index_oid , <NUM_LIT:1> ) <EOL> self . assertEqual ( result . mode , '<STR_LIT>' ) <EOL> self . assertEqual ( result . oid , '<STR_LIT>' ) <EOL> class TestActionsQueue ( unittest . TestCase ) : <EOL> def _makeOne ( self ) : <EOL> from . . deferred import ActionsQueue <EOL> return ActionsQueue ( ) <EOL> def test_extend ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> inst . extend ( [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( inst . actions , [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( inst . gen , <NUM_LIT:1> ) <EOL> def test_len ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> inst . extend ( [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( len ( inst ) , <NUM_LIT:1> ) <EOL> def test_popall_no_actions ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> self . assertEqual ( inst . popall ( ) , None ) <EOL> self . assertEqual ( inst . gen , <NUM_LIT:0> ) <EOL> def test_popall_with_actions ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> inst . actions = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> self . assertEqual ( inst . popall ( ) , [ <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> self . assertEqual ( inst . actions , [ ] ) <EOL> self . assertEqual ( inst . gen , <NUM_LIT:1> ) <EOL> def test__p_resolveConflict_states_have_different_keys ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , None , { '<STR_LIT:a>' : <NUM_LIT:1> } , { '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> ) <EOL> def test__p_resolveConflict_unknown_state_value_change ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , None , { '<STR_LIT:a>' : <NUM_LIT:1> } , { '<STR_LIT:a>' : <NUM_LIT:2> } <EOL> ) <EOL> def test__p_resolveConflict_states_get_optimized ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> old = state ( [ ] ) <EOL> committed = state ( [ a1 , a1 ] ) <EOL> new = state ( [ ] ) <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ a1 ] ) <EOL> def test__p_resolveConflict_action_intersection_states_conflict ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import IndexAction , UnindexAction <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> old = state ( [ a1 ] ) <EOL> committed = state ( [ a2 ] ) <EOL> new = state ( [ ] ) <EOL> self . assertRaises ( <EOL> ConflictError , inst . _p_resolveConflict , old , committed , new ) <EOL> def test__p_resolveConflict_action_intersection_states_resolveable ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> old = state ( [ a1 ] ) <EOL> committed = state ( [ a2 ] ) <EOL> new = state ( [ ] ) <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ ] ) <EOL> def test__p_resolveConflict_both_new_and_commited_remove_same ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> old = state ( [ a1 ] ) <EOL> committed = state ( [ ] ) <EOL> new = state ( [ ] ) <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , <EOL> old , <EOL> committed , <EOL> new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> def test__p_resolveConflict_new_and_commited_remove_different ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> old = state ( [ a1 , a2 ] ) <EOL> committed = state ( [ a1 ] ) <EOL> new = state ( [ a2 ] ) <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ ] ) <EOL> def test__p_resolveConflict_both_new_and_commited_add_same ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> old = state ( [ ] ) <EOL> committed = state ( [ a1 ] ) <EOL> new = state ( [ a1 ] ) <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , <EOL> old , <EOL> committed , <EOL> new <EOL> ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> def test__p_resolveConflict_new_and_commited_add_different ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> old = state ( [ ] ) <EOL> committed = state ( [ a1 ] ) <EOL> new = state ( [ a2 ] ) <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ a1 , a2 ] ) <EOL> def test__p_resolveConflict_with_committed_added_new_not_added ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> old = state ( [ ] ) <EOL> committed = state ( [ a1 ] ) <EOL> new = state ( [ ] ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ a1 ] ) <EOL> def test__p_resolveConflict_with_new_removed ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> old = state ( [ a1 ] ) <EOL> committed = state ( [ a1 , a2 ] ) <EOL> new = state ( [ ] ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ a2 ] ) <EOL> def test__p_resolveConflict_undo_generates_antiactions_for_removed ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> a3 = DummyAction ( <NUM_LIT:3> ) <EOL> old = state ( [ a1 , a2 ] , gen = <NUM_LIT:1> ) <EOL> committed = state ( [ a3 ] , gen = <NUM_LIT:0> ) <EOL> new = state ( [ ] , gen = <NUM_LIT:0> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:3> ) <EOL> actions = result [ '<STR_LIT>' ] <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> self . assertEqual ( actions [ <NUM_LIT:0> ] . oid , <NUM_LIT:1> ) <EOL> self . assertEqual ( actions [ <NUM_LIT:1> ] . oid , <NUM_LIT:2> ) <EOL> self . assertEqual ( actions [ <NUM_LIT:2> ] . oid , <NUM_LIT:3> ) <EOL> self . assertTrue ( actions [ <NUM_LIT:0> ] . _anti ) <EOL> self . assertTrue ( actions [ <NUM_LIT:1> ] . _anti ) <EOL> self . assertFalse ( actions [ <NUM_LIT:2> ] . _anti ) <EOL> def test__p_resolveConflict_undo_no_antiactions_to_generate ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> old = state ( [ a1 , a2 ] , gen = <NUM_LIT:1> ) <EOL> committed = state ( [ a1 , a2 ] , gen = <NUM_LIT:0> ) <EOL> new = state ( [ a1 , a2 ] , gen = <NUM_LIT:0> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> actions = result [ '<STR_LIT>' ] <EOL> self . assertEqual ( actions , [ a1 , a2 ] ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , <NUM_LIT:1> ) <EOL> def test__p_resolveConflict_undo_anti_already_in_new_added ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> , antiresult = a1 ) <EOL> old = state ( [ a2 ] , gen = <NUM_LIT:1> ) <EOL> committed = state ( [ ] , gen = <NUM_LIT:0> ) <EOL> new = state ( [ a1 ] , gen = <NUM_LIT:0> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , old , committed , new <EOL> ) <EOL> def test__p_resolveConflict_undo_anti_already_in_committed ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> inst = self . _makeOne ( ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a2 = DummyAction ( <NUM_LIT:2> , antiresult = a1 ) <EOL> old = state ( [ a2 ] , gen = <NUM_LIT:1> ) <EOL> committed = state ( [ a1 ] , gen = <NUM_LIT:0> ) <EOL> new = state ( [ ] , gen = <NUM_LIT:0> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> self . assertRaises ( <EOL> ConflictError , <EOL> inst . _p_resolveConflict , old , committed , new <EOL> ) <EOL> def test__p_resolveConflict_resolved_returns_higher_generation_number ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> old = state ( [ ] , gen = <NUM_LIT:0> ) <EOL> committed = state ( [ ] , gen = <NUM_LIT:2> ) <EOL> new = state ( [ ] , gen = <NUM_LIT:1> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ ] ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , <NUM_LIT:2> ) <EOL> def test__p_resolveConflict_resolved_returns_agreed_upon_pactive ( self ) : <EOL> inst = self . _makeOne ( ) <EOL> old = state ( [ ] , gen = <NUM_LIT:0> ) <EOL> committed = state ( [ ] , gen = <NUM_LIT:2> ) <EOL> new = state ( [ ] , gen = <NUM_LIT:1> ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> result = inst . _p_resolveConflict ( old , committed , new ) <EOL> self . assertEqual ( len ( logger . messages ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , [ ] ) <EOL> self . assertEqual ( result [ '<STR_LIT>' ] , True ) <EOL> class Test_which_action ( unittest . TestCase ) : <EOL> def _callFUT ( self , a1 , a2 ) : <EOL> from . . deferred import which_action <EOL> return which_action ( a1 , a2 ) <EOL> def test_conflict_I_U ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import IndexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( ConflictError , self . _callFUT , a1 , a2 ) <EOL> def test_conflict_U_I ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import IndexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( ConflictError , self . _callFUT , a1 , a2 ) <EOL> def test_conflict_R_U ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import ReindexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( ConflictError , self . _callFUT , a1 , a2 ) <EOL> def test_conflict_U_R ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import ReindexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertRaises ( ConflictError , self . _callFUT , a1 , a2 ) <EOL> def test_second_R_I ( self ) : <EOL> from . . deferred import ReindexAction , IndexAction <EOL> index = DummyIndex ( ) <EOL> a1 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . _callFUT ( a1 , a2 ) , a2 ) <EOL> def test_first_I_R ( self ) : <EOL> from . . deferred import ReindexAction , IndexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . _callFUT ( a1 , a2 ) , a1 ) <EOL> def test_first_I_I ( self ) : <EOL> from . . deferred import IndexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . assertEqual ( self . _callFUT ( a1 , a2 ) , a1 ) <EOL> class Test_action_intersection ( unittest . TestCase ) : <EOL> def _callFUT ( self , a1 , a2 ) : <EOL> from . . deferred import action_intersection <EOL> return action_intersection ( a1 , a2 ) <EOL> def test_conflict_I_U ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> from . . deferred import IndexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> s1 = set ( [ a1 ] ) <EOL> s2 = set ( [ a2 ] ) <EOL> self . assertRaises ( ConflictError , self . _callFUT , s1 , s2 ) <EOL> def test_index_replaces_reindex ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> s1 = set ( [ a1 ] ) <EOL> s2 = set ( [ a2 ] ) <EOL> result = self . _callFUT ( s1 , s2 ) <EOL> self . assertEqual ( list ( result ) , [ a1 ] ) <EOL> def test_no_matches ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction <EOL> index = DummyIndex ( ) <EOL> a1 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a3 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> s1 = set ( [ a1 , a3 ] ) <EOL> s2 = set ( [ a2 ] ) <EOL> result = self . _callFUT ( s1 , s2 ) <EOL> self . assertEqual ( sorted ( list ( result ) ) , [ ] ) <EOL> def test_only_some_matches ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction <EOL> index = DummyIndex ( ) <EOL> a1_a = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a1_b = ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) <EOL> s1 = set ( [ a1_a , a2 ] ) <EOL> s2 = set ( [ a1_b ] ) <EOL> result = self . _callFUT ( s1 , s2 ) <EOL> self . assertEqual ( sorted ( list ( result ) ) , [ a1_a ] ) <EOL> class Test_commit ( unittest . TestCase ) : <EOL> def _makeOne ( self , tries , meth ) : <EOL> from . . deferred import commit <EOL> return commit ( tries ) ( meth ) <EOL> def test_gardenpath ( self ) : <EOL> ap = DummyActionProcessor ( None ) <EOL> def fakemethod ( ap ) : <EOL> ap . called += <NUM_LIT:1> <EOL> inst = self . _makeOne ( <NUM_LIT:3> , fakemethod ) <EOL> inst ( ap ) <EOL> self . assertEqual ( ap . synced , <NUM_LIT:1> ) <EOL> self . assertEqual ( ap . called , <NUM_LIT:1> ) <EOL> self . assertEqual ( ap . transaction . begun , <NUM_LIT:1> ) <EOL> self . assertEqual ( ap . transaction . committed , <NUM_LIT:1> ) <EOL> def test_conflict_overflow ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> ap = DummyActionProcessor ( <EOL> None , [ ConflictError , ConflictError , ConflictError ] <EOL> ) <EOL> def fakemethod ( ap ) : <EOL> ap . called += <NUM_LIT:1> <EOL> inst = self . _makeOne ( <NUM_LIT:3> , fakemethod ) <EOL> self . assertRaises ( ConflictError , inst , ap ) <EOL> self . assertEqual ( ap . synced , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . called , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . transaction . begun , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . transaction . committed , <NUM_LIT:0> ) <EOL> self . assertEqual ( ap . transaction . aborted , <NUM_LIT:3> ) <EOL> def test_conflicts_but_success ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> ap = DummyActionProcessor ( <EOL> None , <EOL> [ ConflictError , ConflictError ] <EOL> ) <EOL> def fakemethod ( ap ) : <EOL> ap . called += <NUM_LIT:1> <EOL> inst = self . _makeOne ( <NUM_LIT:3> , fakemethod ) <EOL> inst ( ap ) <EOL> self . assertEqual ( ap . synced , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . called , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . transaction . begun , <NUM_LIT:3> ) <EOL> self . assertEqual ( ap . transaction . committed , <NUM_LIT:1> ) <EOL> self . assertEqual ( ap . transaction . aborted , <NUM_LIT:2> ) <EOL> class TestBasicActionProcessor ( unittest . TestCase ) : <EOL> def _makeOne ( self , context ) : <EOL> from . . deferred import BasicActionProcessor <EOL> return BasicActionProcessor ( context ) <EOL> def test_get_root_no_jar ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> context . _p_jar = None <EOL> inst = self . _makeOne ( context ) <EOL> self . assertTrue ( inst . get_root ( ) is None ) <EOL> def test_get_root_with_jar ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> context . _p_jar = DummyJar ( '<STR_LIT:root>' ) <EOL> inst = self . _makeOne ( context ) <EOL> self . assertEqual ( inst . get_root ( ) , '<STR_LIT:root>' ) <EOL> def test_get_queue_no_root ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> context . _p_jar = None <EOL> inst = self . _makeOne ( context ) <EOL> self . assertTrue ( inst . get_queue ( ) is None ) <EOL> def test_get_queue_with_root ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> context . _p_jar = DummyJar ( { inst . queue_name : '<STR_LIT>' } ) <EOL> self . assertEqual ( inst . get_queue ( ) , '<STR_LIT>' ) <EOL> def test_active_True ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> queue = DummyQueue ( ) <EOL> queue . pactive = True <EOL> context . _p_jar = DummyJar ( { inst . queue_name : queue } ) <EOL> self . assertTrue ( inst . active ( ) ) <EOL> def test_active_False_no_queue ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> context . _p_jar = None <EOL> inst = self . _makeOne ( context ) <EOL> self . assertFalse ( inst . active ( ) ) <EOL> def test_active_False_processor_not_active ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> queue = DummyQueue ( ) <EOL> queue . pactive = False <EOL> inst = self . _makeOne ( context ) <EOL> context . _p_jar = DummyJar ( { inst . queue_name : queue } ) <EOL> self . assertFalse ( inst . active ( ) ) <EOL> def test_engage_queue_already_present ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> queue = DummyQueue ( ) <EOL> inst = self . _makeOne ( context ) <EOL> inst . transaction = DummyTransaction ( ) <EOL> context . _p_jar = DummyJar ( { inst . queue_name : queue } ) <EOL> self . assertEqual ( inst . engage ( ) , None ) <EOL> self . assertTrue ( queue . pactive ) <EOL> def test_engage_queue_missing_context_has_no_jar ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> inst . transaction = DummyTransaction ( ) <EOL> context . _p_jar = DummyJar ( None ) <EOL> self . assertRaises ( RuntimeError , inst . engage ) <EOL> def test_engage_queue_added ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> root = { } <EOL> context . _p_jar = DummyJar ( root ) <EOL> self . assertEqual ( inst . engage ( ) , None ) <EOL> queue = root [ inst . queue_name ] <EOL> self . assertTrue ( transaction . committed ) <EOL> self . assertTrue ( queue . pactive ) <EOL> def test_disengage_no_queue ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> context . _p_jar = None <EOL> self . assertRaises ( RuntimeError , inst . disengage ) <EOL> def test_disengage ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> queue = DummyQueue ( ) <EOL> root = { inst . queue_name : queue } <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> context . _p_jar = DummyJar ( root ) <EOL> inst . disengage ( ) <EOL> self . assertEqual ( queue . pactive , False ) <EOL> self . assertTrue ( transaction . committed ) <EOL> def test_add_not_engaged ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> context . _p_jar = None <EOL> inst = self . _makeOne ( context ) <EOL> self . assertRaises ( RuntimeError , inst . add , [ <NUM_LIT:1> ] ) <EOL> def test_add_extends ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> root = { inst . queue_name : [ <NUM_LIT:1> ] } <EOL> context . _p_jar = DummyJar ( root ) <EOL> inst . add ( [ <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( root [ inst . queue_name ] , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_process_gardenpath ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertEqual ( transaction . committed , <NUM_LIT:3> ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_gardenpath_no_actions ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> queue = DummyQueue ( [ ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertEqual ( transaction . committed , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_conflicterror_at_initial_commit ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( [ ConflictError ] ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> inst . engage = lambda * arg , ** kw : False <EOL> inst . disengage = lambda * arg , ** kw : False <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertTrue ( transaction . aborted ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_conflicterror_at_disengage ( self ) : <EOL> from ZODB . POSException import ConflictError <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( [ None , ConflictError ] ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> inst . engage = lambda * arg , ** kw : False <EOL> L = [ ConflictError ] <EOL> def disengage ( * arg , ** kw ) : <EOL> if L : <EOL> raise L . pop ( <NUM_LIT:0> ) <EOL> inst . disengage = disengage <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_resource_not_found_at_execute ( self ) : <EOL> from . . deferred import ResourceNotFound <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> inst . engage = lambda * arg , ** kw : False <EOL> inst . disengage = lambda * arg , ** kw : False <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a1 . raises = ResourceNotFound ( <NUM_LIT:1> ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertFalse ( a1 . executed ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertFalse ( transaction . committed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_execute_raises_Break ( self ) : <EOL> from . . deferred import Break <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> inst . engage = lambda * arg , ** kw : False <EOL> inst . disengage = lambda * arg , ** kw : False <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a1 . raises = Break ( <NUM_LIT:1> ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertFalse ( a1 . executed ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertFalse ( transaction . committed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test_process_execute_raises_user_error ( self ) : <EOL> context = testing . DummyResource ( ) <EOL> inst = self . _makeOne ( context ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> inst . engage = lambda * arg , ** kw : False <EOL> inst . disengage = lambda * arg , ** kw : False <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a1 . raises = ValueError ( ) <EOL> queue = DummyQueue ( [ a1 ] ) <EOL> root = { inst . queue_name : queue } <EOL> jar = DummyJar ( root ) <EOL> context . _p_jar = jar <EOL> inst . process ( once = True ) <EOL> self . assertTrue ( jar . synced ) <EOL> self . assertEqual ( queue . result , [ ] ) <EOL> self . assertFalse ( a1 . executed ) <EOL> self . assertTrue ( transaction . begun ) <EOL> self . assertFalse ( transaction . committed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> class TestIndexActionSavepoint ( unittest . TestCase ) : <EOL> def _makeOne ( self , tm ) : <EOL> from . . deferred import IndexActionSavepoint <EOL> return IndexActionSavepoint ( tm ) <EOL> def test_rollback ( self ) : <EOL> tm = DummyIndexActionTM ( [ <NUM_LIT:1> ] ) <EOL> inst = self . _makeOne ( tm ) <EOL> tm . actions = None <EOL> inst . rollback ( ) <EOL> self . assertEqual ( tm . actions , [ <NUM_LIT:1> ] ) <EOL> class TestIndexActionTM ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . config = testing . setUp ( ) <EOL> def tearDown ( self ) : <EOL> testing . tearDown ( ) <EOL> def _makeOne ( self , index ) : <EOL> from . . deferred import IndexActionTM <EOL> return IndexActionTM ( index ) <EOL> def test_register ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> inst . register ( ) <EOL> self . assertTrue ( transaction . joined ) <EOL> self . assertEqual ( transaction . beforecommit_fn , inst . flush ) <EOL> self . assertEqual ( transaction . beforecommit_args , ( False , ) ) <EOL> self . assertTrue ( inst . registered ) <EOL> def test_register_already_registered ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> transaction = DummyTransaction ( ) <EOL> inst . transaction = transaction <EOL> inst . registered = True <EOL> inst . register ( ) <EOL> self . assertFalse ( transaction . joined ) <EOL> def test_savepoint ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> sp = inst . savepoint ( ) <EOL> self . assertEqual ( sp . tm , inst ) <EOL> def test_tpc_begin ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . tpc_begin ( None ) , None ) <EOL> def test_tpc_finish ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . tpc_finish ( None ) , None ) <EOL> self . assertEqual ( index . _p_action_tm , None ) <EOL> self . assertEqual ( inst . index , None ) <EOL> self . assertFalse ( inst . registered ) <EOL> self . assertEqual ( inst . actions , [ ] ) <EOL> def test_sortKey ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> self . assertEqual ( inst . sortKey ( ) , '<STR_LIT>' ) <EOL> def test_add ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> inst . add ( <NUM_LIT:1> ) <EOL> self . assertEqual ( inst . actions , [ <NUM_LIT:1> ] ) <EOL> def test_flush ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . actions = [ a1 ] <EOL> L = [ ] <EOL> inst . _process = lambda actions , all = None : L . append ( ( actions , all ) ) <EOL> inst . flush ( all = False ) <EOL> self . assertEqual ( L , [ ( [ a1 ] , False ) ] ) <EOL> def test_flush_no_actions ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> L = [ ] <EOL> inst . _process = lambda actions , all = None : L . append ( ( actions , all ) ) <EOL> inst . flush ( all = False ) <EOL> self . assertEqual ( L , [ ] ) <EOL> def test__process_all_True ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_no_action_processor ( self ) : <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] , all = False ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_no_action_processor_force_deferred ( self ) : <EOL> self . config . registry . settings [ <EOL> '<STR_LIT>' ] = True <EOL> index = DummyIndex ( ) <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] , all = False ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_inactive_action_processor ( self ) : <EOL> from substanced . interfaces import IIndexingActionProcessor <EOL> from zope . interface import Interface <EOL> self . config . registry . registerAdapter ( <EOL> DummyActionProcessor , ( Interface , ) , IIndexingActionProcessor <EOL> ) <EOL> index = DummyIndex ( ) <EOL> index . active = False <EOL> index . queue = True <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] , all = False ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_inactive_action_processor_force_deferred ( self ) : <EOL> self . config . registry . settings [ <EOL> '<STR_LIT>' ] = True <EOL> from substanced . interfaces import IIndexingActionProcessor <EOL> from zope . interface import Interface <EOL> self . config . registry . registerAdapter ( <EOL> DummyActionProcessor , ( Interface , ) , IIndexingActionProcessor <EOL> ) <EOL> index = DummyIndex ( ) <EOL> index . _p_jar = DummyJar ( None ) <EOL> index . active = False <EOL> index . queue = True <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] , all = False ) <EOL> self . assertFalse ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_force_deferred_no_jar ( self ) : <EOL> self . config . registry . settings [ <EOL> '<STR_LIT>' ] = True <EOL> from substanced . interfaces import IIndexingActionProcessor <EOL> from zope . interface import Interface <EOL> self . config . registry . registerAdapter ( <EOL> DummyActionProcessor , ( Interface , ) , IIndexingActionProcessor <EOL> ) <EOL> index = DummyIndex ( ) <EOL> index . _p_jar = DummyJar ( None ) <EOL> index . active = False <EOL> index . queue = None <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> inst . _process ( [ a1 ] , all = False ) <EOL> self . assertTrue ( a1 . executed ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> def test__process_all_False_active_action_processor ( self ) : <EOL> from substanced . interfaces import ( <EOL> IIndexingActionProcessor , <EOL> MODE_DEFERRED , <EOL> MODE_ATCOMMIT , <EOL> ) <EOL> from zope . interface import Interface <EOL> self . config . registry . registerAdapter ( <EOL> DummyActionProcessor , ( Interface , ) , IIndexingActionProcessor <EOL> ) <EOL> index = DummyIndex ( ) <EOL> index . active = True <EOL> index . queue = True <EOL> inst = self . _makeOne ( index ) <EOL> logger = DummyLogger ( ) <EOL> inst . logger = logger <EOL> a1 = DummyAction ( <NUM_LIT:1> ) <EOL> a1 . mode = MODE_DEFERRED <EOL> a2 = DummyAction ( <NUM_LIT:2> ) <EOL> a2 . mode = MODE_ATCOMMIT <EOL> inst . _process ( [ a1 , a2 ] , all = False ) <EOL> self . assertFalse ( a1 . executed ) <EOL> self . assertTrue ( a2 . executed ) <EOL> self . assertEqual ( index . added , [ a1 ] ) <EOL> self . assertEqual ( <EOL> logger . messages , <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> ) <EOL> class Test_optimize_actions ( unittest . TestCase ) : <EOL> def _callFUT ( self , actions ) : <EOL> from . . deferred import optimize_actions <EOL> return optimize_actions ( actions ) <EOL> def test_donothing ( self ) : <EOL> from . . deferred import IndexAction , UnindexAction <EOL> index = DummyIndex ( ) <EOL> actions = [ IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> result = self . _callFUT ( actions ) <EOL> self . assertEqual ( result , [ ] ) <EOL> def test_doadd ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction <EOL> index = DummyIndex ( ) <EOL> actions = [ IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ReindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> result = self . _callFUT ( actions ) <EOL> self . assertEqual ( result , [ actions [ <NUM_LIT:0> ] ] ) <EOL> def test_dochange ( self ) : <EOL> from . . deferred import IndexAction , UnindexAction , ReindexAction <EOL> index = DummyIndex ( ) <EOL> actions = [ UnindexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> result = self . _callFUT ( actions ) <EOL> self . assertEqual ( len ( result ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . __class__ , ReindexAction ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . index , index ) <EOL> self . assertEqual ( result [ <NUM_LIT:0> ] . oid , '<STR_LIT>' ) <EOL> def test_dodefault ( self ) : <EOL> from . . deferred import IndexAction <EOL> index = DummyIndex ( ) <EOL> actions = [ IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> IndexAction ( index , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> result = self . _callFUT ( actions ) <EOL> self . assertEqual ( result , [ actions [ - <NUM_LIT:1> ] ] ) <EOL> def test_sorting ( self ) : <EOL> from . . deferred import IndexAction , ReindexAction , UnindexAction <EOL> index1 = DummyIndex ( ) <EOL> index1 . __name__ = '<STR_LIT>' <EOL> index2 = DummyIndex ( ) <EOL> index2 . __oid__ = <NUM_LIT:2> <EOL> index2 . __name__ = '<STR_LIT>' <EOL> a1 = IndexAction ( index2 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a2 = ReindexAction ( index1 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a3 = IndexAction ( index2 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a4 = IndexAction ( index2 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a5 = UnindexAction ( index1 , '<STR_LIT>' , '<STR_LIT>' ) <EOL> actions = [ a1 , a2 , a3 , a4 , a5 ] <EOL> result = self . _callFUT ( actions ) <EOL> self . assertEqual ( result , [ a5 , a1 , a3 , a2 , a4 ] ) <EOL> class DummyIndexActionTM ( object ) : <EOL> def __init__ ( self , actions ) : <EOL> self . actions = actions <EOL> class DummyIndex ( object ) : <EOL> __oid__ = <NUM_LIT:1> <EOL> oid = None <EOL> def index_doc ( self , oid , resource ) : <EOL> self . oid = oid <EOL> self . resource = resource <EOL> reindex_doc = index_doc <EOL> def unindex_doc ( self , oid ) : <EOL> self . oid = oid <EOL> class DummyLogger ( object ) : <EOL> def __init__ ( self ) : <EOL> self . messages = [ ] <EOL> def info ( self , msg ) : <EOL> self . messages . append ( msg ) <EOL> debug = info <EOL> error = info <EOL> class DummyJar ( object ) : <EOL> def __init__ ( self , result ) : <EOL> self . result = result <EOL> def root ( self ) : <EOL> return self . result <EOL> def sync ( self ) : <EOL> self . synced = True <EOL> class DummyTransaction ( object ) : <EOL> joined = False <EOL> def __init__ ( self , raises = None ) : <EOL> if raises is None : <EOL> raises = [ ] <EOL> self . raises = raises <EOL> self . committed = <NUM_LIT:0> <EOL> self . aborted = <NUM_LIT:0> <EOL> self . begun = <NUM_LIT:0> <EOL> def begin ( self ) : <EOL> self . begun += <NUM_LIT:1> <EOL> def commit ( self ) : <EOL> if self . raises : <EOL> result = self . raises . pop ( <NUM_LIT:0> ) <EOL> if result is not None : <EOL> raise result <EOL> self . committed += <NUM_LIT:1> <EOL> def abort ( self ) : <EOL> self . aborted += <NUM_LIT:1> <EOL> def note ( self , msg ) : <EOL> self . _note = msg <EOL> def get ( self ) : <EOL> return self <EOL> def join ( self , tm ) : <EOL> self . joined = tm <EOL> def addBeforeCommitHook ( self , fn , args ) : <EOL> self . beforecommit_fn = fn <EOL> self . beforecommit_args = args <EOL> from substanced . _compat import total_ordering <EOL> @ total_ordering <EOL> class DummyAction ( object ) : <EOL> index = testing . DummyResource ( ) <EOL> index . __oid__ = <NUM_LIT:1> <EOL> executed = False <EOL> mode = None <EOL> def __init__ ( self , oid , index_oid = <NUM_LIT:1> , position = <NUM_LIT:1> , raises = None , anti = False , <EOL> antiresult = None ) : <EOL> self . oid = oid <EOL> self . index_oid = index_oid <EOL> self . position = position <EOL> self . raises = raises <EOL> self . _anti = anti <EOL> self . antiresult = antiresult <EOL> def execute ( self ) : <EOL> if self . raises : <EOL> raise self . raises <EOL> self . executed = True <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . oid <EOL> def __lt__ ( self , other ) : <EOL> return self . oid < other . oid <EOL> def anti ( self ) : <EOL> if self . antiresult : <EOL> return self . antiresult <EOL> return DummyAction ( self . oid , anti = True ) <EOL> class DummyQueue ( object ) : <EOL> def __init__ ( self , result = ( ) ) : <EOL> self . result = result <EOL> def popall ( self ) : <EOL> result = self . result [ : ] <EOL> self . result = [ ] <EOL> return result <EOL> def __len__ ( self ) : <EOL> return len ( self . result ) <EOL> class DummyActionProcessor ( object ) : <EOL> def __init__ ( self , context , commit_raises = None ) : <EOL> self . context = context <EOL> if commit_raises is None : <EOL> commit_raises = [ ] <EOL> self . transaction = DummyTransaction ( commit_raises ) <EOL> self . synced = <NUM_LIT:0> <EOL> self . called = <NUM_LIT:0> <EOL> def active ( self ) : <EOL> return self . context . active <EOL> def sync ( self ) : <EOL> self . synced += <NUM_LIT:1> <EOL> def add ( self , actions ) : <EOL> self . context . added = actions <EOL> def get_queue ( self ) : <EOL> return self . context . queue <EOL> class DummyObjectmap ( object ) : <EOL> def __init__ ( self , result ) : <EOL> self . result = result <EOL> def object_for ( self , oid ) : <EOL> return self . result <EOL> def state ( actions , gen = <NUM_LIT:0> , pactive = True , undo = False ) : <EOL> return dict ( locals ( ) ) </s>
