Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/bet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,59 @@ impl BetOverlord {
return (winner.to_string(), loser.to_string(), ticket_after.get_amount());
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn bet_struct_basics() {
let b = Bet::new("a", "b", 3.5);
assert_eq!(b.get_user1(), "a");
assert_eq!(b.get_user2(), "b");
assert!((b.get_amount() - 3.5).abs() < f32::EPSILON);
}

#[test]
fn bet_overlord_basic_flow() {
let mut bo = BetOverlord::new();
// add betters and relations
bo.add_better("A".to_string());
bo.add_better("B".to_string());
assert!(bo.can_bet("A"));
assert!(!bo.can_bet("C"));
bo.add_trusted("T".to_string());
assert!(bo.is_trusted("T"));
// relations and hours
bo.add_relation("A".to_string(), "Alice".to_string());
bo.add_relation("B".to_string(), "Bob".to_string());
bo.update_bet_hours("A".to_string(), 10.0);
bo.update_bet_hours("B".to_string(), 10.0);
assert!((bo.get_bet_hours("A") - 10.0).abs() < f32::EPSILON);
assert!(bo.hour_check("A", "B", 5.0));

// create a bet
let ticket = bo.handle_bet_creation("A".to_string(), "B".to_string(), 5.0);
assert_eq!(ticket, 0);
// hours deducted
assert!((bo.get_bet_hours("A") - 5.0).abs() < f32::EPSILON);
assert!((bo.get_bet_hours("B") - 5.0).abs() < f32::EPSILON);

// list_bets contains ticket info
let list = bo.list_bets();
assert!(list.contains("Bet number: 0"));

// cancel bet restores hours
let amount = bo.cancel_bet(ticket);
assert!((amount - 5.0).abs() < f32::EPSILON);
assert!((bo.get_bet_hours("A") - 10.0).abs() < f32::EPSILON);

// hour change tests
bo.update_hour_change("A".to_string(), 2.5);
assert!((bo.get_hours_change("A") - 2.5).abs() < f32::EPSILON);
bo.update_hour_change("A".to_string(), 1.25);
assert!((bo.get_hours_change("A") - 3.75).abs() < f32::EPSILON);
bo.update_hour_change("A".to_string(), 0.0);
assert!((bo.get_hours_change("A") - 0.0).abs() < f32::EPSILON);
}
}
19 changes: 19 additions & 0 deletions src/daily_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,22 @@ pub fn get_user_debts(db: Arc<Mutex<Connection>>) -> String {
pub fn round_after_math(val: f32) -> f32 {
(val * 100.0).trunc() / 100.0
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_round_after_math_positive() {
assert_eq!(round_after_math(1.2345), 1.23);
assert_eq!(round_after_math(0.0), 0.0);
assert_eq!(round_after_math(2.9999), 2.99);
}

#[test]
fn test_round_after_math_negative() {
// truncation moves toward zero for negatives
assert_eq!(round_after_math(-1.239), -1.23);
assert_eq!(round_after_math(-0.001), 0.0);
}
}
40 changes: 40 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,43 @@ pub fn update_hours_owed(conn: &Connection, id: &str, hours: f32) -> rusqlite::R
)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_user_getters_setters() {
let mut u = User::new(
"id1".to_string(),
"Alice".to_string(),
1.23,
10.0,
"steam123".to_string(),
0.0,
5.0,
);
assert_eq!(u.get_id(), "id1");
assert_eq!(u.get_name(), "Alice");
assert!((u.get_playtime() - 1.23).abs() < f32::EPSILON);
assert!((u.get_hours_owed() - 10.0).abs() < f32::EPSILON);
u.set_playtime(2.5);
assert!((u.get_playtime() - 2.5).abs() < f32::EPSILON);
u.set_hours_owed(7.75);
assert!((u.get_hours_owed() - 7.75).abs() < f32::EPSILON);
}

#[test]
fn test_time_getters_setters() {
let mut t = Time { id: 1, month: 12, week: 3, year: 2025 };
assert_eq!(t.get_month(), 12);
assert_eq!(t.get_week(), 3);
assert_eq!(t.get_year(), 2025);
t.set_month(1);
t.set_week(7);
t.set_year(2026);
assert_eq!(t.get_month(), 1);
assert_eq!(t.get_week(), 7);
assert_eq!(t.get_year(), 2026);
}
}