Skip to content

Commit

Permalink
test: test pausable functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
gluax authored and Thomasvdam committed Jan 29, 2025
1 parent a1d3836 commit 3d1258d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
17 changes: 17 additions & 0 deletions contract/src/msgs/owner/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ impl TestInfo {

self.execute(sender, &msg)
}

#[track_caller]
pub fn pause(&mut self, sender: &TestExecutor) -> Result<(), ContractError> {
let msg = execute::pause::Execute {}.into();
self.execute(sender, &msg)
}

#[track_caller]
pub fn unpause(&mut self, sender: &TestExecutor) -> Result<(), ContractError> {
let msg = execute::unpause::Execute {}.into();
self.execute(sender, &msg)
}

#[track_caller]
pub fn is_paused(&self) -> bool {
self.query(query::QueryMsg::IsPaused {}).unwrap()
}
}
56 changes: 55 additions & 1 deletion contract/src/msgs/owner/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use seda_common::msgs::staking::StakingConfig;
use seda_common::msgs::{data_requests::DataRequestStatus, staking::StakingConfig};

use crate::{error::ContractError, TestInfo};

Expand Down Expand Up @@ -128,3 +128,57 @@ pub fn allowlist_works() {
// now alice can register a data request executor
test_info.stake(&mut alice, None, 100).unwrap();
}

#[test]
pub fn pause_works() {
let mut test_info = TestInfo::init();

// check that the contract is not paused
assert!(!test_info.is_paused());

// pause the contract
test_info.pause(&test_info.creator()).unwrap();
assert!(test_info.is_paused());

// double pause leaves errors
let err = test_info.pause(&test_info.creator()).unwrap_err();
assert!(err.to_string().contains("Contract paused"));

// check that sudo messages are paused
assert!(test_info
.remove_data_request("Doesn't matter".to_string(), vec![])
.is_err());

// unpause the contract
test_info.unpause(&test_info.creator()).unwrap();
assert!(!test_info.is_paused());

// double unpause leaves errors
let err = test_info.unpause(&test_info.creator()).unwrap_err();
assert!(err.to_string().contains("Contract not paused"));
}

#[test]
pub fn paused_contract_returns_empty_dr_query_by_status() {
let mut test_info = TestInfo::init();
let mut anyone = test_info.new_executor("anyone", Some(22));
anyone.stake(&mut test_info, 1).unwrap();

// post a data request
let dr = crate::msgs::data_requests::test::test_helpers::calculate_dr_id_and_args(1, 1);
let _dr_id = test_info
.post_data_request(&mut anyone, dr, vec![], vec![], 2, None)
.unwrap();

let drs = test_info.get_data_requests_by_status(DataRequestStatus::Committing, 0, 10);
assert_eq!(1, drs.len());

test_info.pause(&test_info.creator()).unwrap();
assert!(test_info.is_paused());

let drs = test_info.get_data_requests_by_status(DataRequestStatus::Committing, 0, 10);
assert_eq!(0, drs.len());

test_info.unpause(&test_info.creator()).unwrap();
assert!(!test_info.is_paused());
}

0 comments on commit 3d1258d

Please sign in to comment.