21
21
import json
22
22
23
23
import mock
24
+ import pytest
24
25
25
26
from conftest import mock_connection_params , MockResponse
26
- from umapi_client import Connection , Action
27
+ from umapi_client import Connection , Action , ServerError
27
28
28
29
29
30
def test_action_create ():
@@ -56,55 +57,75 @@ def test_action_create_two_dofirst():
56
57
'{"a1": "a1 text", "do": [{"com2": {"com2k": "com2v"}}, {"com1": {"com1k": "com1v"}}], "z1": "z1 text"}'
57
58
58
59
59
- def test_execute_single_success ():
60
+ def test_execute_single_success_immediate ():
60
61
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
61
62
mock_post .return_value = MockResponse (200 , {"result" : "success" })
62
63
conn = Connection (** mock_connection_params )
63
64
action = Action (top = "top" ).append (a = "a" )
64
- assert conn .execute_single (action ) == (1 , 1 )
65
+ assert conn .execute_single (action , immediate = True ) == (0 , 1 , 1 )
65
66
66
67
67
- def test_execute_single_dofirst_success ():
68
+ def test_execute_single_success_queued ():
68
69
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
69
70
mock_post .return_value = MockResponse (200 , {"result" : "success" })
70
- conn = Connection (** mock_connection_params )
71
- action = Action (top = "top" ).insert (a = "a" )
72
- assert conn .execute_single (action ) == (1 , 1 )
71
+ conn = Connection (throttle_actions = 2 , ** mock_connection_params )
72
+ action = Action (top = "top" ).append (a = "a" )
73
+ assert conn .execute_single (action ) == (1 , 0 , 0 )
74
+ assert conn .execute_single (action ) == (0 , 2 , 2 )
73
75
74
76
75
- def test_execute_multiple_success ():
77
+ def test_execute_single_error_queued_throttled ():
76
78
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
77
- mock_post .return_value = MockResponse (200 , {"result" : "success" })
78
- conn = Connection (** mock_connection_params )
79
- action0 = Action (top = "top0" ).append (a = "a0" ).append (b = "b" )
80
- action1 = Action (top = "top1" ).append (a = "a1" )
81
- assert conn .execute_multiple ([action0 , action1 ]) == (2 , 2 )
79
+ mock_post .side_effect = [MockResponse (200 , {"result" : "success" }),
80
+ MockResponse (200 , {"result" : "partial" ,
81
+ "completed" : 1 ,
82
+ "notCompleted" : 1 ,
83
+ "errors" : [{"index" : 1 , "step" : 0 ,
84
+ "errorCode" : "test.error" ,
85
+ "message" : "Test error message" }]})]
86
+ conn = Connection (throttle_actions = 2 , throttle_commands = 1 , ** mock_connection_params )
87
+ action = Action (top = "top" ).append (a = "a" ).append (b = "b" ).append (c = "c" ).append (d = "d" )
88
+ assert conn .execute_single (action ) == (0 , 4 , 3 )
89
+ assert action .execution_errors () == [{"command" : {"d" : "d" },
90
+ "errorCode" : "test.error" ,
91
+ "message" : "Test error message" }]
82
92
83
93
84
- def test_execute_multiple_dofirst_success ():
94
+ def test_execute_single_error_immediate_throttled ():
95
+ with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
96
+ mock_post .return_value = MockResponse (200 , {"result" : "partial" ,
97
+ "completed" : 1 ,
98
+ "notCompleted" : 1 ,
99
+ "errors" : [{"index" : 1 , "step" : 0 , "errorCode" : "test" }]})
100
+ conn = Connection (throttle_commands = 2 , ** mock_connection_params )
101
+ action = Action (top = "top0" ).append (a = "a0" ).append (a = "a1" ).append (a = "a2" )
102
+ assert conn .execute_single (action , immediate = True ) == (0 , 2 , 1 )
103
+ assert action .execution_errors () == [{"command" : {"a" : "a2" }, "errorCode" : "test" }]
104
+
105
+
106
+ def test_execute_single_dofirst_success_immediate ():
85
107
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
86
108
mock_post .return_value = MockResponse (200 , {"result" : "success" })
87
109
conn = Connection (** mock_connection_params )
88
- action0 = Action (top = "top0" ).append (a = "a0" ).insert (b = "b" )
89
- action1 = Action (top = "top1" ).append (a = "a1" )
90
- assert conn .execute_multiple ([action0 , action1 ]) == (2 , 2 )
110
+ action = Action (top = "top" ).insert (a = "a" )
111
+ assert conn .execute_single (action , immediate = True ) == (0 , 1 , 1 )
91
112
92
113
93
- def test_execute_single_error ():
114
+ def test_execute_single_error_immediate ():
94
115
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
95
116
mock_post .return_value = MockResponse (200 , {"result" : "error" ,
96
117
"errors" : [{"index" : 0 , "step" : 0 ,
97
118
"errorCode" : "test.error" ,
98
119
"message" : "Test error message" }]})
99
120
conn = Connection (** mock_connection_params )
100
121
action = Action (top = "top" ).append (a = "a" )
101
- assert conn .execute_single (action ) == (1 , 0 )
122
+ assert conn .execute_single (action , immediate = True ) == (0 , 1 , 0 )
102
123
assert action .execution_errors () == [{"command" : {"a" : "a" },
103
124
"errorCode" : "test.error" ,
104
125
"message" : "Test error message" }]
105
126
106
127
107
- def test_execute_single_multi_error ():
128
+ def test_execute_single_multi_error_immediate ():
108
129
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
109
130
mock_post .return_value = MockResponse (200 , {"result" : "error" ,
110
131
"errors" : [{"index" : 0 , "step" : 0 ,
@@ -115,7 +136,7 @@ def test_execute_single_multi_error():
115
136
"message" : "message2" }]})
116
137
conn = Connection (** mock_connection_params )
117
138
action = Action (top = "top" ).append (a = "a" )
118
- assert conn .execute_single (action ) == (1 , 0 )
139
+ assert conn .execute_single (action , immediate = True ) == (0 , 1 , 0 )
119
140
assert action .execution_errors () == [{"command" : {"a" : "a" },
120
141
"errorCode" : "error1" ,
121
142
"message" : "message1" },
@@ -124,20 +145,48 @@ def test_execute_single_multi_error():
124
145
"message" : "message2" }]
125
146
126
147
127
- def test_execute_single_dofirst_error ():
148
+ def test_execute_single_dofirst_error_immediate ():
128
149
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
129
150
mock_post .return_value = MockResponse (200 , {"result" : "error" ,
130
151
"errors" : [{"index" : 0 , "step" : 0 ,
131
152
"errorCode" : "test.error" ,
132
153
"message" : "Test error message" }]})
133
154
conn = Connection (** mock_connection_params )
134
155
action = Action (top = "top" ).insert (a = "a" )
135
- assert conn .execute_single (action ) == (1 , 0 )
156
+ assert conn .execute_single (action , immediate = True ) == (0 , 1 , 0 )
136
157
assert action .execution_errors () == [{"command" : {"a" : "a" },
137
158
"errorCode" : "test.error" ,
138
159
"message" : "Test error message" }]
139
160
140
161
162
+ def test_execute_multiple_success ():
163
+ with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
164
+ mock_post .return_value = MockResponse (200 , {"result" : "success" })
165
+ conn = Connection (** mock_connection_params )
166
+ action0 = Action (top = "top0" ).append (a = "a0" ).append (b = "b" )
167
+ action1 = Action (top = "top1" ).append (a = "a1" )
168
+ assert conn .execute_multiple ([action0 , action1 ]) == (0 , 2 , 2 )
169
+
170
+
171
+ def test_execute_multiple_success_queued ():
172
+ with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
173
+ mock_post .return_value = MockResponse (200 , {"result" : "success" })
174
+ conn = Connection (** mock_connection_params )
175
+ action0 = Action (top = "top0" ).append (a = "a0" ).append (b = "b" )
176
+ action1 = Action (top = "top1" ).append (a = "a1" )
177
+ assert conn .execute_multiple ([action0 , action1 ], immediate = False ) == (2 , 0 , 0 )
178
+ assert conn .execute_queued () == (0 , 2 , 2 )
179
+
180
+
181
+ def test_execute_multiple_dofirst_success ():
182
+ with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
183
+ mock_post .return_value = MockResponse (200 , {"result" : "success" })
184
+ conn = Connection (** mock_connection_params )
185
+ action0 = Action (top = "top0" ).append (a = "a0" ).insert (b = "b" )
186
+ action1 = Action (top = "top1" ).append (a = "a1" )
187
+ assert conn .execute_multiple ([action0 , action1 ]) == (0 , 2 , 2 )
188
+
189
+
141
190
def test_execute_multiple_error ():
142
191
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
143
192
mock_post .return_value = MockResponse (200 , {"result" : "partial" ,
@@ -149,7 +198,7 @@ def test_execute_multiple_error():
149
198
conn = Connection (** mock_connection_params )
150
199
action0 = Action (top = "top0" ).append (a = "a0" )
151
200
action1 = Action (top = "top1" ).append (a = "a1" ).append (b = "b" )
152
- assert conn .execute_multiple ([action0 , action1 ]) == (2 , 1 )
201
+ assert conn .execute_multiple ([action0 , action1 ]) == (0 , 2 , 1 )
153
202
assert action0 .execution_errors () == []
154
203
assert action1 .execution_errors () == [{"command" : {"b" : "b" },
155
204
"errorCode" : "test.error" ,
@@ -170,7 +219,7 @@ def test_execute_multiple_multi_error():
170
219
conn = Connection (** mock_connection_params )
171
220
action0 = Action (top = "top0" ).append (a = "a0" )
172
221
action1 = Action (top = "top1" ).append (a = "a1" ).append (b = "b" )
173
- assert conn .execute_multiple ([action0 , action1 ]) == (2 , 1 )
222
+ assert conn .execute_multiple ([action0 , action1 ]) == (0 , 2 , 1 )
174
223
assert action0 .execution_errors () == []
175
224
assert action1 .execution_errors () == [{"command" : {"b" : "b" },
176
225
"errorCode" : "error1" ,
@@ -191,37 +240,70 @@ def test_execute_multiple_dofirst_error():
191
240
conn = Connection (** mock_connection_params )
192
241
action0 = Action (top = "top0" ).append (a = "a0" )
193
242
action1 = Action (top = "top1" ).append (a = "a1" ).insert (b = "b" )
194
- assert conn .execute_multiple ([action0 , action1 ]) == (2 , 1 )
243
+ assert conn .execute_multiple ([action0 , action1 ]) == (0 , 2 , 1 )
195
244
assert action0 .execution_errors () == []
196
245
assert action1 .execution_errors () == [{"command" : {"a" : "a1" },
197
246
"errorCode" : "test.error" ,
198
247
"message" : "Test error message" }]
199
248
200
249
201
- def test_execute_single_throttle_commands ():
202
- with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
203
- mock_post .return_value = MockResponse (200 , {"result" : "partial" ,
204
- "completed" : 1 ,
205
- "notCompleted" : 1 ,
206
- "errors" : [{"index" : 1 , "step" : 0 , "errorCode" : "test" }]})
207
- conn = Connection (throttle_commands = 2 , ** mock_connection_params )
208
- action = Action (top = "top0" ).append (a = "a0" ).append (a = "a1" ).append (a = "a2" )
209
- assert conn .execute_single (action ) == (2 , 1 )
210
- assert action .execution_errors () == [{"command" : {"a" : "a2" }, "errorCode" : "test" }]
211
-
212
-
213
- def test_execute_multiple_throttle_actions ():
250
+ def test_execute_multiple_single_queued_throttle_actions ():
214
251
with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
215
252
mock_post .side_effect = [MockResponse (200 , {"result" : "success" }),
216
253
MockResponse (200 , {"result" : "partial" ,
217
- "completed" : 0 ,
254
+ "completed" : 1 ,
218
255
"notCompleted" : 1 ,
219
256
"errors" : [{"index" : 0 , "step" : 0 , "errorCode" : "test" }]})]
220
257
conn = Connection (throttle_actions = 2 , ** mock_connection_params )
221
258
action0 = Action (top = "top0" ).append (a = "a0" )
222
259
action1 = Action (top = "top1" ).append (a = "a1" )
223
260
action2 = Action (top = "top2" ).append (a = "a2" )
224
- assert conn .execute_multiple ([action0 , action1 , action2 ]) == (3 , 2 )
261
+ action3 = Action (top = "top3" ).append (a = "a3" )
262
+ assert conn .execute_multiple ([action0 , action1 , action2 ], immediate = False ) == (1 , 2 , 2 )
263
+ local_status , server_status = conn .status (remote = False )
264
+ assert server_status == {"status" : "Never contacted" ,
265
+ "endpoint" : conn .endpoint }
266
+ assert local_status == {"multiple-query-count" : 0 ,
267
+ "single-query-count" : 0 ,
268
+ "actions-sent" : 2 ,
269
+ "actions-completed" : 2 ,
270
+ "actions-queued" : 1 }
271
+ assert conn .execute_single (action3 ) == (0 , 2 , 1 )
272
+ local_status , _ = conn .status (remote = False )
273
+ assert local_status == {"multiple-query-count" : 0 ,
274
+ "single-query-count" : 0 ,
275
+ "actions-sent" : 4 ,
276
+ "actions-completed" : 3 ,
277
+ "actions-queued" : 0 }
225
278
assert action0 .execution_errors () == []
226
279
assert action1 .execution_errors () == []
227
280
assert action2 .execution_errors () == [{"command" : {"a" : "a2" }, "errorCode" : "test" }]
281
+ assert action3 .execution_errors () == []
282
+
283
+
284
+ def test_execute_multiple_queued_throttle_actions_error ():
285
+ with mock .patch ("umapi_client.connection.requests.post" ) as mock_post :
286
+ mock_post .return_value = MockResponse (500 )
287
+ conn = Connection (throttle_actions = 2 , ** mock_connection_params )
288
+ action0 = Action (top = "top0" ).append (a = "a0" )
289
+ action1 = Action (top = "top1" ).append (a = "a1" )
290
+ action2 = Action (top = "top2" ).append (a = "a2" )
291
+ action3 = Action (top = "top3" ).append (a = "a3" )
292
+ action4 = Action (top = "top4" ).append (a = "a4" )
293
+ action5 = Action (top = "top5" ).append (a = "a5" )
294
+ pytest .raises (ServerError , conn .execute_multiple ,
295
+ [action0 , action1 , action2 , action3 , action4 , action5 ], immediate = False )
296
+ local_status , _ = conn .status (remote = False )
297
+ assert local_status == {"multiple-query-count" : 0 ,
298
+ "single-query-count" : 0 ,
299
+ "actions-sent" : 2 ,
300
+ "actions-completed" : 0 ,
301
+ "actions-queued" : 4 }
302
+ mock_post .return_value = MockResponse (200 , {"result" : "success" })
303
+ assert conn .execute_queued () == (0 , 4 , 4 )
304
+ local_status , _ = conn .status (remote = False )
305
+ assert local_status == {"multiple-query-count" : 0 ,
306
+ "single-query-count" : 0 ,
307
+ "actions-sent" : 6 ,
308
+ "actions-completed" : 4 ,
309
+ "actions-queued" : 0 }
0 commit comments