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_all() {
// let pool = testdb!(); #[sqlx::test]
// let res = Boat::all(&pool).await; fn test_on_water() {
// assert!(res.len() > 3); let pool = testdb!();
// } let logbook = Logbook::find_by_id(&pool, 1).await.unwrap();
// let logbookWithDetails = Logbook::on_water(&pool).await;
// #[sqlx::test]
// fn test_succ_create() { assert_eq!(logbookWithDetails[0].logbook, logbook);
// let pool = testdb!(); }
//
// assert_eq!( #[sqlx::test]
// Boat::create( fn test_completed() {
// &pool, let pool = testdb!();
// "new-boat-name".into(), let completed = Logbook::completed(&pool).await;
// 42,
// None, assert_eq!(
// "Best Boatbuilder".into(), completed[0].logbook,
// true, Logbook::find_by_id(&pool, 3).await.unwrap()
// true, );
// false, assert_eq!(
// Some(1), completed[1].logbook,
// None Logbook::find_by_id(&pool, 2).await.unwrap()
// ) );
// .await, }
// true
// ); //#[sqlx::test]
// } //fn test_all() {
// // let pool = testdb!();
// #[sqlx::test] // let res = Boat::all(&pool).await;
// fn test_duplicate_name_create() { // assert!(res.len() > 3);
// let pool = testdb!(); //}
//
// assert_eq!( #[sqlx::test]
// Boat::create( fn test_succ_create() {
// &pool, let pool = testdb!();
// "Haichenbach".into(),
// 42, Logbook::create(
// None, &pool,
// "Best Boatbuilder".into(), LogToAdd {
// true, boat_id: 3,
// true, shipmaster: 5,
// false, shipmaster_only_steering: false,
// Some(1), departure: "2128-05-20T12:00".into(),
// None arrival: None,
// ) destination: None,
// .await, distance_in_km: None,
// false comments: None,
// ); logtype: None,
// } rower: Vec::new(),
//} },
)
.await
.unwrap()
}
#[sqlx::test]
fn test_create_boat_not_found() {
let pool = testdb!();
let res = Logbook::create(
&pool,
LogToAdd {
boat_id: 999,
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::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)));
}
}