adds comments and new tests
This commit is contained in:
@@ -26,7 +26,6 @@ pub type QueryResult<T> = Result<T, diesel::result::Error>;
|
|||||||
pub type ActionResult<R> = QueryResult<ActionStatus<R>>;
|
pub type ActionResult<R> = QueryResult<ActionStatus<R>>;
|
||||||
|
|
||||||
/// Return status of an API Action
|
/// Return status of an API Action
|
||||||
///
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
pub struct ActionStatus<R: serde::Serialize> {
|
pub struct ActionStatus<R: serde::Serialize> {
|
||||||
/// Has the action made changes ?
|
/// Has the action made changes ?
|
||||||
@@ -213,6 +212,9 @@ impl<'q> AsPlayer<'q> {
|
|||||||
pub struct AsAdmin<'q>(&'q DbConnection);
|
pub struct AsAdmin<'q>(&'q DbConnection);
|
||||||
|
|
||||||
impl<'q> AsAdmin<'q> {
|
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<()> {
|
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> {
|
||||||
diesel::insert_into(schema::players::table)
|
diesel::insert_into(schema::players::table)
|
||||||
.values(&models::player::NewPlayer::create(&name, start_wealth))
|
.values(&models::player::NewPlayer::create(&name, start_wealth))
|
||||||
@@ -220,6 +222,9 @@ impl<'q> AsAdmin<'q> {
|
|||||||
.map(ActionStatus::was_updated)
|
.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<()> {
|
pub fn resolve_claims(self) -> ActionResult<()> {
|
||||||
// Fetch all claims, grouped by items.
|
// Fetch all claims, grouped by items.
|
||||||
let loot = models::Loot::owned_by(0).load(self.0)?;
|
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)
|
/// Uses the DATABASE_URL environment variable (must be set)
|
||||||
pub fn create_pool() -> Pool {
|
pub fn create_pool() -> Pool {
|
||||||
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
let connspec = std::env::var("DATABASE_URL").expect("DATABASE_URL");
|
||||||
@@ -251,12 +256,15 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
type TestConnection = DbConnection;
|
type TestConnection = DbConnection;
|
||||||
|
|
||||||
|
/// Return a connection to a fresh database (stored in memory)
|
||||||
fn test_connection() -> TestConnection {
|
fn test_connection() -> TestConnection {
|
||||||
let test_conn = DbConnection::establish(":memory:").unwrap();
|
let test_conn = DbConnection::establish(":memory:").unwrap();
|
||||||
diesel_migrations::run_pending_migrations(&test_conn).unwrap();
|
diesel_migrations::run_pending_migrations(&test_conn).unwrap();
|
||||||
test_conn
|
test_conn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// When migrations are run, a special player with id 0 and name "Groupe"
|
||||||
|
/// must be created.
|
||||||
#[test]
|
#[test]
|
||||||
fn test_group_is_autocreated() {
|
fn test_group_is_autocreated() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
@@ -267,6 +275,8 @@ mod tests {
|
|||||||
assert_eq!(group.name, "Groupe".to_string());
|
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]
|
#[test]
|
||||||
fn test_player_updates_wealth() {
|
fn test_player_updates_wealth() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
@@ -288,7 +298,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_player() {
|
fn test_admin_add_player() {
|
||||||
let conn = test_connection();
|
let conn = test_connection();
|
||||||
let result = DbApi::with_conn(&conn).as_admin()
|
let result = DbApi::with_conn(&conn).as_admin()
|
||||||
.add_player("PlayerName".to_string(), 403.21)
|
.add_player("PlayerName".to_string(), 403.21)
|
||||||
@@ -300,6 +310,21 @@ mod tests {
|
|||||||
assert_eq!(new_player.name, "PlayerName");
|
assert_eq!(new_player.name, "PlayerName");
|
||||||
assert_eq!((new_player.cp, new_player.sp, new_player.gp, new_player.pp), (1, 2, 3, 4));
|
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() {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user