Skip to content

Commit

Permalink
Merge pull request #4 from matiasbenary/updateRs
Browse files Browse the repository at this point in the history
Update contract rs
  • Loading branch information
gagdiez authored Mar 19, 2024
2 parents 9a85271 + 27b236a commit 09e00af
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 24 deletions.
8 changes: 4 additions & 4 deletions contract-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ pub struct Counter {
val: i8,
}

// Implement the contract structure
#[near_bindgen]
impl Counter {

// Public read-only method: Returns the counter value.
pub fn get_num(&self) -> i8 {
return self.val;
Expand All @@ -37,12 +39,10 @@ impl Counter {
}

/*
* the rest of this file sets up unit tests
* The rest of this file holds the inline tests for the code above
* to run these, the command will be: `cargo test`
* Note: 'rust-counter-tutorial' comes from cargo.toml's 'name' key
* Learn more about Rust tests: https://doc.rust-lang.org/book/ch11-01-writing-tests.html
*/

// use the attribute below for unit tests
#[cfg(test)]
mod tests {
use super::*;
Expand Down
113 changes: 93 additions & 20 deletions contract-rs/tests/sandbox.rs
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(())
}

0 comments on commit 09e00af

Please sign in to comment.