Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions simple-counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,17 @@ anyhow = "1.0"
thiserror = "1.0"
near-sdk = "4.0.0"

[dev-dependencies]
anyhow = "1.0"
# arbitrary_precision enabled for u128 types that workspaces requires for Balance types
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
tokio = { version = "1.18.1", features = ["full"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3.11", features = ["env-filter"] }
workspaces = "0.4.0"
pkg-config = "0.3.1"
serde = { version = "1.0", features = ["derive"] }
near-sdk = "4.0.0"

[lib]
crate-type = ["cdylib", "rlib"]
120 changes: 120 additions & 0 deletions simple-counter/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use near_sdk::serde::{Deserialize, Serialize};
use serde_json::json;
use workspaces::prelude::*;
use workspaces::{network::Sandbox, Account, Contract, Worker};

const WASM_FILEPATH: &str = "../out/simple_counter.wasm";

#[derive(Serialize, Deserialize)]
pub struct Transaction {
value: u64,
}

async fn init() -> anyhow::Result<(Account, Account, Transaction, Contract, Worker<Sandbox>)> {
let sandbox = workspaces::sandbox().await?;
let wasm = std::fs::read(WASM_FILEPATH)?;
let contract = sandbox.dev_deploy(&wasm).await?;

let owner = sandbox.root_account().unwrap();
let alice = owner
.create_subaccount(&sandbox, "alice")
.transact()
.await?
.into_result()?;

let bob = owner
.create_subaccount(&sandbox, "bob")
.transact()
.await?
.into_result()?;

let transaction = Transaction { value: 10 };

contract
.call(&sandbox, "new")
.args_json(json!({"val": 100, "auths": vec![alice.id()]}))?
.transact()
.await?;

Ok((alice, bob, transaction, contract, sandbox))
}

#[tokio::test]
async fn test_increment() -> anyhow::Result<()> {
let (alice, _, transaction, contract, sandbox) = init().await?;

let start_counter: u64 = alice
.call(&sandbox, contract.id(), "get_num")
.args_json(json!({}))?
.transact()
.await?
.json()?;

alice
.call(&sandbox, contract.id(), "increment")
.args_json(json!({ "trans": transaction }))?
.transact()
.await?;

let end_counter: u64 = alice
.call(&sandbox, contract.id(), "get_num")
.args_json(json!({}))?
.transact()
.await?
.json()?;

assert_eq!(end_counter, start_counter + transaction.value);
println!("Increment ✅");
Ok(())
}

#[tokio::test]
async fn test_decrement() -> anyhow::Result<()> {
let (alice, _, transaction, contract, sandbox) = init().await?;

let start_counter: u64 = alice
.call(&sandbox, contract.id(), "get_num")
.args_json(json!({}))?
.transact()
.await?
.json()?;

alice
.call(&sandbox, contract.id(), "decrement")
.args_json(json!({ "trans": transaction }))?
.transact()
.await?;

let end_counter: u64 = alice
.call(&sandbox, contract.id(), "get_num")
.args_json(json!({}))?
.transact()
.await?
.json()?;

assert_eq!(end_counter, start_counter - transaction.value);
println!("Decrement ✅");
Ok(())
}

#[tokio::test]
async fn test_reset() -> anyhow::Result<()> {
let (alice, _, _, contract, sandbox) = init().await?;

alice
.call(&sandbox, contract.id(), "reset")
.args_json(json!({}))?
.transact()
.await?;

let reset_counter: u64 = alice
.call(&sandbox, contract.id(), "get_num")
.args_json(json!({}))?
.transact()
.await?
.json()?;

assert_eq!(reset_counter, 0);
println!("Reset ✅");
Ok(())
}