From ea0f709292c13390a7275c70a0609b7b6498505c Mon Sep 17 00:00:00 2001 From: Cory Dickson Date: Tue, 6 Feb 2024 17:17:52 -0500 Subject: [PATCH 1/2] Add CI for main branch --- .github/workflows/main.yml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..ad44ef4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,17 @@ +name: Main CI +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] +jobs: + unit-testing: + name: Unit testing + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + - uses: Swatinem/rust-cache@v2 + - run: rustup target add wasm32-unknown-unknown + - name: Build and test contract + run: ./build.sh | cargo test From ad67ef46892802b1ac2251fcf5d63e171443e0b7 Mon Sep 17 00:00:00 2001 From: Cory Dickson Date: Tue, 6 Feb 2024 17:21:31 -0500 Subject: [PATCH 2/2] Integration testing sandbox --- sandbox-rs/Cargo.toml | 14 +++++++++ sandbox-rs/src/tests.rs | 68 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 sandbox-rs/Cargo.toml create mode 100644 sandbox-rs/src/tests.rs diff --git a/sandbox-rs/Cargo.toml b/sandbox-rs/Cargo.toml new file mode 100644 index 0000000..65a38ea --- /dev/null +++ b/sandbox-rs/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "sandbox" +version = "1.0.0" +publish = false +edition = "2021" + +[dev-dependencies] +tokio = { version = "1.18.1", features = ["full"] } +near-workspaces = "0.9.0" +serde_json = { version = "1.0", features = ["arbitrary_precision"] } + +[[example]] +name = "sandbox" +path = "src/tests.rs" \ No newline at end of file diff --git a/sandbox-rs/src/tests.rs b/sandbox-rs/src/tests.rs new file mode 100644 index 0000000..757fd11 --- /dev/null +++ b/sandbox-rs/src/tests.rs @@ -0,0 +1,68 @@ +use near_workspaces::{types::NearToken, Account, Contract}; +use serde_json::json; +use std::{env, fs}; + +#[tokio::main] +async fn main() -> Result<(), Box> { + let wasm_arg: &str = &(env::args().nth(1).unwrap()); + let wasm_filepath = fs::canonicalize(env::current_dir()?.join(wasm_arg))?; + + let worker = near_workspaces::sandbox().await?; + let wasm = std::fs::read(wasm_filepath)?; + let contract = worker.dev_deploy(&wasm).await?; + + // create accounts + let account = worker.dev_create_account().await?; + let alice = account + .create_subaccount("alice") + .initial_balance(NearToken::from_near(30)) + .transact() + .await? + .into_result()?; + + // begin tests + // test_default_message(&alice, &contract).await?; + // test_changes_message(&alice, &contract).await?; + Ok(()) +} + + +/* +async fn test_default_message( + user: &Account, + contract: &Contract, +) -> Result<(), Box> { + let greeting: String = user + .call(contract.id(), "get_greeting") + .args_json(json!({})) + .transact() + .await? + .json()?; + + assert_eq!(greeting, "Hello".to_string()); + println!(" Passed ✅ gets default greeting"); + Ok(()) +} + +async fn test_changes_message( + user: &Account, + contract: &Contract, +) -> Result<(), Box> { + user.call(contract.id(), "set_greeting") + .args_json(json!({"greeting": "Howdy"})) + .transact() + .await? + .into_result()?; + + let greeting: String = user + .call(contract.id(), "get_greeting") + .args_json(json!({})) + .transact() + .await? + .json()?; + + assert_eq!(greeting, "Howdy".to_string()); + println!(" Passed ✅ changes greeting"); + Ok(()) +} +*/