create tests for model/logbook

Closes #31
This commit is contained in:
philipp 2023-07-31 19:10:34 +02:00
parent adb26f6b91
commit 47b534f8c3

View File

@ -5,7 +5,7 @@ use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
use super::{boat::Boat, rower::Rower, user::User}; use super::{boat::Boat, rower::Rower, user::User};
#[derive(FromRow, Serialize, Clone)] #[derive(FromRow, Serialize, Clone, Debug)]
pub struct Logbook { pub struct Logbook {
pub id: i64, pub id: i64,
pub boat_id: i64, pub boat_id: i64,
@ -20,6 +20,12 @@ pub struct Logbook {
pub logtype: Option<i64>, pub logtype: Option<i64>,
} }
impl PartialEq for Logbook {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
#[derive(FromForm)] #[derive(FromForm)]
pub struct LogToAdd { pub struct LogToAdd {
pub boat_id: i32, pub boat_id: i32,
@ -54,12 +60,14 @@ pub struct LogbookWithBoatAndRowers {
pub arrival_timestamp: Option<i64>, pub arrival_timestamp: Option<i64>,
} }
#[derive(Debug, PartialEq)]
pub enum LogbookUpdateError { pub enum LogbookUpdateError {
NotYourEntry, NotYourEntry,
TooManyRowers(usize, usize), TooManyRowers(usize, usize),
RowerCreateError(i64, String), RowerCreateError(i64, String),
} }
#[derive(Debug, PartialEq)]
pub enum LogbookCreateError { pub enum LogbookCreateError {
BoatAlreadyOnWater, BoatAlreadyOnWater,
BoatLocked, BoatLocked,
@ -312,75 +320,286 @@ ORDER BY departure DESC
// .unwrap(); //Okay, because we can only create a User of a valid id // .unwrap(); //Okay, because we can only create a User of a valid id
// } // }
} }
//
//#[cfg(test)] #[cfg(test)]
//mod test { mod test {
// use crate::{model::boat::Boat, testdb}; use super::{LogToAdd, Logbook, LogbookCreateError, LogbookUpdateError};
// use crate::model::user::User;
// use sqlx::SqlitePool; use crate::testdb;
//
// #[sqlx::test] use sqlx::SqlitePool;
// fn test_find_correct_id() {
// let pool = testdb!(); #[sqlx::test]
// let boat = Boat::find_by_id(&pool, 1).await.unwrap(); fn test_find_correct_id() {
// assert_eq!(boat.id, 1); let pool = testdb!();
// } let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
// assert_eq!(logbook.id, 1);
// #[sqlx::test] }
// fn test_find_wrong_id() {
// let pool = testdb!(); #[sqlx::test]
// let boat = Boat::find_by_id(&pool, 1337).await; fn test_find_wrong_id() {
// assert!(boat.is_none()); let pool = testdb!();
// } let logbook = Logbook::find_by_id(&pool, 1337).await;
// assert_eq!(logbook, None);
}
#[sqlx::test]
fn test_on_water() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
let logbookWithDetails = Logbook::on_water(&pool).await;
assert_eq!(logbookWithDetails[0].logbook, logbook);
}
#[sqlx::test]
fn test_completed() {
let pool = testdb!();
let completed = Logbook::completed(&pool).await;
assert_eq!(
completed[0].logbook,
Logbook::find_by_id(&pool, 3).await.unwrap()
);
assert_eq!(
completed[1].logbook,
Logbook::find_by_id(&pool, 2).await.unwrap()
);
}
//#[sqlx::test] //#[sqlx::test]
//fn test_all() { //fn test_all() {
// let pool = testdb!(); // let pool = testdb!();
// let res = Boat::all(&pool).await; // let res = Boat::all(&pool).await;
// assert!(res.len() > 3); // assert!(res.len() > 3);
//} //}
//
// #[sqlx::test] #[sqlx::test]
// fn test_succ_create() { fn test_succ_create() {
// let pool = testdb!(); let pool = testdb!();
//
// assert_eq!( Logbook::create(
// Boat::create( &pool,
// &pool, LogToAdd {
// "new-boat-name".into(), boat_id: 3,
// 42, shipmaster: 5,
// None, shipmaster_only_steering: false,
// "Best Boatbuilder".into(), departure: "2128-05-20T12:00".into(),
// true, arrival: None,
// true, destination: None,
// false, distance_in_km: None,
// Some(1), comments: None,
// None logtype: None,
// ) rower: Vec::new(),
// .await, },
// true )
// ); .await
// } .unwrap()
// }
// #[sqlx::test]
// fn test_duplicate_name_create() { #[sqlx::test]
// let pool = testdb!(); fn test_create_boat_not_found() {
// let pool = testdb!();
// assert_eq!(
// Boat::create( let res = Logbook::create(
// &pool, &pool,
// "Haichenbach".into(), LogToAdd {
// 42, boat_id: 999,
// None, shipmaster: 5,
// "Best Boatbuilder".into(), shipmaster_only_steering: false,
// true, departure: "2128-05-20T12:00".into(),
// true, arrival: None,
// false, destination: None,
// Some(1), distance_in_km: None,
// None comments: None,
// ) logtype: None,
// .await, rower: Vec::new(),
// false },
// ); )
// } .await;
//}
assert_eq!(res, Err(LogbookCreateError::BoatNotFound));
}
#[sqlx::test]
fn test_create_boat_locked() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 5,
shipmaster: 5,
shipmaster_only_steering: false,
departure: "2128-05-20T12:00".into(),
arrival: None,
destination: None,
distance_in_km: None,
comments: None,
logtype: None,
rower: Vec::new(),
},
)
.await;
assert_eq!(res, Err(LogbookCreateError::BoatLocked));
}
#[sqlx::test]
fn test_create_boat_on_water() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 2,
shipmaster: 5,
shipmaster_only_steering: false,
departure: "2128-05-20T12:00".into(),
arrival: None,
destination: None,
distance_in_km: None,
comments: None,
logtype: None,
rower: Vec::new(),
},
)
.await;
assert_eq!(res, Err(LogbookCreateError::BoatAlreadyOnWater));
}
#[sqlx::test]
fn test_create_shipmaster_on_water() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 3,
shipmaster: 2,
shipmaster_only_steering: false,
departure: "2128-05-20T12:00".into(),
arrival: None,
destination: None,
distance_in_km: None,
comments: None,
logtype: None,
rower: Vec::new(),
},
)
.await;
assert_eq!(res, Err(LogbookCreateError::ShipmasterAlreadyOnWater));
}
#[sqlx::test]
fn test_create_too_many_rowers() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 1,
shipmaster: 5,
shipmaster_only_steering: false,
departure: "2128-05-20T12:00".into(),
arrival: None,
destination: None,
distance_in_km: None,
comments: None,
logtype: None,
rower: vec![1],
},
)
.await;
assert_eq!(res, Err(LogbookCreateError::TooManyRowers(1, 2)));
}
#[sqlx::test]
fn test_distances() {
let pool = testdb!();
let res = Logbook::distances(&pool).await;
assert_eq!(
res,
vec![
("Ottensheim".into(), 25 as i64),
("Ottensheim + Regattastrecke".into(), 29 as i64),
]
);
}
#[sqlx::test]
fn test_succ_home() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
let user = User::find_by_id(&pool, 2).await.unwrap();
logbook
.home(
&pool,
&user,
super::LogToFinalize {
destination: "new-destination".into(),
distance_in_km: 42,
comments: Some("Perfect water".into()),
logtype: None,
rower: vec![],
},
)
.await
.unwrap();
}
#[sqlx::test]
fn test_home_wrong_user() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
let user = User::find_by_id(&pool, 1).await.unwrap();
let res = logbook
.home(
&pool,
&user,
super::LogToFinalize {
destination: "new-destination".into(),
distance_in_km: 42,
comments: Some("Perfect water".into()),
logtype: None,
rower: vec![],
},
)
.await;
assert_eq!(res, Err(LogbookUpdateError::NotYourEntry));
}
#[sqlx::test]
fn test_home_too_many_rower() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
let user = User::find_by_id(&pool, 2).await.unwrap();
let res = logbook
.home(
&pool,
&user,
super::LogToFinalize {
destination: "new-destination".into(),
distance_in_km: 42,
comments: Some("Perfect water".into()),
logtype: None,
rower: vec![1],
},
)
.await;
assert_eq!(res, Err(LogbookUpdateError::TooManyRowers(1, 2)));
}
}