add unit tests

This commit is contained in:
2023-04-26 16:54:53 +02:00
parent 9236113507
commit 346a9b1d91
8 changed files with 325 additions and 135 deletions

View File

@ -8,6 +8,7 @@ use sqlx::SqlitePool;
use crate::model::{
log::Log,
planned_event::PlannedEvent,
trip::{CoxHelpError, Trip, TripDeleteError, TripUpdateError},
tripdetails::TripDetails,
user::CoxUser,
@ -33,10 +34,11 @@ async fn create(db: &State<SqlitePool>, data: Form<AddTripForm>, cox: CoxUser) -
data.notes.clone(),
)
.await;
let trip_details = TripDetails::find_by_id(db, trip_details_id).await.unwrap(); //Okay, bc just
//created
Trip::new_own(db, &cox, trip_details).await;
//TODO: fix clone()
Trip::new_own(db, cox.id, trip_details_id).await;
Log::create(
db,
format!(
@ -65,72 +67,88 @@ async fn update(
trip_id: i64,
cox: CoxUser,
) -> Flash<Redirect> {
match Trip::update_own(db, cox.id, trip_id, data.max_people, data.notes.clone()).await {
Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."),
Err(TripUpdateError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
Err(TripUpdateError::TripDoesNotExist) => {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
if let Some(trip) = Trip::find_by_id(db, trip_id).await {
match Trip::update_own(db, &cox, &trip, data.max_people, data.notes.clone()).await {
Ok(_) => Flash::success(Redirect::to("/"), "Ausfahrt erfolgreich aktualisiert."),
Err(TripUpdateError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
Err(TripUpdateError::TripDetailsDoesNotExist) => {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
}
}
} else {
Flash::error(Redirect::to("/"), "Ausfahrt gibt's nicht")
}
}
#[get("/join/<planned_event_id>")]
async fn join(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
match Trip::new_join(db, cox.id, planned_event_id).await {
Ok(_) => {
Log::create(
db,
format!(
"Cox {} helps at planned_event.id={}",
cox.name, planned_event_id,
),
)
.await;
Flash::success(Redirect::to("/"), "Danke für's helfen!")
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
match Trip::new_join(db, &cox, &planned_event).await {
Ok(_) => {
Log::create(
db,
format!(
"Cox {} helps at planned_event.id={}",
cox.name, planned_event_id,
),
)
.await;
Flash::success(Redirect::to("/"), "Danke für's helfen!")
}
Err(CoxHelpError::AlreadyRegisteredAsCox) => {
Flash::error(Redirect::to("/"), "Du hilfst bereits aus!")
}
Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error(
Redirect::to("/"),
"Du hast dich bereits als Ruderer angemeldet!",
),
}
Err(CoxHelpError::AlreadyRegisteredAsCox) => {
Flash::error(Redirect::to("/"), "Du hilfst bereits aus!")
}
Err(CoxHelpError::AlreadyRegisteredAsRower) => Flash::error(
Redirect::to("/"),
"Du hast dich bereits als Ruderer angemeldet!",
),
} else {
Flash::error(Redirect::to("/"), "Event gibt's nicht")
}
}
#[get("/remove/trip/<trip_id>")]
async fn remove_trip(db: &State<SqlitePool>, trip_id: i64, cox: CoxUser) -> Flash<Redirect> {
match Trip::delete(db, cox.id, trip_id).await {
Ok(_) => {
Log::create(db, format!("Cox {} deleted trip.id={}", cox.name, trip_id)).await;
Flash::success(Redirect::to("/"), "Erfolgreich gelöscht!")
}
Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error(
Redirect::to("/"),
"Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!",
),
Err(TripDeleteError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
let trip = Trip::find_by_id(db, trip_id).await;
match trip {
None => Flash::error(Redirect::to("/"), "Trip gibt's nicht!"),
Some(trip) => match trip.delete(db, &cox).await {
Ok(_) => {
Log::create(db, format!("Cox {} deleted trip.id={}", cox.name, trip_id)).await;
Flash::success(Redirect::to("/"), "Erfolgreich gelöscht!")
}
Err(TripDeleteError::SomebodyAlreadyRegistered) => Flash::error(
Redirect::to("/"),
"Ausfahrt kann nicht gelöscht werden, da bereits jemand registriert ist!",
),
Err(TripDeleteError::NotYourTrip) => {
Flash::error(Redirect::to("/"), "Nicht deine Ausfahrt!")
}
},
}
}
#[get("/remove/<planned_event_id>")]
async fn remove(db: &State<SqlitePool>, planned_event_id: i64, cox: CoxUser) -> Flash<Redirect> {
Trip::delete_by_planned_event_id(db, cox.id, planned_event_id).await;
if let Some(planned_event) = PlannedEvent::find_by_id(db, planned_event_id).await {
Trip::delete_by_planned_event(db, &cox, &planned_event).await;
Log::create(
db,
format!(
"Cox {} deleted registration for planned_event.id={}",
cox.name, planned_event_id
),
)
.await;
Log::create(
db,
format!(
"Cox {} deleted registration for planned_event.id={}",
cox.name, planned_event_id
),
)
.await;
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
Flash::success(Redirect::to("/"), "Erfolgreich abgemeldet!")
} else {
Flash::error(Redirect::to("/"), "Planned_event does not exist.")
}
}
pub fn routes() -> Vec<Route> {