-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from matiasbenary/updateRs
Update contract rs
- Loading branch information
Showing
2 changed files
with
97 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,122 @@ | ||
#[tokio::test] | ||
async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>> { | ||
let sandbox = near_workspaces::sandbox().await?; | ||
use serde_json::json; | ||
|
||
async fn prepare_test_environment() -> Result<(near_workspaces::Account, near_workspaces::Contract), Box<dyn std::error::Error>> { | ||
let worker = near_workspaces::sandbox().await?; | ||
let contract_wasm = near_workspaces::compile_project("./").await?; | ||
let contract = worker.dev_deploy(&contract_wasm).await?; | ||
|
||
// create accounts | ||
let account = worker.dev_create_account().await?; | ||
|
||
Ok((account, contract)) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_can_be_incremented() -> Result<(), Box<dyn std::error::Error>> { | ||
let (account, contract) = prepare_test_environment().await?; | ||
|
||
let contract = sandbox.dev_deploy(&contract_wasm).await?; | ||
let counter_in_zero = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
let user_account = sandbox.dev_create_account().await?; | ||
assert_eq!(counter_in_zero.json::<u8>()?, 0); | ||
|
||
let increment = user_account | ||
let _ = account | ||
.call(contract.id(), "increment") | ||
.args_json({}) | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
assert!(increment.is_success()); | ||
let _ = account | ||
.call(contract.id(), "decrement") | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
let _increment = user_account | ||
let _ = account | ||
.call(contract.id(), "increment") | ||
.args_json({}) | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
let counter_in_one = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
assert_eq!(counter_in_one.json::<u8>()?, 1); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_can_be_decremented() -> Result<(), Box<dyn std::error::Error>> { | ||
|
||
let (account, contract) = prepare_test_environment().await?; | ||
let counter_in_zero = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
assert_eq!(counter_in_zero.json::<u8>()?,0); | ||
|
||
let _ = account | ||
.call(contract.id(), "decrement") | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
let decrement = user_account | ||
let _ = account | ||
.call(contract.id(), "decrement") | ||
.args_json({}) | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
assert!(decrement.is_success()); | ||
|
||
let get_num_result = contract.view("get_num").args_json({}).await?; | ||
let counter_in_minus_one = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
assert_eq!(get_num_result.json::<i8>()?, 1); | ||
assert_eq!(counter_in_minus_one.json::<i8>()?,-2); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_can_be_reset() -> Result<(), Box<dyn std::error::Error>> { | ||
|
||
let reset_result = user_account | ||
let (account, contract) = prepare_test_environment().await?; | ||
let counter_in_zero = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
assert_eq!(counter_in_zero.json::<u8>()?,0); | ||
|
||
let outcome_increment = account | ||
.call(contract.id(), "increment") | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
assert!(outcome_increment.is_success()); | ||
|
||
let outcome_reset = account | ||
.call(contract.id(), "reset") | ||
.args_json({}) | ||
.args_json(json!({})) | ||
.transact() | ||
.await?; | ||
|
||
assert!(reset_result.is_success()); | ||
assert!(outcome_reset.is_success()); | ||
|
||
let get_reset_num = contract.view("get_num").args_json({}).await?; | ||
let counter_reset = contract | ||
.view("get_num") | ||
.args_json(json!({})) | ||
.await?; | ||
|
||
assert_eq!(get_reset_num.json::<i8>()?, 0); | ||
assert_eq!(counter_reset.json::<i8>()?,0); | ||
|
||
Ok(()) | ||
} |