create tests for model/rower.rs, Closes #32

This commit is contained in:
philipp 2023-07-31 16:59:15 +02:00
parent 32e6148844
commit adb26f6b91
5 changed files with 82 additions and 12 deletions

View File

@ -57,6 +57,7 @@ pub struct LogbookWithBoatAndRowers {
pub enum LogbookUpdateError {
NotYourEntry,
TooManyRowers(usize, usize),
RowerCreateError(i64, String),
}
pub enum LogbookCreateError {
@ -66,6 +67,7 @@ pub enum LogbookCreateError {
TooManyRowers(usize, usize),
ShipmasterAlreadyOnWater,
RowerAlreadyOnWater(User),
RowerCreateError(i64, String),
}
impl Logbook {
@ -164,8 +166,8 @@ ORDER BY departure DESC
pub async fn create(db: &SqlitePool, log: LogToAdd) -> Result<(), LogbookCreateError> {
let Some(boat) = Boat::find_by_id(db, log.boat_id).await else {
return Err(LogbookCreateError::BoatNotFound);
};
return Err(LogbookCreateError::BoatNotFound);
};
if boat.is_locked(db).await {
return Err(LogbookCreateError::BoatLocked);
@ -217,7 +219,11 @@ ORDER BY departure DESC
.await.unwrap();
for rower in &log.rower {
Rower::create(&mut tx, inserted_row.id, *rower).await;
Rower::create(&mut tx, inserted_row.id, *rower)
.await
.map_err(|e| {
return LogbookCreateError::RowerCreateError(*rower, e.to_string());
})?;
}
tx.commit().await.unwrap();
@ -289,7 +295,9 @@ ORDER BY departure DESC
self.remove_rowers(&mut tx).await;
for rower in &log.rower {
Rower::create(&mut tx, self.id, *rower).await;
Rower::create(&mut tx, self.id, *rower).await.map_err(|e| {
return LogbookUpdateError::RowerCreateError(*rower, e.to_string());
})?;
}
tx.commit().await.unwrap();

View File

@ -25,14 +25,69 @@ WHERE id in (SELECT rower_id FROM rower WHERE logbook_id=?)
.unwrap()
}
pub async fn create(db: &mut Transaction<'_, Sqlite>, logbook_id: i64, rower_id: i64) {
let _ = sqlx::query!(
pub async fn create(
db: &mut Transaction<'_, Sqlite>,
logbook_id: i64,
rower_id: i64,
) -> Result<(), String> {
//TODO: Check if rower is allowed to row
sqlx::query!(
"INSERT INTO rower(logbook_id, rower_id) VALUES (?,?)",
logbook_id,
rower_id
)
.execute(db)
.await
.unwrap();
.map_err(|e| return e.to_string())?;
Ok(())
}
}
#[cfg(test)]
mod test {
use sqlx::SqlitePool;
use super::Logbook;
use crate::model::{rower::Rower, user::User};
use crate::testdb;
#[sqlx::test]
fn test_for_log() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 3).await.unwrap();
let rowers = Rower::for_log(&pool, &logbook).await;
let expected = User::find_by_id(&pool, 3).await.unwrap();
assert_eq!(rowers, vec![expected]);
}
#[sqlx::test]
fn test_for_log_none() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 2).await.unwrap();
let rowers = Rower::for_log(&pool, &logbook).await;
assert_eq!(rowers, vec![]);
}
#[sqlx::test]
fn test_create() {
let pool = testdb!();
let logbook = Logbook::find_by_id(&pool, 3).await.unwrap();
let mut tx = pool.begin().await.unwrap();
Rower::create(&mut tx, logbook.id, 2).await.unwrap();
tx.commit().await.unwrap();
let rowers = Rower::for_log(&pool, &logbook).await;
assert_eq!(
rowers,
vec![
User::find_by_id(&pool, 2).await.unwrap(),
User::find_by_id(&pool, 3).await.unwrap()
]
);
}
}

View File

@ -27,6 +27,12 @@ pub struct User {
pub last_access: Option<chrono::NaiveDateTime>,
}
impl PartialEq for User {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
#[derive(Debug)]
pub enum LoginError {
InvalidAuthenticationCombo,

View File

@ -453,7 +453,7 @@ mod test {
assert_eq!(flash_cookie.value(), "7:successDanke für's helfen!");
let req = client.get("/cox/join/1");
let response = req.dispatch().await;
let _ = req.dispatch().await;
let req = client.get("/cox/remove/1");
let response = req.dispatch().await;

View File

@ -129,6 +129,7 @@ async fn create_logbook(db: &SqlitePool, data: Form<LogToAdd>) -> Flash<Redirect
Err(LogbookCreateError::BoatLocked) => Flash::error(Redirect::to("/log"), format!("Boot gesperrt")),
Err(LogbookCreateError::BoatNotFound) => Flash::error(Redirect::to("/log"), format!("Boot gibt's ned")),
Err(LogbookCreateError::TooManyRowers(expected, actual)) => Flash::error(Redirect::to("/log"), format!("Zu viele Ruderer (Boot fasst maximal {expected}, es wurden jedoch {actual} Ruderer ausgewählt)")),
Err(LogbookCreateError::RowerCreateError(rower, e)) => Flash::error(Redirect::to("/log"), format!("Fehler bei Ruderer {rower}: {e}")),
}
}
@ -159,10 +160,10 @@ async fn home_logbook(
) -> Flash<Redirect> {
let logbook: Option<Logbook> = Logbook::find_by_id(db, logbook_id).await;
let Some(logbook) = logbook else {
return Flash::error(
Redirect::to("/admin/log"),
format!("Log with ID {} does not exist!", logbook_id),
)
return Flash::error(
Redirect::to("/admin/log"),
format!("Log with ID {} does not exist!", logbook_id),
);
};
match logbook.home(db, user, data.into_inner()).await {