add tests; Closes #30
This commit is contained in:
@ -50,7 +50,6 @@ pub struct BoatToAdd<'r> {
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct BoatToUpdate<'r> {
|
||||
pub id: i32,
|
||||
pub name: &'r str,
|
||||
pub amount_seats: i64,
|
||||
pub year_built: Option<i64>,
|
||||
@ -58,8 +57,8 @@ pub struct BoatToUpdate<'r> {
|
||||
pub default_shipmaster_only_steering: bool,
|
||||
pub skull: bool,
|
||||
pub external: bool,
|
||||
pub location_id: Option<i64>,
|
||||
pub owner: Option<i64>,
|
||||
pub location_id: i64,
|
||||
pub owner_id: Option<i64>,
|
||||
}
|
||||
|
||||
impl Boat {
|
||||
@ -143,7 +142,7 @@ ORDER BY amount_seats DESC
|
||||
.await.is_ok()
|
||||
}
|
||||
|
||||
pub async fn update(&self, db: &SqlitePool, boat: BoatToUpdate<'_>) -> bool {
|
||||
pub async fn update(&self, db: &SqlitePool, boat: BoatToUpdate<'_>) -> Result<(), String> {
|
||||
sqlx::query!(
|
||||
"UPDATE boat SET name=?, amount_seats=?, year_built=?, boatbuilder=?, default_shipmaster_only_steering=?, skull=?, external=?, location_id=?, owner=? WHERE id=?",
|
||||
boat.name,
|
||||
@ -154,12 +153,12 @@ ORDER BY amount_seats DESC
|
||||
boat.skull,
|
||||
boat.external,
|
||||
boat.location_id,
|
||||
boat.owner,
|
||||
boat.owner_id,
|
||||
self.id
|
||||
)
|
||||
.execute(db)
|
||||
.await
|
||||
.is_ok()
|
||||
.await.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn delete(&self, db: &SqlitePool) {
|
||||
@ -173,12 +172,17 @@ ORDER BY amount_seats DESC
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
model::boat::{Boat, BoatToAdd},
|
||||
model::{
|
||||
boat::{Boat, BoatToAdd},
|
||||
location::Location,
|
||||
},
|
||||
testdb,
|
||||
};
|
||||
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use super::BoatToUpdate;
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_find_correct_id() {
|
||||
let pool = testdb!();
|
||||
@ -247,4 +251,133 @@ mod test {
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_is_locked() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 5)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_locked(&pool)
|
||||
.await;
|
||||
assert_eq!(res, true);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_is_not_locked() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 4)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_locked(&pool)
|
||||
.await;
|
||||
assert_eq!(res, false);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_is_not_locked_no_damage() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 3)
|
||||
.await
|
||||
.unwrap()
|
||||
.is_locked(&pool)
|
||||
.await;
|
||||
assert_eq!(res, false);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_has_minor_damage() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 4)
|
||||
.await
|
||||
.unwrap()
|
||||
.has_minor_damage(&pool)
|
||||
.await;
|
||||
assert_eq!(res, true);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_has_no_minor_damage() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 5)
|
||||
.await
|
||||
.unwrap()
|
||||
.has_minor_damage(&pool)
|
||||
.await;
|
||||
assert_eq!(res, false);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_on_water() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 2)
|
||||
.await
|
||||
.unwrap()
|
||||
.on_water(&pool)
|
||||
.await;
|
||||
assert_eq!(res, true);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_not_on_water() {
|
||||
let pool = testdb!();
|
||||
let res = Boat::find_by_id(&pool, 4)
|
||||
.await
|
||||
.unwrap()
|
||||
.on_water(&pool)
|
||||
.await;
|
||||
assert_eq!(res, false);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_succ_update() {
|
||||
let pool = testdb!();
|
||||
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||
let location = Location::find_by_id(&pool, 1).await.unwrap();
|
||||
let update = BoatToUpdate {
|
||||
name: "my-new-boat-name",
|
||||
amount_seats: 3,
|
||||
year_built: None,
|
||||
boatbuilder: None,
|
||||
default_shipmaster_only_steering: false,
|
||||
skull: true,
|
||||
external: false,
|
||||
location_id: 1,
|
||||
owner_id: None,
|
||||
};
|
||||
|
||||
boat.update(&pool, update).await.unwrap();
|
||||
|
||||
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||
assert_eq!(boat.name, "my-new-boat-name");
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
fn test_failed_update() {
|
||||
let pool = testdb!();
|
||||
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||
let location = Location::find_by_id(&pool, 1).await.unwrap();
|
||||
let update = BoatToUpdate {
|
||||
name: "my-new-boat-name",
|
||||
amount_seats: 3,
|
||||
year_built: None,
|
||||
boatbuilder: None,
|
||||
default_shipmaster_only_steering: false,
|
||||
skull: true,
|
||||
external: false,
|
||||
location_id: 999,
|
||||
owner_id: None,
|
||||
};
|
||||
|
||||
match boat.update(&pool, update).await {
|
||||
Ok(_) => panic!("Update with invalid location should not succeed"),
|
||||
Err(e) => assert_eq!(
|
||||
e,
|
||||
"error returned from database: (code: 787) FOREIGN KEY constraint failed"
|
||||
),
|
||||
};
|
||||
|
||||
let boat = Boat::find_by_id(&pool, 1).await.unwrap();
|
||||
assert_eq!(boat.name, "Haichenbach");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user