parent
adb26f6b91
commit
47b534f8c3
@ -5,7 +5,7 @@ use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||||
|
||||
use super::{boat::Boat, rower::Rower, user::User};
|
||||
|
||||
#[derive(FromRow, Serialize, Clone)]
|
||||
#[derive(FromRow, Serialize, Clone, Debug)]
|
||||
pub struct Logbook {
|
||||
pub id: i64,
|
||||
pub boat_id: i64,
|
||||
@ -20,6 +20,12 @@ pub struct Logbook {
|
||||
pub logtype: Option<i64>,
|
||||
}
|
||||
|
||||
impl PartialEq for Logbook {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct LogToAdd {
|
||||
pub boat_id: i32,
|
||||
@ -54,12 +60,14 @@ pub struct LogbookWithBoatAndRowers {
|
||||
pub arrival_timestamp: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum LogbookUpdateError {
|
||||
NotYourEntry,
|
||||
TooManyRowers(usize, usize),
|
||||
RowerCreateError(i64, String),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum LogbookCreateError {
|
||||
BoatAlreadyOnWater,
|
||||
BoatLocked,
|
||||
@ -312,75 +320,286 @@ ORDER BY departure DESC
|
||||
// .unwrap(); //Okay, because we can only create a User of a valid id
|
||||
// }
|
||||
}
|
||||
//
|
||||
//#[cfg(test)]
|
||||
//mod test {
|
||||
// use crate::{model::boat::Boat, testdb};
|
||||
//
|
||||
// use sqlx::SqlitePool;
|
||||
//
|
||||
// #[sqlx::test]
|
||||
// fn test_find_correct_id() {
|
||||
// let pool = testdb!();
|
||||
// let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||
// assert_eq!(boat.id, 1);
|
||||
// }
|
||||
//
|
||||
// #[sqlx::test]
|
||||
// fn test_find_wrong_id() {
|
||||
// let pool = testdb!();
|
||||
// let boat = Boat::find_by_id(&pool, 1337).await;
|
||||
// assert!(boat.is_none());
|
||||
// }
|
||||
//
|
||||
// #[sqlx::test]
|
||||
// fn test_all() {
|
||||
// let pool = testdb!();
|
||||
// let res = Boat::all(&pool).await;
|
||||
// assert!(res.len() > 3);
|
||||
// }
|
||||
//
|
||||
// #[sqlx::test]
|
||||
// fn test_succ_create() {
|
||||
// let pool = testdb!();
|
||||
//
|
||||
// assert_eq!(
|
||||
// Boat::create(
|
||||
// &pool,
|
||||
// "new-boat-name".into(),
|
||||
// 42,
|
||||
// None,
|
||||
// "Best Boatbuilder".into(),
|
||||
// true,
|
||||
// true,
|
||||
// false,
|
||||
// Some(1),
|
||||
// None
|
||||
// )
|
||||
// .await,
|
||||
// true
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// #[sqlx::test]
|
||||
// fn test_duplicate_name_create() {
|
||||
// let pool = testdb!();
|
||||
//
|
||||
// assert_eq!(
|
||||
// Boat::create(
|
||||
// &pool,
|
||||
// "Haichenbach".into(),
|
||||
// 42,
|
||||
// None,
|
||||
// "Best Boatbuilder".into(),
|
||||
// true,
|
||||
// true,
|
||||
// false,
|
||||
// Some(1),
|
||||
// None
|
||||
// )
|
||||
// .await,
|
||||
// false
|
||||
// );
|
||||
// }
|
||||
//}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{LogToAdd, Logbook, LogbookCreateError, LogbookUpdateError};
|
||||
use crate::model::user::User;
|
||||
use crate::testdb;
|
||||
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_find_correct_id() {
|
||||
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!();
|
||||
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]
|
||||
//fn test_all() {
|
||||
// let pool = testdb!();
|
||||
// let res = Boat::all(&pool).await;
|
||||
// assert!(res.len() > 3);
|
||||
//}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_succ_create() {
|
||||
let pool = testdb!();
|
||||
|
||||
Logbook::create(
|
||||
&pool,
|
||||
LogToAdd {
|
||||
boat_id: 3,
|
||||
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
|
||||
.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)));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user