From b47d91ad6ed1be6093edc0c3f702cb997223522c Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 14 Oct 2021 21:21:24 +1100 Subject: [PATCH] Extend DB test suite --- daemon/src/db.rs | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/daemon/src/db.rs b/daemon/src/db.rs index 7c3db0b..ae49c88 100644 --- a/daemon/src/db.rs +++ b/daemon/src/db.rs @@ -622,27 +622,55 @@ mod tests { } #[tokio::test] - async fn test_insert_new_cfd_state() { + async fn test_insert_new_cfd_state_and_load_with_multiple_cfd() { let pool = setup_test_db().await; let mut conn = pool.acquire().await.unwrap(); - let mut cfd = Cfd::default(); + let mut cfd_1 = Cfd::default(); - insert_order(&cfd.order, &mut conn).await.unwrap(); - insert_cfd(cfd.clone(), &mut conn).await.unwrap(); + insert_order(&cfd_1.order, &mut conn).await.unwrap(); + insert_cfd(cfd_1.clone(), &mut conn).await.unwrap(); - cfd.state = CfdState::Accepted { + cfd_1.state = CfdState::Accepted { common: CfdStateCommon { transition_timestamp: SystemTime::now(), }, }; - insert_new_cfd_state_by_order_id(cfd.order.id, &cfd.state, &mut conn) + insert_new_cfd_state_by_order_id(cfd_1.order.id, &cfd_1.state, &mut conn) .await .unwrap(); let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); + assert_eq!(cfds_from_db.len(), 1); + let cfd_from_db = cfds_from_db.first().unwrap().clone(); - assert_eq!(cfd, cfd_from_db) + assert_eq!(cfd_1, cfd_from_db); + + let mut cfd_2 = Cfd::default(); + + insert_order(&cfd_2.order, &mut conn).await.unwrap(); + insert_cfd(cfd_2.clone(), &mut conn).await.unwrap(); + + let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); + assert_eq!(cfds_from_db.len(), 2); + + assert!(cfds_from_db.contains(&cfd_1)); + assert!(cfds_from_db.contains(&cfd_2)); + + cfd_2.state = CfdState::Rejected { + common: CfdStateCommon { + transition_timestamp: SystemTime::now(), + }, + }; + insert_new_cfd_state_by_order_id(cfd_2.order.id, &cfd_2.state, &mut conn) + .await + .unwrap(); + + let cfds_from_db = load_all_cfds(&mut conn).await.unwrap(); + assert_eq!(cfds_from_db.len(), 2); + + assert!(cfds_from_db.contains(&cfd_1)); + assert!(cfds_from_db.contains(&cfd_2)); } async fn setup_test_db() -> SqlitePool {