11from twisted .trial import unittest
22from twisted .python import log
33from mock import MagicMock
4+ import mock
45
5- from market .listeners import MessageListenerImpl
6+ from market .listeners import MessageListenerImpl , BroadcastListenerImpl
67from protos .objects import PlaintextMessage
78
89class MarketListenersTest (unittest .TestCase ):
@@ -47,6 +48,7 @@ def _create_valid_message_json(handle):
4748 return message
4849
4950 def test_MarketListeners_notify_without_handle_success (self ):
51+ '''MessageListenerImpl correctly notify without handle.'''
5052 p = self ._create_valid_plaintext_message ('' )
5153 signature = 'test_signature'
5254 l = MessageListenerImpl (self .ws , self .db )
@@ -61,6 +63,7 @@ def test_MarketListeners_notify_without_handle_success(self):
6163 self .ws .push .assert_called_with (self ._create_valid_message_json ('' ))
6264
6365 def test_MarketListeners_notify_with_handle_success (self ):
66+ '''MessageListenerImpl correctly notify with handle.'''
6467 p = self ._create_valid_plaintext_message ('test_handle' )
6568 signature = 'test_signature'
6669 l = MessageListenerImpl (self .ws , self .db )
@@ -74,3 +77,21 @@ def test_MarketListeners_notify_with_handle_success(self):
7477 'test_avatar_hash' ,
7578 signature , False )
7679 self .ws .push .assert_called_with (self ._create_valid_message_json ('test_handle' ))
80+
81+ def test_MarketListeners_save_message_exception (self ):
82+ p = self ._create_valid_plaintext_message ('test_handle' )
83+ signature = 'test_signature'
84+ l = MessageListenerImpl (self .ws , self .db )
85+ self .db .messages .save_message .side_effect = Exception ("test_exception" )
86+ l .notify (p , signature )
87+ self .assertEqual ('[ERROR] Market.Listener.notify Exception: test_exception' , self .catcher [0 ]['message' ][0 ])
88+
89+ def test_MarketListeners_broadcast_notify_success (self ):
90+ '''BroadcastListenerImpl correctly notifies.'''
91+ b = BroadcastListenerImpl (self .ws , self .db )
92+ b .notify ('123' , 'test_message' )
93+ self .db .broadcasts .save_broadcast .assert_called_once_with (mock .ANY ,
94+ '313233' , '' ,
95+ 'test_message' ,
96+ mock .ANY , '' )
97+ self .ws .push .assert_called_once_with (mock .ANY )
0 commit comments