diff --git a/lootalot_db/src/lib.rs b/lootalot_db/src/lib.rs index 260c96a..a03cd0b 100644 --- a/lootalot_db/src/lib.rs +++ b/lootalot_db/src/lib.rs @@ -26,7 +26,6 @@ pub type QueryResult = Result; pub type ActionResult = QueryResult>; /// Return status of an API Action -/// #[derive(Serialize, Debug)] pub struct ActionStatus { /// Has the action made changes ? @@ -213,6 +212,9 @@ impl<'q> AsPlayer<'q> { pub struct AsAdmin<'q>(&'q DbConnection); impl<'q> AsAdmin<'q> { + /// Adds a player to the database + /// + /// Takes the player name and starting wealth (in gold value). pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> { diesel::insert_into(schema::players::table) .values(&models::player::NewPlayer::create(&name, start_wealth)) @@ -220,6 +222,9 @@ impl<'q> AsAdmin<'q> { .map(ActionStatus::was_updated) } + /// Resolve all pending claims and dispatch claimed items. + /// + /// When a player gets an item, it's debt is increased by this item sell value pub fn resolve_claims(self) -> ActionResult<()> { // Fetch all claims, grouped by items. let loot = models::Loot::owned_by(0).load(self.0)?; @@ -235,7 +240,7 @@ impl<'q> AsAdmin<'q> { } } -/// Create a connection pool and returns it. +/// Sets up a connection pool and returns it. /// Uses the DATABASE_URL environment variable (must be set) pub fn create_pool() -> Pool { let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL"); @@ -251,12 +256,15 @@ mod tests { use super::*; type TestConnection = DbConnection; + /// Return a connection to a fresh database (stored in memory) fn test_connection() -> TestConnection { let test_conn = DbConnection::establish(":memory:").unwrap(); diesel_migrations::run_pending_migrations(&test_conn).unwrap(); test_conn } + /// When migrations are run, a special player with id 0 and name "Groupe" + /// must be created. #[test] fn test_group_is_autocreated() { let conn = test_connection(); @@ -267,6 +275,8 @@ mod tests { assert_eq!(group.name, "Groupe".to_string()); } + /// When a player updates wealth, a difference is returned by API. + /// Added to the previous amount of coins, it should equal the updated weath. #[test] fn test_player_updates_wealth() { let conn = test_connection(); @@ -288,7 +298,7 @@ mod tests { } #[test] - fn test_add_player() { + fn test_admin_add_player() { let conn = test_connection(); let result = DbApi::with_conn(&conn).as_admin() .add_player("PlayerName".to_string(), 403.21) @@ -300,6 +310,21 @@ mod tests { assert_eq!(new_player.name, "PlayerName"); assert_eq!((new_player.cp, new_player.sp, new_player.gp, new_player.pp), (1, 2, 3, 4)); } + + #[test] + fn test_admin_resolve_claims() { + + } + + #[test] + fn test_player_claim_item() { + + } + + #[test] + fn test_player_unclaim_item() { + + } }