refactors claims unit tests
This commit is contained in:
@@ -37,6 +37,11 @@ pub struct Claims<'q>(pub &'q DbConnection);
|
||||
|
||||
impl<'q> Claims<'q> {
|
||||
|
||||
pub fn all(&self) -> QueryResult<Vec<Claim>> {
|
||||
claims::table
|
||||
.load(self.0)
|
||||
}
|
||||
|
||||
/// Finds a single claim by association of player and loot ids.
|
||||
pub fn find(&self, player_id: i32, loot_id: i32) -> QueryResult<Claim> {
|
||||
claims::table
|
||||
@@ -50,6 +55,11 @@ impl<'q> Claims<'q> {
|
||||
// We need to validate that the claimed item exists
|
||||
// AND is actually owned by group (id 0)
|
||||
let _item = models::item::LootManager(self.0, 0).find(loot_id)?;
|
||||
// We also check if claims does not already exists
|
||||
if let Ok(_) = self.find(player_id, loot_id) {
|
||||
return Err(diesel::result::Error::RollbackTransaction);
|
||||
}
|
||||
|
||||
let claim = NewClaim::new(player_id, loot_id);
|
||||
diesel::insert_into(claims::table)
|
||||
.values(&claim)
|
||||
@@ -99,3 +109,61 @@ impl NewClaim {
|
||||
Self { player_id, loot_id }
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
type TestResult = Result<(), diesel::result::Error>;
|
||||
|
||||
fn test_connection() -> Result<DbConnection, diesel::result::Error> {
|
||||
let conn = DbConnection::establish(":memory:")
|
||||
.map_err(|_| diesel::result::Error::NotFound)?;
|
||||
diesel_migrations::run_pending_migrations(&conn)
|
||||
.map_err(|_| diesel::result::Error::NotFound)?;
|
||||
let manager = models::player::Players(&conn);
|
||||
manager.add("Player1", 0.0)?;
|
||||
manager.add("Player2", 0.0)?;
|
||||
crate::LootManager(&conn, 0)
|
||||
.add_from(&crate::Item{ id: 0, name: "Epee".to_string(), base_price: 30 })?;
|
||||
crate::LootManager(&conn, 1)
|
||||
.add_from(&crate::Item{ id: 0, name: "Arc".to_string(), base_price: 20 })?;
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_claim() -> TestResult {
|
||||
let conn = test_connection()?;
|
||||
Claims(&conn).add(1, 1)?;
|
||||
assert_eq!(Claims(&conn).all()?.len(), 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_duplicate_by_adding() -> TestResult {
|
||||
let conn = test_connection()?;
|
||||
Claims(&conn).add(1, 1)?;
|
||||
let res = Claims(&conn).add(1, 1);
|
||||
assert_eq!(res.is_err(), true);
|
||||
assert_eq!(Claims(&conn).all()?.len(), 1);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_claim() -> TestResult {
|
||||
let conn = test_connection()?;
|
||||
let claim = Claims(&conn).add(1, 1)?;
|
||||
claim.remove(&conn);
|
||||
assert_eq!(Claims(&conn).all()?.len(), 0);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_only_claim_from_group() -> TestResult {
|
||||
let conn = test_connection()?;
|
||||
let claim = Claims(&conn).add(1, 2);
|
||||
assert_eq!(claim.is_err(), true);
|
||||
assert_eq!(Claims(&conn).all()?.len(), 0);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user