@@ -137,11 +137,12 @@ fn update_prescribed_quantity(
137
137
mod test {
138
138
use repository:: {
139
139
mock:: {
140
- mock_item_a, mock_prescription_picked , mock_stock_line_a , mock_store_a , MockData ,
141
- MockDataInserts ,
140
+ mock_item_a, mock_item_b , mock_prescription_picked , mock_stock_line_a ,
141
+ mock_stock_line_b , mock_store_a , MockData , MockDataInserts ,
142
142
} ,
143
143
test_db:: setup_all_with_data,
144
- InvoiceLineRow , InvoiceLineType ,
144
+ EqualFilter , InvoiceLineFilter , InvoiceLineRepository , InvoiceLineRow ,
145
+ InvoiceLineRowRepository , InvoiceLineType ,
145
146
} ;
146
147
use util:: inline_init;
147
148
@@ -151,7 +152,7 @@ mod test {
151
152
152
153
fn mock_prescription_unallocated_invoice_line ( ) -> InvoiceLineRow {
153
154
InvoiceLineRow {
154
- id : "test_invoice_line " . to_string ( ) ,
155
+ id : "unallocated_invoice_line " . to_string ( ) ,
155
156
invoice_id : mock_prescription_picked ( ) . id ,
156
157
item_name : mock_item_a ( ) . name ,
157
158
item_code : mock_item_a ( ) . code ,
@@ -165,7 +166,7 @@ mod test {
165
166
166
167
fn mock_prescription_invoice_line_a ( ) -> InvoiceLineRow {
167
168
InvoiceLineRow {
168
- id : "existing_stock_invoice_line " . to_string ( ) ,
169
+ id : "existing_stock_invoice_line_a " . to_string ( ) ,
169
170
invoice_id : mock_prescription_picked ( ) . id ,
170
171
item_name : mock_item_a ( ) . name ,
171
172
item_code : mock_item_a ( ) . code ,
@@ -179,34 +180,34 @@ mod test {
179
180
180
181
fn mock_prescription_invoice_line_b ( ) -> InvoiceLineRow {
181
182
InvoiceLineRow {
182
- id : "existing_stock_invoice_line " . to_string ( ) ,
183
+ id : "existing_stock_invoice_line_b " . to_string ( ) ,
183
184
invoice_id : mock_prescription_picked ( ) . id ,
184
185
item_name : mock_item_a ( ) . name ,
185
186
item_code : mock_item_a ( ) . code ,
186
187
item_link_id : mock_item_a ( ) . id ,
187
188
r#type : InvoiceLineType :: StockOut ,
188
- stock_line_id : Some ( mock_stock_line_a ( ) . id ) ,
189
+ stock_line_id : Some ( mock_stock_line_b ( ) . id ) ,
189
190
..Default :: default ( )
190
191
}
191
192
}
192
193
193
194
fn mock_prescription_invoice_line_c ( ) -> InvoiceLineRow {
194
195
InvoiceLineRow {
195
- id : "existing_stock_invoice_line " . to_string ( ) ,
196
+ id : "existing_stock_invoice_line_c " . to_string ( ) ,
196
197
invoice_id : mock_prescription_picked ( ) . id ,
197
198
item_name : mock_item_a ( ) . name ,
198
199
item_code : mock_item_a ( ) . code ,
199
200
item_link_id : mock_item_a ( ) . id ,
200
201
r#type : InvoiceLineType :: StockOut ,
201
- stock_line_id : Some ( mock_stock_line_a ( ) . id ) ,
202
+ stock_line_id : Some ( mock_stock_line_b ( ) . id ) , // TODO: should be different stock line ideally
202
203
..Default :: default ( )
203
204
}
204
205
}
205
206
206
207
#[ actix_rt:: test]
207
- async fn set_prescribed_quantity_no_item_line ( ) {
208
+ async fn set_prescribed_quantity_no_stock_line ( ) {
208
209
let ( _, _, connection_manager, _) = setup_all_with_data (
209
- "set_prescribed_quantity_no_item_line " ,
210
+ "set_prescribed_quantity_no_stock_line " ,
210
211
MockDataInserts :: all ( ) ,
211
212
inline_init ( |r : & mut MockData | {
212
213
r. invoice_lines = vec ! [ mock_prescription_unallocated_invoice_line( ) ]
@@ -220,39 +221,75 @@ mod test {
220
221
. unwrap ( ) ;
221
222
let service = service_provider. invoice_line_service ;
222
223
224
+ let new_prescribed_quantity = 20.0 ;
225
+
223
226
let result = service. set_prescribed_quantity (
224
227
& context,
225
228
SetPrescribedQuantity {
226
229
invoice_id : mock_prescription_unallocated_invoice_line ( ) . invoice_id ,
227
230
item_id : mock_prescription_unallocated_invoice_line ( ) . item_link_id ,
228
- prescribed_quantity : 10.0 ,
231
+ prescribed_quantity : new_prescribed_quantity ,
229
232
} ,
230
233
) ;
231
234
232
235
assert ! ( result. is_ok( ) ) ;
233
236
234
- let invoice_line = result. unwrap ( ) ;
237
+ let repo = InvoiceLineRowRepository :: new ( & context. connection ) ;
238
+
239
+ let invoice_line = repo
240
+ . find_one_by_id ( & mock_prescription_unallocated_invoice_line ( ) . id )
241
+ . unwrap ( )
242
+ . unwrap ( ) ;
235
243
236
244
assert_eq ! (
237
- invoice_line. invoice_line_row . invoice_id,
245
+ invoice_line. invoice_id,
238
246
mock_prescription_unallocated_invoice_line( ) . invoice_id
239
247
) ;
240
248
assert_eq ! (
241
- invoice_line. invoice_line_row . item_link_id,
249
+ invoice_line. item_link_id,
242
250
mock_prescription_unallocated_invoice_line( ) . item_link_id
243
251
) ;
244
252
assert_eq ! (
245
- invoice_line. invoice_line_row . item_code,
253
+ invoice_line. item_code,
246
254
mock_prescription_unallocated_invoice_line( ) . item_code
247
255
) ;
248
256
assert_eq ! (
249
- invoice_line. invoice_line_row . item_name,
257
+ invoice_line. item_name,
250
258
mock_prescription_unallocated_invoice_line( ) . item_name
251
259
) ;
260
+ assert_eq ! (
261
+ invoice_line. prescribed_quantity,
262
+ Some ( new_prescribed_quantity)
263
+ ) ;
264
+
265
+ let line_repo = InvoiceLineRepository :: new ( & context. connection ) ;
266
+
267
+ let filter = InvoiceLineFilter :: new ( ) . invoice_id ( EqualFilter :: equal_to (
268
+ & mock_prescription_unallocated_invoice_line ( ) . invoice_id ,
269
+ ) ) ;
270
+ let line_count = line_repo. count ( Some ( filter. clone ( ) ) ) . unwrap ( ) ;
271
+ assert_eq ! ( line_count, 1 ) ;
272
+
273
+ // Check a new line gets created if one doesn't exist yet...
274
+ let result = service. set_prescribed_quantity (
275
+ & context,
276
+ SetPrescribedQuantity {
277
+ invoice_id : mock_prescription_unallocated_invoice_line ( ) . invoice_id ,
278
+ item_id : mock_item_b ( ) . id ,
279
+ prescribed_quantity : new_prescribed_quantity,
280
+ } ,
281
+ ) ;
282
+
283
+ assert ! ( result. is_ok( ) ) ;
284
+ let invoice_line = result. unwrap ( ) ;
285
+
252
286
assert_eq ! (
253
287
invoice_line. invoice_line_row. prescribed_quantity,
254
- mock_prescription_unallocated_invoice_line ( ) . prescribed_quantity
288
+ Some ( new_prescribed_quantity )
255
289
) ;
290
+
291
+ let line_count = line_repo. count ( Some ( filter. clone ( ) ) ) . unwrap ( ) ;
292
+ assert_eq ! ( line_count, 2 ) ;
256
293
}
257
294
258
295
#[ actix_rt:: test]
@@ -276,12 +313,14 @@ mod test {
276
313
. unwrap ( ) ;
277
314
let service = service_provider. invoice_line_service ;
278
315
316
+ let new_prescribed_quantity = 55.0 ;
317
+
279
318
let result = service. set_prescribed_quantity (
280
319
& context,
281
320
SetPrescribedQuantity {
282
321
invoice_id : mock_prescription_invoice_line_a ( ) . invoice_id ,
283
322
item_id : mock_prescription_invoice_line_a ( ) . item_link_id ,
284
- prescribed_quantity : 55.0 ,
323
+ prescribed_quantity : new_prescribed_quantity ,
285
324
} ,
286
325
) ;
287
326
@@ -294,26 +333,27 @@ mod test {
294
333
invoice_line. invoice_line_row. invoice_id,
295
334
mock_prescription_invoice_line_a( ) . invoice_id
296
335
) ;
297
- assert_eq ! (
298
- invoice_line. invoice_line_row. item_link_id,
299
- mock_prescription_invoice_line_a( ) . item_link_id
300
- ) ;
301
- assert_eq ! (
302
- invoice_line. invoice_line_row. item_code,
303
- mock_prescription_invoice_line_a( ) . item_code
304
- ) ;
305
- assert_eq ! (
306
- invoice_line. invoice_line_row. item_name,
307
- mock_prescription_invoice_line_a( ) . item_name
308
- ) ;
309
- assert_eq ! (
310
- invoice_line. invoice_line_row. prescribed_quantity,
311
- Some ( 55.0 )
312
- ) ;
313
336
314
337
// doesn't update invoice lines that doesn't have prescribed quantity initially
315
- assert_eq ! ( mock_prescription_invoice_line_b( ) . prescribed_quantity, None ) ;
316
- assert_eq ! ( mock_prescription_invoice_line_c( ) . prescribed_quantity, None ) ;
338
+ let line_row_repo = InvoiceLineRowRepository :: new ( & context. connection ) ;
339
+ let line_a = line_row_repo
340
+ . find_one_by_id ( & mock_prescription_invoice_line_a ( ) . id )
341
+ . unwrap ( )
342
+ . unwrap ( ) ;
343
+ assert_eq ! ( line_a. prescribed_quantity, Some ( new_prescribed_quantity) ) ;
344
+ let line_b = line_row_repo
345
+ . find_one_by_id ( & mock_prescription_invoice_line_b ( ) . id )
346
+ . unwrap ( )
347
+ . unwrap ( ) ;
348
+
349
+ assert_eq ! ( line_b. prescribed_quantity, None ) ;
350
+
351
+ let line_c = line_row_repo
352
+ . find_one_by_id ( & mock_prescription_invoice_line_c ( ) . id )
353
+ . unwrap ( )
354
+ . unwrap ( ) ;
355
+
356
+ assert_eq ! ( line_c. prescribed_quantity, None ) ;
317
357
}
318
358
319
359
#[ actix_rt:: test]
@@ -338,43 +378,46 @@ mod test {
338
378
. unwrap ( ) ;
339
379
let service = service_provider. invoice_line_service ;
340
380
381
+ let new_prescribed_quantity = 15.0 ;
382
+
341
383
let result = service. set_prescribed_quantity (
342
384
& context,
343
385
SetPrescribedQuantity {
344
386
invoice_id : mock_prescription_unallocated_invoice_line ( ) . invoice_id ,
345
387
item_id : mock_prescription_unallocated_invoice_line ( ) . item_link_id ,
346
- prescribed_quantity : 10.0 ,
388
+ prescribed_quantity : new_prescribed_quantity ,
347
389
} ,
348
390
) ;
349
391
350
392
assert ! ( result. is_ok( ) ) ;
351
393
352
- let invoice_line = result . unwrap ( ) ;
394
+ let line_row_repo = InvoiceLineRowRepository :: new ( & context . connection ) ;
353
395
354
- // updates invoice line
355
- assert_eq ! (
356
- invoice_line. invoice_line_row. invoice_id,
357
- mock_prescription_invoice_line_a( ) . invoice_id
358
- ) ;
359
- assert_eq ! (
360
- invoice_line. invoice_line_row. item_link_id,
361
- mock_prescription_invoice_line_a( ) . item_link_id
362
- ) ;
363
- assert_eq ! (
364
- invoice_line. invoice_line_row. item_code,
365
- mock_prescription_invoice_line_a( ) . item_code
366
- ) ;
367
- assert_eq ! (
368
- invoice_line. invoice_line_row. item_name,
369
- mock_prescription_invoice_line_a( ) . item_name
370
- ) ;
371
- assert_eq ! (
372
- invoice_line. invoice_line_row. prescribed_quantity,
373
- Some ( 10.0 )
374
- ) ;
396
+ // Check that the first allocated line gets the prescribed quantity
397
+ let line_a = line_row_repo
398
+ . find_one_by_id ( & mock_prescription_invoice_line_a ( ) . id )
399
+ . unwrap ( )
400
+ . unwrap ( ) ;
401
+ assert_eq ! ( line_a. prescribed_quantity, Some ( new_prescribed_quantity) ) ;
375
402
376
- // doesn't update invoice lines that doesn't have prescribed quantity initially
377
- assert_eq ! ( mock_prescription_invoice_line_b( ) . prescribed_quantity, None ) ;
378
- assert_eq ! ( mock_prescription_invoice_line_c( ) . prescribed_quantity, None ) ;
403
+ // Check other lines don't have the prescribed quantity
404
+ let line_b = line_row_repo
405
+ . find_one_by_id ( & mock_prescription_invoice_line_b ( ) . id )
406
+ . unwrap ( )
407
+ . unwrap ( ) ;
408
+ assert_eq ! ( line_b. prescribed_quantity, None ) ;
409
+
410
+ let line_c = line_row_repo
411
+ . find_one_by_id ( & mock_prescription_invoice_line_c ( ) . id )
412
+ . unwrap ( )
413
+ . unwrap ( ) ;
414
+ assert_eq ! ( line_c. prescribed_quantity, None ) ;
415
+
416
+ // Check that the unallocated line is deleted
417
+ let result = service
418
+ . get_invoice_line ( & context, & mock_prescription_unallocated_invoice_line ( ) . id )
419
+ . unwrap ( ) ;
420
+
421
+ assert ! ( result. is_none( ) ) ;
379
422
}
380
423
}
0 commit comments