learning how to test
This commit is contained in:
@@ -5,10 +5,10 @@ extern crate diesel;
|
||||
extern crate serde_derive;
|
||||
|
||||
use diesel::prelude::*;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
use diesel::query_dsl::RunQueryDsl;
|
||||
use diesel::r2d2::{self, ConnectionManager};
|
||||
|
||||
mod models;
|
||||
pub mod models;
|
||||
mod schema;
|
||||
|
||||
/// The connection used
|
||||
@@ -49,7 +49,7 @@ impl ActionStatus<()> {
|
||||
/// It offers a convenient way to deal with connection
|
||||
///
|
||||
/// # Todo list
|
||||
/// ```
|
||||
/// ```text
|
||||
/// 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)
|
||||
@@ -77,7 +77,9 @@ impl<'q> DbApi<'q> {
|
||||
///
|
||||
/// # Usage
|
||||
/// ```
|
||||
/// let conn = DbConnection::establish();
|
||||
/// use lootalot_db::{DbConnection, DbApi};
|
||||
/// # use diesel::connection::Connection;
|
||||
/// let conn = DbConnection::establish(":memory:").unwrap();
|
||||
/// let api = DbApi::with_conn(&conn);
|
||||
/// ```
|
||||
pub fn with_conn(conn: &'q DbConnection) -> Self {
|
||||
@@ -87,26 +89,17 @@ impl<'q> DbApi<'q> {
|
||||
///
|
||||
/// This method consumes the DbApi object.
|
||||
pub fn fetch_players(self) -> QueryResult<Vec<models::Player>> {
|
||||
Ok(
|
||||
schema::players::table
|
||||
.load::<models::Player>(self.0)?
|
||||
)
|
||||
Ok(schema::players::table.load::<models::Player>(self.0)?)
|
||||
}
|
||||
/// Fetch the inventory of items
|
||||
///
|
||||
/// Consumes the DbApi instance
|
||||
pub fn fetch_inventory(self) -> QueryResult<Vec<models::Item>> {
|
||||
Ok(
|
||||
schema::items::table
|
||||
.load::<models::Item>(self.0)?
|
||||
)
|
||||
Ok(schema::items::table.load::<models::Item>(self.0)?)
|
||||
}
|
||||
|
||||
pub fn fetch_claims(self) -> QueryResult<Vec<models::Claim>> {
|
||||
Ok(
|
||||
schema::claims::table
|
||||
.load::<models::Claim>(self.0)?
|
||||
)
|
||||
Ok(schema::claims::table.load::<models::Claim>(self.0)?)
|
||||
}
|
||||
/// Wrapper for acting as a specific player
|
||||
///
|
||||
@@ -114,6 +107,10 @@ impl<'q> DbApi<'q> {
|
||||
///
|
||||
/// # Usage
|
||||
/// ```
|
||||
/// # use lootalot_db::{DbConnection, DbApi};
|
||||
/// # use diesel::connection::Connection;
|
||||
/// # let conn = DbConnection::establish(":memory:").unwrap();
|
||||
/// # let api = DbApi::with_conn(&conn);
|
||||
/// let player_id: i32 = 1; // Id that references player in DB
|
||||
/// let player = api.as_player(player_id);
|
||||
/// ```
|
||||
@@ -136,36 +133,37 @@ pub struct AsPlayer<'q> {
|
||||
|
||||
impl<'q> AsPlayer<'q> {
|
||||
/// Fetch the content of a player's chest
|
||||
///
|
||||
/// # Usage
|
||||
/// ```
|
||||
/// # extern crate diesel_migrations;
|
||||
/// # use lootalot_db::{DbConnection, DbApi};
|
||||
/// # use diesel::connection::Connection;
|
||||
/// # let conn = DbConnection::establish(":memory:").unwrap();
|
||||
/// # diesel_migrations::run_pending_migrations(&conn).unwrap();
|
||||
/// # let api = DbApi::with_conn(&conn);
|
||||
/// // Get loot of player with id of 1
|
||||
/// let loot = api.as_player(1).loot().unwrap();
|
||||
/// assert_eq!(format!("{:?}", loot), "[]".to_string());
|
||||
/// ```
|
||||
pub fn loot(self) -> QueryResult<Vec<models::Item>> {
|
||||
Ok(
|
||||
models::Item::owned_by(self.id)
|
||||
.load(self.conn)?
|
||||
)
|
||||
Ok(models::Item::owned_by(self.id).load(self.conn)?)
|
||||
}
|
||||
/// Adds the value in gold to the player's wealth.
|
||||
///
|
||||
/// Value can be negative to substract wealth.
|
||||
pub fn update_wealth(self, value_in_gp: f32)
|
||||
-> ActionResult<Option<(i32, i32, i32, i32)>> {
|
||||
pub fn update_wealth(self, value_in_gp: f32) -> ActionResult<Option<(i32, i32, i32, i32)>> {
|
||||
use schema::players::dsl::*;
|
||||
let current_wealth = players
|
||||
.find(self.id)
|
||||
.select((cp, sp, gp, pp))
|
||||
.first::<models::WealthUpdate>(self.conn)?;
|
||||
.find(self.id)
|
||||
.select((cp, sp, gp, pp))
|
||||
.first::<models::WealthUpdate>(self.conn)?;
|
||||
// TODO: improve this
|
||||
// should be move inside a WealthUpdate method
|
||||
let update = models::WealthUpdate::from_gp(
|
||||
current_wealth.to_gp() + value_in_gp
|
||||
);
|
||||
let update = models::WealthUpdate::from_gp(current_wealth.to_gp() + value_in_gp);
|
||||
// Difference in coins that is sent back
|
||||
let (old, new) =
|
||||
(current_wealth.as_tuple(), update.as_tuple());
|
||||
let diff = (
|
||||
new.0 - old.0,
|
||||
new.1 - old.1,
|
||||
new.2 - old.2,
|
||||
new.3 - old.3
|
||||
);
|
||||
let (old, new) = (current_wealth.as_tuple(), update.as_tuple());
|
||||
let diff = (new.0 - old.0, new.1 - old.1, new.2 - old.2, new.3 - old.3);
|
||||
diesel::update(players)
|
||||
.filter(id.eq(self.id))
|
||||
.set(&update)
|
||||
@@ -175,26 +173,31 @@ impl<'q> AsPlayer<'q> {
|
||||
1 => ActionStatus {
|
||||
executed: true,
|
||||
response: Some(diff),
|
||||
},
|
||||
},
|
||||
_ => ActionStatus {
|
||||
executed: false,
|
||||
response: None,
|
||||
}, })
|
||||
},
|
||||
})
|
||||
}
|
||||
/// 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())
|
||||
return Ok(ActionStatus::nop());
|
||||
};
|
||||
let request = models::NewClaim {
|
||||
player_id: self.id,
|
||||
loot_id: item,
|
||||
};
|
||||
let request = models::NewClaim { player_id: self.id, loot_id: item };
|
||||
diesel::insert_into(schema::claims::table)
|
||||
.values(&request)
|
||||
.execute(self.conn)
|
||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
||||
.map(|r| match r {
|
||||
1 => ActionStatus::ok(),
|
||||
_ => ActionStatus::nop(),
|
||||
})
|
||||
}
|
||||
/// Withdraw claim
|
||||
pub fn unclaim(self, item: i32) -> ActionResult<()> {
|
||||
@@ -202,9 +205,13 @@ impl<'q> AsPlayer<'q> {
|
||||
diesel::delete(
|
||||
claims
|
||||
.filter(loot_id.eq(item))
|
||||
.filter(player_id.eq(self.id)))
|
||||
.execute(self.conn)
|
||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
||||
.filter(player_id.eq(self.id)),
|
||||
)
|
||||
.execute(self.conn)
|
||||
.map(|r| match r {
|
||||
1 => ActionStatus::ok(),
|
||||
_ => ActionStatus::nop(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,12 +219,14 @@ impl<'q> AsPlayer<'q> {
|
||||
pub struct AsAdmin<'q>(&'q DbConnection);
|
||||
|
||||
impl<'q> AsAdmin<'q> {
|
||||
|
||||
pub fn add_player(self, name: String, start_wealth: f32) -> ActionResult<()> {
|
||||
diesel::insert_into(schema::players::table)
|
||||
.values(&models::NewPlayer::create(&name, start_wealth))
|
||||
.execute(self.0)
|
||||
.map(|r| match r { 1 => ActionStatus::ok(), _ => ActionStatus::nop() })
|
||||
.map(|r| match r {
|
||||
1 => ActionStatus::ok(),
|
||||
_ => ActionStatus::nop(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolve_claims(self) -> ActionResult<()> {
|
||||
@@ -239,7 +248,7 @@ impl<'q> AsAdmin<'q> {
|
||||
/// 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 );
|
||||
dbg!(&connspec);
|
||||
let manager = ConnectionManager::<DbConnection>::new(connspec);
|
||||
r2d2::Pool::builder()
|
||||
.build(manager)
|
||||
@@ -247,6 +256,4 @@ pub fn create_pool() -> Pool {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
}
|
||||
mod tests {}
|
||||
|
||||
Reference in New Issue
Block a user