learning how to test
This commit is contained in:
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user