finish tests
This commit is contained in:
@ -31,6 +31,7 @@ pub struct PlannedEventWithUser {
|
||||
pub struct Registration {
|
||||
pub name: String,
|
||||
pub registered_at: String,
|
||||
pub is_guest: bool,
|
||||
}
|
||||
|
||||
impl PlannedEvent {
|
||||
@ -81,7 +82,11 @@ WHERE day=?",
|
||||
sqlx::query_as!(
|
||||
Registration,
|
||||
"
|
||||
SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FROM user WHERE cox_id = id) as registered_at FROM trip WHERE planned_event_id = ?
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE cox_id = id) as name,
|
||||
(SELECT created_at FROM user WHERE cox_id = id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE cox_id = id) as is_guest
|
||||
FROM trip WHERE planned_event_id = ?
|
||||
",
|
||||
self.id
|
||||
)
|
||||
@ -96,7 +101,8 @@ SELECT (SELECT name FROM user WHERE cox_id = id) as name, (SELECT created_at FRO
|
||||
"
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE user_trip.user_id = user.id) as is_guest
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM planned_event WHERE id = ?)
|
||||
",
|
||||
self.id
|
||||
|
@ -124,7 +124,8 @@ WHERE day=?
|
||||
"
|
||||
SELECT
|
||||
(SELECT name FROM user WHERE user_trip.user_id = user.id) as name,
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at
|
||||
(SELECT created_at FROM user WHERE user_trip.user_id = user.id) as registered_at,
|
||||
(SELECT is_guest FROM user WHERE user_trip.user_id = user.id) as is_guest
|
||||
FROM user_trip WHERE trip_details_id = (SELECT trip_details_id FROM trip WHERE id = ?)",
|
||||
self.id
|
||||
)
|
||||
@ -405,7 +406,7 @@ mod test {
|
||||
fn test_fail_delete_someone_registered() {
|
||||
let pool = testdb!();
|
||||
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox2".into())
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox".into())
|
||||
.await
|
||||
.unwrap()
|
||||
.try_into()
|
||||
@ -413,7 +414,12 @@ mod test {
|
||||
|
||||
let trip = Trip::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, 2, 2).await.unwrap();
|
||||
let trip_details = TripDetails::find_by_id(&pool, trip.trip_details_id.unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
|
||||
let result = trip
|
||||
.delete(&pool, &cox)
|
||||
|
@ -1,19 +1,15 @@
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::tripdetails::TripDetails;
|
||||
use super::{tripdetails::TripDetails, user::User};
|
||||
|
||||
pub struct UserTrip {}
|
||||
|
||||
impl UserTrip {
|
||||
pub async fn create(
|
||||
db: &SqlitePool,
|
||||
user_id: i64,
|
||||
trip_details_id: i64,
|
||||
user: &User,
|
||||
trip_details: &TripDetails,
|
||||
) -> Result<(), UserTripError> {
|
||||
let trip_details = TripDetails::find_by_id(db, trip_details_id)
|
||||
.await
|
||||
.ok_or(UserTripError::TripDetailsNotFound)?;
|
||||
|
||||
if trip_details.is_full(db).await {
|
||||
return Err(UserTripError::EventAlreadyFull);
|
||||
}
|
||||
@ -24,14 +20,14 @@ impl UserTrip {
|
||||
FROM trip
|
||||
WHERE trip_details_id = ?
|
||||
AND cox_id = ?",
|
||||
trip_details_id,
|
||||
user_id
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
.unwrap();
|
||||
if is_cox.amount > 0 {
|
||||
return Err(UserTripError::AlreadyRegisteredAsCox);
|
||||
return Err(UserTripError::CantRegisterAtOwnEvent);
|
||||
}
|
||||
|
||||
//check if cox if planned_event
|
||||
@ -42,8 +38,8 @@ impl UserTrip {
|
||||
SELECT id FROM planned_event WHERE trip_details_id = ?
|
||||
)
|
||||
AND cox_id = ?",
|
||||
trip_details_id,
|
||||
user_id
|
||||
trip_details.id,
|
||||
user.id
|
||||
)
|
||||
.fetch_one(db)
|
||||
.await
|
||||
@ -54,8 +50,8 @@ impl UserTrip {
|
||||
|
||||
match sqlx::query!(
|
||||
"INSERT INTO user_trip (user_id, trip_details_id) VALUES(?, ?)",
|
||||
user_id,
|
||||
trip_details_id
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
@ -65,12 +61,12 @@ impl UserTrip {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn delete(db: &SqlitePool, user_id: i64, trip_details_id: i64) {
|
||||
pub async fn delete(db: &SqlitePool, user: &User, trip_details: &TripDetails) {
|
||||
//TODO: Check if > 2 hrs to event
|
||||
let _ = sqlx::query!(
|
||||
"DELETE FROM user_trip WHERE user_id = ? AND trip_details_id = ?",
|
||||
user_id,
|
||||
trip_details_id
|
||||
user.id,
|
||||
trip_details.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
@ -78,25 +74,110 @@ impl UserTrip {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum UserTripError {
|
||||
AlreadyRegistered,
|
||||
AlreadyRegisteredAsCox,
|
||||
EventAlreadyFull,
|
||||
TripDetailsNotFound,
|
||||
CantRegisterAtOwnEvent,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
//use crate::testdb;
|
||||
use crate::{
|
||||
model::{
|
||||
planned_event::PlannedEvent, trip::Trip, tripdetails::TripDetails, user::CoxUser,
|
||||
usertrip::UserTripError,
|
||||
},
|
||||
testdb,
|
||||
};
|
||||
|
||||
//use super::User;
|
||||
//use sqlx::SqlitePool;
|
||||
use super::{User, UserTrip};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
//#[sqlx::test]
|
||||
//fn test_find_correct_id() {
|
||||
// let pool = testdb!();
|
||||
// let user = User::find_by_id(&pool, 1).await.unwrap();
|
||||
// assert_eq!(user.id, 1);
|
||||
//}
|
||||
#[sqlx::test]
|
||||
fn test_succ_create() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_full() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "rower".into()).await.unwrap();
|
||||
let user2 = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let user3 = User::find_by_name(&pool, "rower2".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
UserTrip::create(&pool, &user2, &trip_details)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user3, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because trip is already full");
|
||||
|
||||
assert_eq!(result, UserTripError::EventAlreadyFull);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_already_registered() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
UserTrip::create(&pool, &user, &trip_details).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegistered);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_own_trip() {
|
||||
let pool = testdb!();
|
||||
|
||||
let user = User::find_by_name(&pool, "cox".into()).await.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 2).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &user, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is same as responsible cox");
|
||||
|
||||
assert_eq!(result, UserTripError::CantRegisterAtOwnEvent);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_fail_create_is_cox_planned_event() {
|
||||
let pool = testdb!();
|
||||
|
||||
let cox: CoxUser = User::find_by_name(&pool, "cox".into())
|
||||
.await
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap();
|
||||
|
||||
let trip_details = TripDetails::find_by_id(&pool, 1).await.unwrap();
|
||||
let planned_event = PlannedEvent::find_by_id(&pool, 1).await.unwrap();
|
||||
|
||||
Trip::new_join(&pool, &cox, &planned_event).await.unwrap();
|
||||
|
||||
let result = UserTrip::create(&pool, &cox, &trip_details)
|
||||
.await
|
||||
.expect_err("Expect registration to fail because user is already registered as cox");
|
||||
|
||||
assert_eq!(result, UserTripError::AlreadyRegisteredAsCox);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user