diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 4439256..060f270 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -226,16 +226,14 @@ impl<'q> AsPlayer<'q> { executed: true, response: Some(diff), }, - _ => ActionStatus { - executed: false, - response: None, - }, + _ => ActionStatus::nop(), }) } /// Put a claim on a specific item pub fn claim(self, item: i32) -> ActionResult<()> { // TODO: check that looted item exists - let exists: bool = diesel::select(models::Loot::exists(item)).get_result(self.conn)?; + let exists: bool = diesel::select(models::Loot::exists(item)) + .get_result(self.conn)?; if !exists { return Ok(ActionStatus::nop()); }; @@ -378,11 +376,42 @@ mod tests { #[test] fn test_player_claim_item() { - + let conn = test_connection(); + DbApi::with_conn(&conn).as_admin() + .add_player("Player".to_string(), 0.0).unwrap(); + DbApi::with_conn(&conn).as_admin() + .add_loot(vec![("Épée", 25),]).unwrap(); + // Claim an existing item + let result = DbApi::with_conn(&conn).as_player(1).claim(1).unwrap(); + assert_eq!(result.executed, true); + let claims = DbApi::with_conn(&conn).fetch_claims().unwrap(); + assert_eq!(claims.len(), 1); + let claim = claims.get(0).unwrap(); + assert_eq!(claim.player_id, 1); + assert_eq!(claim.loot_id, 1); + // Claim an inexistant item + let result = DbApi::with_conn(&conn).as_player(1).claim(2).unwrap(); + assert_eq!(result.executed, false); } #[test] fn test_player_unclaim_item() { + let conn = test_connection(); + DbApi::with_conn(&conn).as_admin() + .add_player("Player".to_string(), 0.0).unwrap(); + DbApi::with_conn(&conn).as_admin() + .add_loot(vec![("Épée", 25),]).unwrap(); + // Claim an existing item + let result = DbApi::with_conn(&conn).as_player(1).claim(1).unwrap(); + assert_eq!(result.executed, true); + let result = DbApi::with_conn(&conn).as_player(1).unclaim(1).unwrap(); + assert_eq!(result.executed, true); + // Check that unclaimed items will not be unclaimed... + let result = DbApi::with_conn(&conn).as_player(1).unclaim(1).unwrap(); + assert_eq!(result.executed, false); + let claims = DbApi::with_conn(&conn).fetch_claims().unwrap(); + assert_eq!(claims.len(), 0); + } @@ -396,8 +425,8 @@ mod tests { DbApi::with_conn(&conn).as_admin().add_player("Player".to_string(), 1000.0).unwrap(); // Buy an item let bought = DbApi::with_conn(&conn).as_player(1).buy("Sword", 800).unwrap(); - assert_eq!(bought.executed, true); - assert_eq!(bought.response, Some((0,0,0,-8))); + assert_eq!(bought.executed, true); // Was updated ? + assert_eq!(bought.response, Some((0,0,0,-8))); // Returns diff of player wealth ? let chest = DbApi::with_conn(&conn).as_player(1).loot().unwrap(); assert_eq!(chest.len(), 1); let loot = chest.get(0).unwrap(); @@ -416,6 +445,26 @@ mod tests { let player = players.get(1).unwrap(); assert_eq!(player.pp, 6); } + + #[test] + fn test_admin_add_loot() { + let conn = test_connection(); + assert_eq!(0, DbApi::with_conn(&conn).as_player(0).loot().unwrap().len()); + let loot_to_add = vec![ + ("Cape d'invisibilité", 8000), + ("Arc long", 25), + ]; + let result = DbApi::with_conn(&conn).as_admin().add_loot(loot_to_add.clone()).unwrap(); + assert_eq!(result.executed, true); + let looted = DbApi::with_conn(&conn).as_player(0).loot().unwrap(); + assert_eq!(looted.len(), 2); + // NB: Not a problem now, but this adds constraints of items being + // created in the same order. + for (added, to_add) in looted.into_iter().zip(loot_to_add) { + assert_eq!(added.name, to_add.0); + assert_eq!(added.base_price, to_add.1); + } + } }