Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles committed Jul 22, 2024
1 parent 792293c commit 2179235
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
53 changes: 49 additions & 4 deletions backend/integration_tests/src/chit_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use pocket_ic::PocketIc;
use types::{ChitEarnedReason, Milliseconds, TimestampMillis};

use crate::env::ENV;
use crate::utils::now_millis;
use crate::{client, TestEnv};
use std::ops::Deref;
use pocket_ic::PocketIc;
use std::ops::{Add, Deref};
use std::time::{Duration, SystemTime};
use types::{ChitEarnedReason, Milliseconds, TimestampMillis};
use utils::time::MonthKey;

const DAY_ZERO: TimestampMillis = 1704067200000; // Mon Jan 01 2024 00:00:00 GMT+0000
const MS_IN_DAY: Milliseconds = 1000 * 60 * 60 * 24;
Expand Down Expand Up @@ -80,6 +80,51 @@ fn chit_streak_gained_and_lost_as_expected() {
assert_eq!(result.users[0].volatile.as_ref().unwrap().streak, 0);
}

#[test]
fn chit_stored_per_month() {
let mut wrapper = ENV.deref().get();
let TestEnv { env, canister_ids, .. } = wrapper.env();

let user = client::register_user(env, canister_ids);
ensure_time_at_least_day0(env);
advance_to_next_month(env);
let start_month = MonthKey::from_timestamp(now_millis(env));

for i in 1..5 {
for j in 0..i {
client::user::happy_path::claim_daily_chit(env, &user);
env.advance_time(Duration::from_millis(2 * MS_IN_DAY));
}
advance_to_next_month(env);
}

env.tick();

let mut month = start_month;
for i in 1..5 {
let chit_balance = client::user_index::happy_path::chit_balances(
env,
canister_ids.user_index,
vec![user.user_id],
month.year() as u16,
month.month(),
)
.values()
.next()
.unwrap();

assert_eq!(chit_balance, i * 200);

Check failure on line 116 in backend/integration_tests/src/chit_tests.rs

View workflow job for this annotation

GitHub Actions / run clippy

can't compare `&i32` with `{integer}`

month = month.next();
}
}

fn advance_to_next_month(env: &mut PocketIc) {
let now = now_millis(env);
let next_month = MonthKey::from_timestamp(now).next();
env.set_time(SystemTime::UNIX_EPOCH.add(Duration::from_millis(next_month.start_timestamp())));
}

fn ensure_time_at_least_day0(env: &mut PocketIc) {
if now_millis(env) < DAY_ZERO {
env.set_time(SystemTime::now());
Expand Down
21 changes: 21 additions & 0 deletions backend/integration_tests/src/client/user_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use user_index_canister::*;

// Queries
generate_query_call!(check_username);
generate_query_call!(chit_balances);
generate_query_call!(current_user);
generate_query_call!(search);
generate_query_call!(platform_moderators);
Expand Down Expand Up @@ -31,6 +32,7 @@ generate_update_call!(upgrade_user_canister_wasm);
pub mod happy_path {
use candid::Principal;
use pocket_ic::PocketIc;
use std::collections::HashMap;
use types::{
CanisterId, CanisterWasm, Cryptocurrency, DiamondMembershipFees, DiamondMembershipPlanDuration, Empty, UserId,
};
Expand Down Expand Up @@ -212,4 +214,23 @@ pub mod happy_path {
user_index_canister::add_platform_operator::Response::Success => {}
}
}

pub fn chit_balances(
env: &PocketIc,
user_index_canister_id: CanisterId,
user_ids: Vec<UserId>,
year: u16,
month: u8,
) -> HashMap<UserId, i32> {
let response = super::chit_balances(
env,
Principal::anonymous(),
user_index_canister_id,
&user_index_canister::chit_balances::Args { users, year, month },

Check failure on line 229 in backend/integration_tests/src/client/user_index.rs

View workflow job for this annotation

GitHub Actions / run clippy

mismatched types
);

match response {
user_index_canister::chit_balances::Response::Success(result) => result.balances,
}
}
}
2 changes: 1 addition & 1 deletion backend/libraries/utils/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl MonthKey {
start..end
}

fn start_timestamp(&self) -> TimestampMillis {
pub fn start_timestamp(&self) -> TimestampMillis {
let date = time::Date::from_calendar_date(self.year as i32, self.month.try_into().unwrap(), 1).unwrap();
(OffsetDateTime::new_utc(date, Time::MIDNIGHT).unix_timestamp() * 1000) as TimestampMillis
}
Expand Down

0 comments on commit 2179235

Please sign in to comment.