learning how to test

This commit is contained in:
2019-07-12 15:56:16 +02:00
parent caa8d3fad6
commit 5d54d40bec
13 changed files with 2956 additions and 130 deletions

View File

@@ -1,6 +1,6 @@
use diesel::prelude::*;
use crate::schema::claims;
use crate::models::item::Loot;
use crate::schema::claims;
use diesel::prelude::*;
#[derive(Identifiable, Queryable, Associations, Serialize, Debug)]
#[belongs_to(Loot)]
@@ -12,7 +12,7 @@ pub struct Claim {
}
#[derive(Insertable, Debug)]
#[table_name="claims"]
#[table_name = "claims"]
pub struct NewClaim {
pub player_id: i32,
pub loot_id: i32,

View File

@@ -1,13 +1,11 @@
use diesel::prelude::*;
use diesel::dsl::{Eq, Filter, Select, exists, Find };
use diesel::expression::exists::Exists;
use crate::schema::{looted, items};
use crate::schema::{items, looted};
use crate::DbConnection;
use diesel::dsl::{exists, Eq, Filter, Find, Select};
use diesel::expression::exists::Exists;
use diesel::prelude::*;
type ItemColumns =
( looted::id, looted::name, looted::base_price, );
const ITEM_COLUMNS: ItemColumns =
( looted::id, looted::name, looted::base_price, );
type ItemColumns = (looted::id, looted::name, looted::base_price);
const ITEM_COLUMNS: ItemColumns = (looted::id, looted::name, looted::base_price);
type OwnedBy = Select<OwnedLoot, ItemColumns>;
/// Represents a unique item in inventory
@@ -25,8 +23,7 @@ pub struct Item {
impl Item {
/// Public proxy for Loot::owned_by that selects only Item fields
pub fn owned_by(player: i32) -> OwnedBy {
Loot::owned_by(player)
.select(ITEM_COLUMNS)
Loot::owned_by(player).select(ITEM_COLUMNS)
}
}
@@ -35,7 +32,7 @@ type OwnedLoot = Filter<looted::table, WithOwner>;
/// Represents an item that has been looted
#[derive(Identifiable, Debug, Queryable, Serialize)]
#[table_name="looted"]
#[table_name = "looted"]
pub(crate) struct Loot {
id: i32,
name: String,
@@ -46,8 +43,7 @@ pub(crate) struct Loot {
impl Loot {
/// A filter on Loot that is owned by given player
pub(crate) fn owned_by(id: i32) -> OwnedLoot {
looted::table
.filter(looted::owner_id.eq(id))
looted::table.filter(looted::owner_id.eq(id))
}
pub(crate) fn exists(id: i32) -> Exists<Find<looted::table, i32>> {
@@ -55,7 +51,9 @@ impl Loot {
}
}
/// Description of an item : (name, value in gold)
type ItemDesc<'a> = (&'a str, i32);
/// An item being looted or bought.
///
/// The owner is set to 0 in case of looting,
@@ -69,6 +67,7 @@ struct NewLoot<'a> {
}
impl<'a> NewLoot<'a> {
/// A new loot going to the group (loot procedure)
fn to_group(desc: ItemDesc<'a>) -> Self {
Self {
name: desc.0,
@@ -77,6 +76,7 @@ impl<'a> NewLoot<'a> {
}
}
/// A new loot going to a specific player (buy procedure)
fn to_player(player: i32, desc: ItemDesc<'a>) -> Self {
Self {
name: desc.0,

View File

@@ -1,8 +1,8 @@
mod claim;
mod item;
mod player;
mod claim;
pub use claim::{Claim, NewClaim};
pub use item::Item;
pub(crate) use item::Loot;
pub use player::{Player, NewPlayer, WealthUpdate};
pub use claim::{NewClaim, Claim};
pub use player::{NewPlayer, Player, WealthUpdate};

View File

@@ -1,6 +1,6 @@
use diesel::prelude::*;
use crate::schema::players;
use crate::DbConnection;
use diesel::prelude::*;
/// Representation of a player in database
#[derive(Debug, Queryable, Serialize)]
@@ -14,7 +14,6 @@ pub struct Player {
pp: i32,
}
/// Unpack a floating value in gold pieces to integer
/// values of copper, silver, gold and platinum pieces
///
@@ -38,33 +37,39 @@ fn unpack_gold_value(gold: f32) -> (i32, i32, i32, i32) {
/// The values held here are the amount of pieces to add or
/// substract to player wealth.
#[derive(Queryable, AsChangeset, Debug)]
#[table_name="players"]
#[table_name = "players"]
pub struct WealthUpdate {
cp: i32,
sp: i32,
gp: i32,
pp: i32
pp: i32,
}
impl WealthUpdate{
impl WealthUpdate {
/// Unpack individual pieces counts from gold value
pub fn from_gp(gp: f32) -> Self {
let (cp, sp, gp, pp) = unpack_gold_value(gp);
Self { cp, sp, gp, pp }
}
/// Convert total value to a floating value in gold pieces
///
/// # Examples
/// ```
/// # use lootalot_db::models::WealthUpdate;
/// let wealth = WealthUpdate::from_gp(403.21);
/// assert_eq!(wealth.to_gp(), 403.21);
/// ```
pub fn to_gp(&self) -> f32 {
let i = self.pp * 100 + self.gp;
let f = ( self.sp * 10 + self.cp ) as f32 / 100.0;
let f = (self.sp * 10 + self.cp) as f32 / 100.0;
i as f32 + f
}
/// Pack the counts inside a tuple, from lower to higher coin value.
pub fn as_tuple(&self) -> (i32, i32, i32, i32) {
(self.cp, self.sp, self.gp, self.pp)
}
}
/// Representation of a new player record
#[derive(Insertable)]
#[table_name = "players"]
@@ -98,13 +103,13 @@ mod tests {
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))
(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() {