66from unittest import mock
77
88from murfey .cli import repost_failed_calls
9+ from murfey .util .config import security_from_file
10+ from murfey .util .db import Tilt
911
1012
1113@mock .patch ("murfey.cli.repost_failed_calls.PikaTransport" )
@@ -104,101 +106,124 @@ def test_handle_dlq_messages(mock_transport, tmp_path):
104106 mock_transport ().disconnect .assert_called_once ()
105107
106108
107- @mock .patch ("murfey.cli.repost_failed_calls.requests" )
108- def test_handle_failed_posts (mock_requests , tmp_path ):
109+ def test_handle_failed_posts (tmp_path ):
109110 """Test that the API is called with any failed client post messages"""
110111 # Create some sample messages
111112 messages_paths_list : list [Path ] = []
112113 messages_dict : dict [str , dict ] = {
113114 "msg1" : {
114- "message" : {"url" : "sample/url" , "json" : {"content" : "msg1" }},
115- },
115+ "message" : {
116+ "router_name" : "workflow.tomo_router" ,
117+ "function_name" : "register_completed_tilt_series" ,
118+ "kwargs" : {"visit_name" : "cm12345-1" , "session_id" : 1 },
119+ "data" : {
120+ "tags" : ["tag" ],
121+ "source" : "source" ,
122+ "tilt_series_lengths" : [10 ],
123+ },
124+ },
125+ }, # normal example
116126 "msg2" : {
117- "message" : {"url" : "sample/url" , "json" : {"content" : "msg2" }},
118- },
127+ "message" : {
128+ "router_name" : "workflow.tomo_router" ,
129+ "function_name" : "register_tilt" ,
130+ "kwargs" : {"visit_name" : "cm12345-1" , "session_id" : 1 },
131+ "data" : {
132+ "tilt_series_tag" : "tag" ,
133+ "source" : "source" ,
134+ "movie_path" : "path" ,
135+ },
136+ },
137+ }, # async example
119138 "msg3" : {
120- "message" : {"content" : "msg3" }, # not a failed client post
139+ "message" : {
140+ "router_name" : "workflow.tomo_router" ,
141+ "function_name" : "register_completed_tilt_series" ,
142+ "data" : {"tags" : ["tag" ]},
143+ }
121144 },
122145 "msg4" : {
123- "header" : {"content" : "msg3" }, # does not have a message
146+ "message" : {"function_name" : "dummy" }, # does not have a router
147+ },
148+ "msg5" : {
149+ "message" : {"router_name" : "workflow" }, # does not have a function
150+ },
151+ "msg6" : {
152+ "message" : {
153+ "router_name" : "workflow" ,
154+ "function_name" : "dummy" ,
155+ }, # function does not exist
124156 },
125157 }
126158 for file_name , message in messages_dict .items ():
127159 messages_paths_list .append (tmp_path / file_name )
128160 with open (tmp_path / file_name , "w" ) as msg_file :
129161 json .dump (message , msg_file )
130162
131- class Response :
132- def __init__ (self , status_code ):
133- self .status_code = status_code
134-
135- mock_requests .post .side_effect = [Response (200 ), Response (300 )]
136-
137- repost_failed_calls .handle_failed_posts (messages_paths_list , "dummy_token" )
163+ mock_db = mock .Mock ()
164+ mock_exec_return = mock .Mock ()
165+ mock_exec_return .all .return_value = []
166+ mock_db .exec .return_value = mock_exec_return
167+ repost_failed_calls .handle_failed_posts (messages_paths_list , mock_db )
138168
139169 # Check the failed posts were resent
140- assert mock_requests .post .call_count == 2
141- mock_requests .post .assert_any_call (
142- "sample/url" ,
143- json = {"content" : "msg1" },
144- headers = {"Authorization" : "Bearer dummy_token" },
145- )
146- mock_requests .post .assert_any_call (
147- "sample/url" ,
148- json = {"content" : "msg2" },
149- headers = {"Authorization" : "Bearer dummy_token" },
170+ assert mock_db .exec .call_count == 3
171+ assert mock_db .exec ().one .call_count == 1
172+ assert mock_db .exec ().all .call_count == 2
173+ assert mock_exec_return .one .call_count == 1
174+ assert mock_exec_return .all .call_count == 2
175+ assert mock_db .commit .call_count == 3
176+ mock_db .add .assert_called_once_with (
177+ Tilt (movie_path = "path" , tilt_series_id = mock .ANY , motion_corrected = False )
150178 )
151179
152180 # Check only the failed post which was successfully reinjected got deleted
153181 assert not (tmp_path / "msg1" ).is_file () # got resent
154- assert (tmp_path / "msg2" ).is_file () # failed reinjection
155- assert (tmp_path / "msg3" ).is_file () # not a failed client post
156- assert (tmp_path / "msg4" ).is_file () # does not have a message
182+ assert not (tmp_path / "msg2" ).is_file () # got resent
183+ assert (tmp_path / "msg3" ).is_file () # failed reinjection
184+ assert (tmp_path / "msg4" ).is_file () # does not have a router
185+ assert (tmp_path / "msg5" ).is_file () # does not have a function
186+ assert (tmp_path / "msg6" ).is_file () # function does not exist
157187
158188
159189@mock .patch ("murfey.cli.repost_failed_calls.dlq_purge" )
160190@mock .patch ("murfey.cli.repost_failed_calls.handle_failed_posts" )
161191@mock .patch ("murfey.cli.repost_failed_calls.handle_dlq_messages" )
162- @mock .patch ("murfey.cli.repost_failed_calls.jwt " )
192+ @mock .patch ("murfey.cli.repost_failed_calls.get_murfey_db_session " )
163193def test_run_repost_failed_calls (
164- mock_jwt ,
194+ mock_db ,
165195 mock_reinject ,
166196 mock_repost ,
167197 mock_purge ,
168198 mock_security_configuration ,
169199):
170- mock_jwt . encode . return_value = "dummy_token "
200+ mock_db . return_value = "db "
171201 mock_purge .return_value = ["/path/to/msg1" ]
172202
173203 config_file = mock_security_configuration
174204 with open (config_file ) as f :
175- security_config = json .load (f )
205+ security_config_dict = json .load (f )
176206
177207 sys .argv = [
178208 "murfey.repost_failed_calls" ,
179209 "--config" ,
180210 str (config_file ),
181- "--username" ,
182- "user" ,
183211 "--dir" ,
184212 "DLQ_dir" ,
185213 ]
186214 repost_failed_calls .run ()
187215
188- mock_jwt .encode .assert_called_with (
189- {"user" : "user" },
190- security_config ["auth_key" ],
191- algorithm = security_config ["auth_algorithm" ],
192- )
216+ security_config_class = security_from_file (config_file )
217+ mock_db .assert_called_with (security_config_class )
193218
194219 mock_purge .assert_called_once_with (
195220 Path ("DLQ_dir" ),
196221 "murfey_feedback" ,
197- Path (security_config ["rabbitmq_credentials" ]),
222+ Path (security_config_dict ["rabbitmq_credentials" ]),
198223 )
199- mock_repost .assert_called_once_with (["/path/to/msg1" ], "dummy_token " )
224+ mock_repost .assert_called_once_with (["/path/to/msg1" ], "db " )
200225 mock_reinject .assert_called_once_with (
201- ["/path/to/msg1" ], Path (security_config ["rabbitmq_credentials" ])
226+ ["/path/to/msg1" ], Path (security_config_dict ["rabbitmq_credentials" ])
202227 )
203228
204229
@@ -218,6 +243,4 @@ def test_repost_failed_calls_exists():
218243 cleaned_help_line = (
219244 stdout_as_string .split ("\n \n " )[0 ].replace ("\n " , "" ).replace (" " , "" )
220245 )
221- assert cleaned_help_line == (
222- "usage:murfey.repost_failed_calls[-h]-cCONFIG-uUSERNAME[-dDIR]"
223- )
246+ assert cleaned_help_line == ("usage:murfey.repost_failed_calls[-h]-cCONFIG[-dDIR]" )
0 commit comments