adds docs, tests unpack_gold_value

This commit is contained in:
2019-07-11 15:35:32 +02:00
parent e08cd64743
commit caa8d3fad6
4 changed files with 30 additions and 17 deletions

Binary file not shown.

View File

@@ -11,17 +11,23 @@ use diesel::query_dsl::RunQueryDsl;
mod models;
mod schema;
/// The connection used
pub type DbConnection = SqliteConnection;
/// A pool of connections
pub type Pool = r2d2::Pool<ConnectionManager<DbConnection>>;
/// The result of a query on DB
pub type QueryResult<T> = Result<T, diesel::result::Error>;
/// The result of an action provided by DbApi
pub type ActionResult<R> = QueryResult<ActionStatus<R>>;
/// Return status of an API Action
///
#[derive(Serialize, Debug)]
pub struct ActionStatus<R: serde::Serialize> {
/// Has the action made changes ?
executed: bool,
pub executed: bool,
/// Response payload
response: R,
pub response: R,
}
impl ActionStatus<()> {
@@ -43,7 +49,7 @@ impl ActionStatus<()> {
/// It offers a convenient way to deal with connection
///
/// # Todo list
///
/// ```
/// struct DbApi<'q>(&'q DbConnection);
/// ::new() -> DbApi<'q> (Db finds a connection by itself, usefull for cli)
/// ::with_conn(conn) -> DbApi<'q> (uses a user-defined connection)
@@ -62,6 +68,7 @@ impl ActionStatus<()> {
/// x .sell_loot([players], [excluded_item_ids]) -> Success status (bool, player_share)
/// x .resolve_claims()
/// v .add_player(player_data)
/// ```
///
pub struct DbApi<'q>(&'q DbConnection);
@@ -201,7 +208,7 @@ impl<'q> AsPlayer<'q> {
}
}
/// Wrapper for interactions of admins with the DB.
pub struct AsAdmin<'q>(&'q DbConnection);
impl<'q> AsAdmin<'q> {
@@ -228,6 +235,8 @@ impl<'q> AsAdmin<'q> {
}
}
/// Create 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");
dbg!( &connspec );

View File

@@ -95,19 +95,21 @@ mod tests {
use crate::tests;
#[test]
fn new_player_only_with_name() {
let n = NewPlayer::new("Féfi", None);
assert_eq!(n.name, "Féfi");
assert_eq!(n.cp, 0);
assert_eq!(n.sp, 0);
assert_eq!(n.gp, 0);
assert_eq!(n.pp, 0);
fn test_unpack_gold_values() {
use super::unpack_gold_value;
let test_values = [
(1.0, (0,0,1,0)),
(1.23, (3,2,1,0)),
(1.03, (3,0,1,0)),
(100.23, (3,2,0,1)),
(-100.23, (-3,-2,-0,-1)),
(10189.23, (3,2,89,101)),
(-8090.20, (0,-2,-90,-80))
];
for (tested, expected) in test_values.into_iter() {
assert_eq!(unpack_gold_value(*tested), *expected);
}
}
#[test]
fn new_player_with_wealth() {
let initial_wealth = (1, 2, 3, 4);
let n = NewPlayer::new("Féfi", Some(initial_wealth));
assert_eq!(initial_wealth, (n.cp, n.sp, n.gp, n.pp));
}
}