Skip to content

Commit f80676f

Browse files
committed
Update test cases
1 parent 86f7cd9 commit f80676f

File tree

1 file changed

+106
-63
lines changed
  • server/service/src/invoice_line/stock_out_line/set_prescribed_quantity

1 file changed

+106
-63
lines changed

server/service/src/invoice_line/stock_out_line/set_prescribed_quantity/mod.rs

+106-63
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ fn update_prescribed_quantity(
137137
mod test {
138138
use repository::{
139139
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,
142142
},
143143
test_db::setup_all_with_data,
144-
InvoiceLineRow, InvoiceLineType,
144+
EqualFilter, InvoiceLineFilter, InvoiceLineRepository, InvoiceLineRow,
145+
InvoiceLineRowRepository, InvoiceLineType,
145146
};
146147
use util::inline_init;
147148

@@ -151,7 +152,7 @@ mod test {
151152

152153
fn mock_prescription_unallocated_invoice_line() -> InvoiceLineRow {
153154
InvoiceLineRow {
154-
id: "test_invoice_line".to_string(),
155+
id: "unallocated_invoice_line".to_string(),
155156
invoice_id: mock_prescription_picked().id,
156157
item_name: mock_item_a().name,
157158
item_code: mock_item_a().code,
@@ -165,7 +166,7 @@ mod test {
165166

166167
fn mock_prescription_invoice_line_a() -> InvoiceLineRow {
167168
InvoiceLineRow {
168-
id: "existing_stock_invoice_line".to_string(),
169+
id: "existing_stock_invoice_line_a".to_string(),
169170
invoice_id: mock_prescription_picked().id,
170171
item_name: mock_item_a().name,
171172
item_code: mock_item_a().code,
@@ -179,34 +180,34 @@ mod test {
179180

180181
fn mock_prescription_invoice_line_b() -> InvoiceLineRow {
181182
InvoiceLineRow {
182-
id: "existing_stock_invoice_line".to_string(),
183+
id: "existing_stock_invoice_line_b".to_string(),
183184
invoice_id: mock_prescription_picked().id,
184185
item_name: mock_item_a().name,
185186
item_code: mock_item_a().code,
186187
item_link_id: mock_item_a().id,
187188
r#type: InvoiceLineType::StockOut,
188-
stock_line_id: Some(mock_stock_line_a().id),
189+
stock_line_id: Some(mock_stock_line_b().id),
189190
..Default::default()
190191
}
191192
}
192193

193194
fn mock_prescription_invoice_line_c() -> InvoiceLineRow {
194195
InvoiceLineRow {
195-
id: "existing_stock_invoice_line".to_string(),
196+
id: "existing_stock_invoice_line_c".to_string(),
196197
invoice_id: mock_prescription_picked().id,
197198
item_name: mock_item_a().name,
198199
item_code: mock_item_a().code,
199200
item_link_id: mock_item_a().id,
200201
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
202203
..Default::default()
203204
}
204205
}
205206

206207
#[actix_rt::test]
207-
async fn set_prescribed_quantity_no_item_line() {
208+
async fn set_prescribed_quantity_no_stock_line() {
208209
let (_, _, connection_manager, _) = setup_all_with_data(
209-
"set_prescribed_quantity_no_item_line",
210+
"set_prescribed_quantity_no_stock_line",
210211
MockDataInserts::all(),
211212
inline_init(|r: &mut MockData| {
212213
r.invoice_lines = vec![mock_prescription_unallocated_invoice_line()]
@@ -220,39 +221,75 @@ mod test {
220221
.unwrap();
221222
let service = service_provider.invoice_line_service;
222223

224+
let new_prescribed_quantity = 20.0;
225+
223226
let result = service.set_prescribed_quantity(
224227
&context,
225228
SetPrescribedQuantity {
226229
invoice_id: mock_prescription_unallocated_invoice_line().invoice_id,
227230
item_id: mock_prescription_unallocated_invoice_line().item_link_id,
228-
prescribed_quantity: 10.0,
231+
prescribed_quantity: new_prescribed_quantity,
229232
},
230233
);
231234

232235
assert!(result.is_ok());
233236

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();
235243

236244
assert_eq!(
237-
invoice_line.invoice_line_row.invoice_id,
245+
invoice_line.invoice_id,
238246
mock_prescription_unallocated_invoice_line().invoice_id
239247
);
240248
assert_eq!(
241-
invoice_line.invoice_line_row.item_link_id,
249+
invoice_line.item_link_id,
242250
mock_prescription_unallocated_invoice_line().item_link_id
243251
);
244252
assert_eq!(
245-
invoice_line.invoice_line_row.item_code,
253+
invoice_line.item_code,
246254
mock_prescription_unallocated_invoice_line().item_code
247255
);
248256
assert_eq!(
249-
invoice_line.invoice_line_row.item_name,
257+
invoice_line.item_name,
250258
mock_prescription_unallocated_invoice_line().item_name
251259
);
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+
252286
assert_eq!(
253287
invoice_line.invoice_line_row.prescribed_quantity,
254-
mock_prescription_unallocated_invoice_line().prescribed_quantity
288+
Some(new_prescribed_quantity)
255289
);
290+
291+
let line_count = line_repo.count(Some(filter.clone())).unwrap();
292+
assert_eq!(line_count, 2);
256293
}
257294

258295
#[actix_rt::test]
@@ -276,12 +313,14 @@ mod test {
276313
.unwrap();
277314
let service = service_provider.invoice_line_service;
278315

316+
let new_prescribed_quantity = 55.0;
317+
279318
let result = service.set_prescribed_quantity(
280319
&context,
281320
SetPrescribedQuantity {
282321
invoice_id: mock_prescription_invoice_line_a().invoice_id,
283322
item_id: mock_prescription_invoice_line_a().item_link_id,
284-
prescribed_quantity: 55.0,
323+
prescribed_quantity: new_prescribed_quantity,
285324
},
286325
);
287326

@@ -294,26 +333,27 @@ mod test {
294333
invoice_line.invoice_line_row.invoice_id,
295334
mock_prescription_invoice_line_a().invoice_id
296335
);
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-
);
313336

314337
// 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);
317357
}
318358

319359
#[actix_rt::test]
@@ -338,43 +378,46 @@ mod test {
338378
.unwrap();
339379
let service = service_provider.invoice_line_service;
340380

381+
let new_prescribed_quantity = 15.0;
382+
341383
let result = service.set_prescribed_quantity(
342384
&context,
343385
SetPrescribedQuantity {
344386
invoice_id: mock_prescription_unallocated_invoice_line().invoice_id,
345387
item_id: mock_prescription_unallocated_invoice_line().item_link_id,
346-
prescribed_quantity: 10.0,
388+
prescribed_quantity: new_prescribed_quantity,
347389
},
348390
);
349391

350392
assert!(result.is_ok());
351393

352-
let invoice_line = result.unwrap();
394+
let line_row_repo = InvoiceLineRowRepository::new(&context.connection);
353395

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));
375402

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());
379422
}
380423
}

0 commit comments

Comments
 (0)