<s> from mock import patch <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from rapidsms . contrib . echo . handlers . echo import EchoHandler <EOL> from rapidsms . errors import MessageSendingError <EOL> from rapidsms . models import Connection <EOL> from rapidsms . tests import harness <EOL> from rapidsms . tests . harness . backend import RaisesBackend , FailedIdentitiesBackend <EOL> from rapidsms . router . db import DatabaseRouter <EOL> from rapidsms . router . db . models import Message , Transmission <EOL> from rapidsms . router . db . tasks import send_transmissions , receive_async <EOL> class MessageStatusTest ( harness . CustomRouterMixin , TestCase ) : <EOL> router_class = '<STR_LIT>' <EOL> def test_inbound_message_status_error ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:I>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:E>' ) <EOL> self . assertEqual ( '<STR_LIT:E>' , dbm . set_status ( ) ) <EOL> def test_inbound_message_status_queued ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:I>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , dbm . set_status ( ) ) <EOL> def test_inbound_message_status_received ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:I>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:R>' ) <EOL> self . assertEqual ( '<STR_LIT:R>' , dbm . set_status ( ) ) <EOL> def test_outbound_message_status_error ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:E>' ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:S>' ) <EOL> self . assertEqual ( '<STR_LIT:E>' , dbm . set_status ( ) ) <EOL> def test_outbound_message_status_processing ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT>' ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:S>' ) <EOL> self . assertEqual ( '<STR_LIT:P>' , dbm . set_status ( ) ) <EOL> def test_outbound_message_status_sent ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:S>' ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:D>' ) <EOL> self . assertEqual ( '<STR_LIT:S>' , dbm . set_status ( ) ) <EOL> def test_outbound_message_status_delivered ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:D>' ) <EOL> dbm . transmissions . create ( connection = self . create_connection ( ) , <EOL> status = '<STR_LIT:D>' ) <EOL> self . assertEqual ( '<STR_LIT:D>' , dbm . set_status ( ) ) <EOL> class DatabaseRouterReceiveTest ( harness . DatabaseBackendMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> router_class = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . conn1 = self . lookup_connections ( backend = '<STR_LIT>' , identities = [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> self . conn2 = self . lookup_connections ( backend = '<STR_LIT>' , identities = [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> def test_queue_status ( self ) : <EOL> """<STR_LIT>""" <EOL> router = DatabaseRouter ( ) <EOL> dbm = router . queue_message ( "<STR_LIT:I>" , [ self . conn1 ] , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( "<STR_LIT>" , dbm . status ) <EOL> transmission = dbm . transmissions . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT>" , transmission . status ) <EOL> def test_queue_single_connection ( self ) : <EOL> """<STR_LIT>""" <EOL> connections = [ self . conn1 ] <EOL> router = DatabaseRouter ( ) <EOL> dbm = router . queue_message ( "<STR_LIT:I>" , connections , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( <NUM_LIT:1> , dbm . transmissions . count ( ) ) <EOL> def test_queue_multi_connections ( self ) : <EOL> """<STR_LIT>""" <EOL> connections = [ self . conn1 , self . conn2 ] <EOL> router = DatabaseRouter ( ) <EOL> dbm = router . queue_message ( "<STR_LIT:I>" , connections , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( <NUM_LIT:2> , dbm . transmissions . count ( ) ) <EOL> def test_queue_queryset_connections ( self ) : <EOL> """<STR_LIT>""" <EOL> connections = Connection . objects . all ( ) <EOL> router = DatabaseRouter ( ) <EOL> dbm = router . queue_message ( "<STR_LIT:I>" , connections , "<STR_LIT:foo>" ) <EOL> self . assertEqual ( <NUM_LIT:2> , dbm . transmissions . count ( ) ) <EOL> def test_receive ( self ) : <EOL> """<STR_LIT>""" <EOL> self . receive ( text = "<STR_LIT:foo>" , connection = self . conn1 ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT:foo>" , dbm . text ) <EOL> self . assertEqual ( "<STR_LIT:I>" , dbm . direction ) <EOL> self . assertEqual ( <NUM_LIT:1> , dbm . transmissions . count ( ) ) <EOL> def test_receive_status ( self ) : <EOL> """<STR_LIT>""" <EOL> self . receive ( text = "<STR_LIT:foo>" , connection = self . conn1 ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT:R>" , dbm . status ) <EOL> transmission = dbm . transmissions . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT:R>" , transmission . status ) <EOL> @ patch . object ( EchoHandler , '<STR_LIT>' ) <EOL> def test_receive_status_with_error ( self , mock_handle ) : <EOL> """<STR_LIT>""" <EOL> mock_handle . side_effect = Exception <EOL> self . receive ( text = "<STR_LIT>" , connection = self . conn1 ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT:E>" , dbm . status ) <EOL> transmission = dbm . transmissions . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT:E>" , transmission . status ) <EOL> def test_receive_message_id ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = self . receive ( text = "<STR_LIT:foo>" , connection = self . conn1 ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( msg . id , dbm . pk ) <EOL> def test_receive_external_id ( self ) : <EOL> """<STR_LIT>""" <EOL> fields = { '<STR_LIT>' : '<STR_LIT>' } <EOL> msg = self . receive ( text = "<STR_LIT:foo>" , connection = self . conn1 , fields = fields ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( "<STR_LIT>" , msg . fields [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( "<STR_LIT>" , dbm . external_id ) <EOL> def test_create_message_from_dbm ( self ) : <EOL> """<STR_LIT>""" <EOL> router = DatabaseRouter ( ) <EOL> dbm = router . queue_message ( direction = '<STR_LIT:I>' , text = "<STR_LIT:foo>" , <EOL> connections = [ self . conn1 ] ) <EOL> dbm2 = router . create_message_from_dbm ( dbm , { '<STR_LIT:a>' : '<STR_LIT:b>' } ) <EOL> self . assertEqual ( { '<STR_LIT:a>' : '<STR_LIT:b>' } , dbm2 . fields ) <EOL> def test_in_response_to ( self ) : <EOL> """<STR_LIT>""" <EOL> connection = self . lookup_connections ( [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> self . receive ( text = "<STR_LIT>" , connection = connection ) <EOL> message = Message . objects . get ( direction = '<STR_LIT:I>' ) <EOL> response = Message . objects . get ( direction = '<STR_LIT:O>' ) <EOL> self . assertEqual ( message . pk , response . in_response_to . pk ) <EOL> def test_in_response_to_external_id ( self ) : <EOL> """<STR_LIT>""" <EOL> connection = self . lookup_connections ( [ '<STR_LIT>' ] ) [ <NUM_LIT:0> ] <EOL> msg = self . receive ( "<STR_LIT:test>" , connection , <EOL> fields = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> backend_msg = self . sent_messages [ <NUM_LIT:0> ] <EOL> self . assertEqual ( msg . fields [ '<STR_LIT>' ] , backend_msg . external_id ) <EOL> def test_receive_async_fields ( self ) : <EOL> """<STR_LIT>""" <EOL> with patch . object ( receive_async , '<STR_LIT>' ) as mock_method : <EOL> connections = self . lookup_connections ( [ '<STR_LIT>' ] ) <EOL> msg = self . receive ( "<STR_LIT:test>" , connections [ <NUM_LIT:0> ] , fields = { '<STR_LIT:a>' : '<STR_LIT:b>' } ) <EOL> mock_method . assert_called_once_with ( message_id = msg . id , <EOL> fields = msg . fields ) <EOL> class DatabaseRouterSendTest ( harness . DatabaseBackendMixin , TestCase ) : <EOL> """<STR_LIT>""" <EOL> router_class = '<STR_LIT>' <EOL> def create_trans ( self , s1 = '<STR_LIT>' , s2 = '<STR_LIT>' , backend = None ) : <EOL> backend = backend or self . backend <EOL> Connection . objects . bulk_create ( ( <EOL> Connection ( identity = '<STR_LIT>' , backend = backend ) , <EOL> Connection ( identity = '<STR_LIT>' , backend = backend ) , <EOL> Connection ( identity = '<STR_LIT>' , backend = backend ) , <EOL> Connection ( identity = '<STR_LIT>' , backend = backend ) , <EOL> ) ) <EOL> dbm = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> for connection in Connection . objects . order_by ( '<STR_LIT:id>' ) [ : <NUM_LIT:2> ] : <EOL> dbm . transmissions . create ( connection = connection , status = s1 ) <EOL> for connection in Connection . objects . order_by ( '<STR_LIT:id>' ) [ <NUM_LIT:2> : ] : <EOL> dbm . transmissions . create ( connection = connection , status = s2 ) <EOL> ids = dbm . transmissions . order_by ( '<STR_LIT:id>' ) . values_list ( '<STR_LIT:id>' , flat = True ) <EOL> trans1 = dbm . transmissions . filter ( id__in = ids [ : <NUM_LIT:2> ] ) <EOL> trans2 = dbm . transmissions . filter ( id__in = ids [ <NUM_LIT:2> : ] ) <EOL> return dbm , trans1 , trans2 <EOL> def create_many_transmissions ( self , num , backend = None ) : <EOL> backend = backend or self . backend <EOL> message = Message . objects . create ( text = "<STR_LIT:test>" , direction = "<STR_LIT:O>" ) <EOL> for i in range ( num ) : <EOL> connection = Connection . objects . create ( identity = "<STR_LIT>" % i , <EOL> backend = backend ) <EOL> message . transmissions . create ( connection = connection , status = "<STR_LIT>" ) <EOL> def test_send_successful_status ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT>' , s2 = '<STR_LIT>' ) <EOL> send_transmissions ( self . backend . pk , dbm . pk , t1 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> status = t1 . values_list ( '<STR_LIT:status>' , flat = True ) . distinct ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT:S>' , status ) <EOL> def test_send_successful_message_status ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT:S>' , s2 = '<STR_LIT>' ) <EOL> send_transmissions ( self . backend . pk , dbm . pk , t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT:S>' , dbm . status ) <EOL> def test_send_successful_message_status_previous_error ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT:E>' , s2 = '<STR_LIT>' ) <EOL> send_transmissions ( self . backend . pk , dbm . pk , t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> dbm = Message . objects . all ( ) [ <NUM_LIT:0> ] <EOL> self . assertEqual ( '<STR_LIT:E>' , dbm . status ) <EOL> def test_group_transmissions ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT>' , s2 = '<STR_LIT>' ) <EOL> router = DatabaseRouter ( ) <EOL> trans = list ( router . group_transmissions ( Transmission . objects . all ( ) , <EOL> batch_size = <NUM_LIT:2> ) ) <EOL> _ , batch1 = trans [ <NUM_LIT:0> ] <EOL> _ , batch2 = trans [ <NUM_LIT:1> ] <EOL> self . assertEqual ( list ( batch1 . values_list ( '<STR_LIT:id>' , flat = True ) ) , <EOL> list ( t1 . values_list ( '<STR_LIT:id>' , flat = True ) ) ) <EOL> self . assertEqual ( list ( batch2 . values_list ( '<STR_LIT:id>' , flat = True ) ) , <EOL> list ( t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) ) <EOL> def test_group_transmissions_different_backends ( self ) : <EOL> """<STR_LIT>""" <EOL> other_backend = self . create_backend ( data = { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . create_many_transmissions ( <NUM_LIT:7> ) <EOL> self . create_many_transmissions ( <NUM_LIT:6> , backend = other_backend ) <EOL> router = DatabaseRouter ( ) <EOL> result = list ( router . group_transmissions ( Transmission . objects . all ( ) , <EOL> batch_size = <NUM_LIT:4> ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( result [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( result [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:4> , len ( result [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result [ <NUM_LIT:3> ] [ <NUM_LIT:1> ] ) ) <EOL> def test_group_transmissions_default_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> self . create_many_transmissions ( <NUM_LIT> ) <EOL> router = DatabaseRouter ( ) <EOL> result = list ( router . group_transmissions ( Transmission . objects . all ( ) ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:200> , len ( result [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> def test_group_transmissions_custom_default_batch_size ( self ) : <EOL> """<STR_LIT>""" <EOL> self . create_many_transmissions ( <NUM_LIT:3> ) <EOL> router = DatabaseRouter ( ) <EOL> with self . settings ( DB_ROUTER_DEFAULT_BATCH_SIZE = <NUM_LIT:2> ) : <EOL> transmissions = Transmission . objects . all ( ) <EOL> result = list ( router . group_transmissions ( transmissions ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( result [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> def test_send_doesnt_send_already_sent_transmissions ( self ) : <EOL> """<STR_LIT>""" <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT:S>' , s2 = '<STR_LIT>' ) <EOL> both_transmisssion_sets = list ( t1 . values_list ( '<STR_LIT:id>' , flat = True ) ) + list ( t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> send_transmissions ( self . backend . pk , dbm . pk , both_transmisssion_sets ) <EOL> dbm = Message . objects . get ( ) <EOL> self . assertEqual ( '<STR_LIT:S>' , dbm . status ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( self . sent_messages ) ) <EOL> self . assertEqual ( <EOL> set ( [ m . identity for m in self . sent_messages ] ) , <EOL> set ( [ t . connection . identity for t in t2 ] ) <EOL> ) <EOL> def test_all_transmissions_set_to_E_if_backend_sending_error ( self ) : <EOL> error_backend = RaisesBackend ( self . get_router ( ) , '<STR_LIT>' ) <EOL> self . backends [ '<STR_LIT>' ] = { '<STR_LIT>' : RaisesBackend } <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT>' , s2 = '<STR_LIT>' , backend = error_backend . model ) <EOL> both_transmisssion_sets = list ( t1 . values_list ( '<STR_LIT:id>' , flat = True ) ) + list ( t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> with override_settings ( INSTALLED_BACKENDS = self . backends ) : <EOL> with self . assertRaises ( MessageSendingError ) : <EOL> send_transmissions ( error_backend . model . pk , dbm . pk , both_transmisssion_sets ) <EOL> errored = Transmission . objects . filter ( status = '<STR_LIT:E>' ) <EOL> self . assertEqual ( <NUM_LIT:4> , errored . count ( ) ) <EOL> def test_only_failed_transmissions_set_to_E ( self ) : <EOL> error_backend = FailedIdentitiesBackend ( self . get_router ( ) , '<STR_LIT>' ) <EOL> self . backends [ '<STR_LIT>' ] = { '<STR_LIT>' : FailedIdentitiesBackend } <EOL> dbm , t1 , t2 = self . create_trans ( s1 = '<STR_LIT>' , s2 = '<STR_LIT>' , backend = error_backend . model ) <EOL> both_transmisssion_sets = list ( t1 . values_list ( '<STR_LIT:id>' , flat = True ) ) + list ( t2 . values_list ( '<STR_LIT:id>' , flat = True ) ) <EOL> with override_settings ( INSTALLED_BACKENDS = self . backends ) : <EOL> with self . assertRaises ( MessageSendingError ) : <EOL> send_transmissions ( error_backend . model . pk , dbm . pk , both_transmisssion_sets ) <EOL> errored = Transmission . objects . filter ( status = '<STR_LIT:E>' ) <EOL> self . assertEqual ( <NUM_LIT:1> , errored . count ( ) ) </s>
