Skip to content

Commit

Permalink
max_length and total_length can be negative
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Kozarin committed Jul 12, 2024
1 parent 6000501 commit d38bb7e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/repo/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ struct PersonalStatsEntity {

pub struct PersonalStats {
pub chats: u64,
pub max_length: u32,
pub total_length: u64,
pub max_length: i32,
pub total_length: i64,
}

impl From<PersonalStatsEntity> for PersonalStats {
fn from(value: PersonalStatsEntity) -> Self {
Self {
chats: value.chats.map(|x| x.to_u64().expect("chats count, fetched from the database, must fit into u64")).unwrap_or_default(),
max_length: value.max_length.map(|x| x.to_u32().expect("max_length, fetched from the database, must fit into u32")).unwrap_or_default(),
total_length: value.total_length.map(|x| x.to_u64().expect("total_length, fetched from the database, must fit into u64")).unwrap_or_default(),
max_length: value.max_length.unwrap_or_default(),
total_length: value.total_length.unwrap_or_default(),
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions src/repo/test/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,27 @@ async fn test_all() {
assert_eq!(stats.max_length, 0);
assert_eq!(stats.total_length, 0);

dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_1), 10).await
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_1.clone()), 10).await
.expect("couldn't grow the dick in the first chat");
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_2), 20).await
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_2.clone()), 20).await
.expect("couldn't grow the dick in the second chat");

let stats = personal_stats.get(uid).await
.expect("couldn't fetch the non-null stats");
assert_eq!(stats.chats, 2);
assert_eq!(stats.max_length, 20);
assert_eq!(stats.total_length, 30);

sqlx::query!("DROP TRIGGER IF EXISTS trg_check_and_update_dicks_timestamp ON Dicks")
.execute(&db)
.await.expect("couldn't drop the trigger");

dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_1), -20).await
.expect("couldn't shrink the dick in the first chat");
dicks.create_or_grow(uid, &ChatIdPartiality::Specific(chat_id_2), -40).await
.expect("couldn't shrink the dick in the second chat");
let stats = personal_stats.get(uid).await
.expect("couldn't fetch the stats with negative lengths");
assert_eq!(stats.max_length, -10);
assert_eq!(stats.total_length, -30);
}

0 comments on commit d38bb7e

Please sign in to comment.