Skip to content

Commit

Permalink
Extract config api and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia committed Nov 3, 2023
1 parent 2cd57d4 commit 962f248
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 16 deletions.
2 changes: 1 addition & 1 deletion contract/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ pub(crate) mod tests;

pub(crate) fn unix_timestamp(ms: u64) -> UnixTimestamp {
u32::try_from(ms / 1000)
.unwrap_or_else(|err| panic_str(&format!("Failed to get convert milliseconds to Unix timestamp: {err}")))
.unwrap_or_else(|err| panic_str(&format!("Failed to get convert milliseconds to Unix timestamp")))
}
19 changes: 18 additions & 1 deletion contract/src/common/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;
use model::api::InitApi;
use near_sdk::{test_utils::VMContextBuilder, testing_env, AccountId};

use crate::Contract;
use crate::{common::unix_timestamp, Contract};

pub(crate) struct Context {
builder: VMContextBuilder,
Expand Down Expand Up @@ -138,3 +138,20 @@ pub(crate) mod data {
assert!(get_test_future_success(name));
}
}

#[test]
fn convert_milliseconds_to_unix_timestamp_successfully() {
let millis: u64 = 1_699_038_575_819;
let timestamp = unix_timestamp(millis);

assert_eq!(1_699_038_575, timestamp);
}

#[test]
#[should_panic(expected = "Failed to get convert milliseconds to Unix timestamp")]
fn convert_milliseconds_to_unix_timestamp_with_unsuccessfully() {
let millis: u64 = u64::MAX;
let timestamp = unix_timestamp(millis);

assert_eq!(1_699_038_575, timestamp);
}
24 changes: 24 additions & 0 deletions contract/src/config/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use model::Duration;
use near_sdk::near_bindgen;

use crate::{Contract, ContractExt};

pub trait ConfigApi {
fn set_claim_period(&mut self, period: Duration);
fn set_burn_period(&mut self, period: Duration);
}

#[near_bindgen]
impl ConfigApi for Contract {
fn set_claim_period(&mut self, period: Duration) {
self.assert_oracle();

self.claim_period = period;
}

fn set_burn_period(&mut self, period: Duration) {
self.assert_oracle();

self.burn_period = period;
}
}
2 changes: 2 additions & 0 deletions contract/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) mod api;
mod tests;
57 changes: 57 additions & 0 deletions contract/src/config/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![cfg(test)]

use crate::{common::tests::Context, config::api::ConfigApi};

#[test]
fn set_claim_period_by_oracle() {
let (mut context, mut contract, accounts) = Context::init_with_oracle();

let claim_period = 1_000_000;
context.switch_account(&accounts.oracle);
contract.set_claim_period(claim_period);

assert_eq!(claim_period, contract.claim_period);

let claim_period = 3_000_000;
context.switch_account(&accounts.oracle);
contract.set_claim_period(claim_period);

assert_eq!(claim_period, contract.claim_period);
}

#[test]
#[should_panic(expected = "Unauthorized access")]
fn set_claim_period_by_not_oracle() {
let (mut context, mut contract, accounts) = Context::init_with_oracle();

let claim_period = 1_000_000;
context.switch_account(&accounts.alice);
contract.set_claim_period(claim_period);
}

#[test]
fn set_burn_period_by_oracle() {
let (mut context, mut contract, accounts) = Context::init_with_oracle();

let burn_period = 1_000_000;
context.switch_account(&accounts.oracle);
contract.set_burn_period(burn_period);

assert_eq!(burn_period, contract.burn_period);

let burn_period = 3_000_000;
context.switch_account(&accounts.oracle);
contract.set_burn_period(burn_period);

assert_eq!(burn_period, contract.burn_period);
}

#[test]
#[should_panic(expected = "Unauthorized access")]
fn set_burn_period_by_not_oracle() {
let (mut context, mut contract, accounts) = Context::init_with_oracle();

let burn_period = 1_000_000;
context.switch_account(&accounts.alice);
contract.set_burn_period(burn_period);
}
13 changes: 1 addition & 12 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod burn;
mod claim;
mod clean;
mod common;
mod config;
mod record;

const INITIAL_CLAIM_PERIOD_MS: u32 = 24 * 60 * 60;
Expand Down Expand Up @@ -58,16 +59,4 @@ impl InitApi for Contract {
is_service_call_running: false,
}
}

fn set_claim_period(&mut self, period: Duration) {
self.assert_oracle();

self.claim_period = period;
}

fn set_burn_period(&mut self, period: Duration) {
self.assert_oracle();

self.burn_period = period;
}
}
1 change: 1 addition & 0 deletions contract/src/record/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub(crate) mod api;
mod tests;
35 changes: 35 additions & 0 deletions contract/src/record/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![cfg(test)]

use model::api::{ClaimApi, RecordApi};
use near_sdk::{json_types::U128, test_utils::accounts};

use crate::common::tests::Context;

#[test]
fn record_by_oracle() {
let (mut context, mut contract, accounts) = Context::init_with_oracle();

let alice_balance_1 = 1_000_000;

context.switch_account(&accounts.oracle);
contract.record_batch_for_hold(vec![(accounts.alice.clone(), U128(alice_balance_1))]);

let alice_actual_balance = contract.get_claimable_balance_for_account(accounts.alice.clone());
assert_eq!(alice_balance_1, alice_actual_balance.0);

context.set_block_timestamp_in_seconds(1_000);

let alice_balance_2 = 500_000;
let bob_balance = 200_000;

contract.record_batch_for_hold(vec![
(accounts.alice.clone(), U128(alice_balance_2)),
(accounts.bob.clone(), U128(bob_balance)),
]);

let alice_actual_balance = contract.get_claimable_balance_for_account(accounts.alice.clone());
assert_eq!(alice_balance_1 + alice_balance_2, alice_actual_balance.0);

let bob_actual_balance = contract.get_claimable_balance_for_account(accounts.bob.clone());
assert_eq!(bob_balance, bob_actual_balance.0);
}
2 changes: 0 additions & 2 deletions model/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::{ClaimAvailabilityView, Duration};
#[make_integration_version]
pub trait InitApi {
fn init(token_account_id: AccountId) -> Self;
fn set_claim_period(&mut self, period: Duration);
fn set_burn_period(&mut self, period: Duration);
}

#[make_integration_version]
Expand Down

0 comments on commit 962f248

Please sign in to comment.