Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating workspaces test suit #46

Merged
merged 20 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c616da1
add, remove and get featured communities
jaswinder6991 Jul 11, 2023
751c7be
fix: implemented the migration properly, and use `Vec` instead of `Ve…
frol Jul 18, 2023
df48d72
change add_featured_communities to accept a Vec of handles, removed d…
jaswinder6991 Jul 18, 2023
afad6b1
changed function name from add_featured_communities to set_featured_c…
jaswinder6991 Jul 19, 2023
7a3074f
Merge branch 'main' into featured-communities
frol Jul 19, 2023
fe0606c
workspaces setup, deploying contract
jaswinder6991 Jul 24, 2023
86d667a
use two different version wasms to test self_upgrade
jaswinder6991 Jul 24, 2023
2d2164d
Merged with main
jaswinder6991 Jul 28, 2023
1a13e2d
Fixed corrupted Cargo Lock
jaswinder6991 Aug 1, 2023
9e1272b
wip: enabled cargo test, added pulling contract from mainnet.
jaswinder6991 Aug 4, 2023
af7fc9f
added helper function for compile_project, finished test case.
jaswinder6991 Aug 6, 2023
9091e50
Merge branch 'main' of https://github.com/near/neardevhub-contract in…
jaswinder6991 Aug 6, 2023
4d933d1
Merge branch 'main' of https://github.com/near/neardevhub-contract in…
jaswinder6991 Aug 28, 2023
2dc3041
wip: adding posts and checking self_upgrade
jaswinder6991 Aug 30, 2023
87d2903
add more kinds of posts and get them after upgrade to check migration.
jaswinder6991 Sep 1, 2023
c349961
added readme
jaswinder6991 Sep 1, 2023
b4b4388
feat: use `insta` crate for snapshot testing
frol Sep 5, 2023
b166bf5
Merge branch 'main' into setup-workspaces
frol Sep 5, 2023
87df082
reverted unnecessary change in lib.rs
frol Sep 5, 2023
35b44a9
use near-workspaces crate name instead of workspaces to avoid confusion
frol Sep 5, 2023
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
3,219 changes: 2,929 additions & 290 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ near-sdk = "3.1.0"
near-contract-standards = "3.2.0"

[dev-dependencies]
insta = { version = "1.31.0", features = ["json", "redactions"] }
regex = "1"
near-workspaces = { version = "0.7.0", features = ["unstable"] }
tokio = { version = "1.10.0", features = ["full"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
near-units = "0.2.0"
anyhow = "1.0"

[profile.release]
codegen-units = 1
Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "1.69.0"
4 changes: 3 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ contract=i.zxcvn.testnet

# near call $contract unsafe_self_upgrade --accountId $contract --args $(base64 < res/devgovgigs.wasm ) --base64 --gas 300000000000000

near call $contract unsafe_migrate --accountId $contract --gas 300000000000000
#near call contract.devhubopen.testnet unsafe_self_upgrade --accountId contract.devhubopen.testnet --args $(base64 < res/devgovgigs.wasm ) --base64 --gas 300000000000000

# near call $contract unsafe_migrate --accountId $contract --gas 300000000000000
18 changes: 18 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Integration Test using near-workspaces-rs

This codebase is designed to deploy and test the Devhub contract on a Sandbox node.

## Dependencies

- Rust: For writing the test suite.
- `near-workspaces`: A custom library for handling contract calls.
- `near_units`: For handling NEAR unit conversions.
- `serde_json`: For JSON serialization and deserialization.

### How to Run

To run the test, use the following command:

```bash
cargo test
```
223 changes: 223 additions & 0 deletions tests/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
use near_units::parse_near;
use near_workspaces::AccountId;
use serde_json::json;

const DEVHUB_CONTRACT: &str = "devgovgigs.near";
const NEAR_SOCIAL: &str = "social.near";

#[tokio::test]
async fn test_deploy_contract_self_upgrade() -> anyhow::Result<()> {
// Test Flow:
// 1. Deploy devhub and near social contract on sandbox
// 2. Add all kinds of posts and add a community.
// 3. Upgrade the contract.
// 4. Get all the posts and community and check if migration was succesfull.

// Initialize the devhub and near social contract on chain,
// contract is devhub contract instance.
let contract = init_contracts().await?;

let deposit_amount = near_units::parse_near!("0.1");

// Add Posts
let add_idea_post = contract
.call("add_post")
.args_json(json!({
"parent_id": null,
"labels": [],
"body": {
"name": "This is a test idea.",
"description": "This is a test description.",
"post_type": "Idea",
"idea_version": "V1"
}
}))
.deposit(deposit_amount)
.transact()
.await?;
assert!(add_idea_post.is_success());

let add_submission_post = contract
.call("add_post")
.args_json(json!({
"parent_id": null,
"labels": [],
"body": {
"name": "Solution Test",
"description": "###### Requested amount: 100 NEAR\n###### Requested sponsor: @neardevgov.near\nThis is a test submission. ",
"post_type": "Submission",
"submission_version": "V1"
}
}))
.deposit(deposit_amount)
.max_gas()
.transact()
.await?;
assert!(add_submission_post.is_success());

let add_comment_post = contract
.call("add_post")
.args_json(json!({
"parent_id": 0,
"labels": [],
"body": {
"description": "This is test Comment.",
"comment_version": "V2",
"post_type": "Comment"
}
}))
.deposit(deposit_amount)
.max_gas()
.transact()
.await?;
assert!(add_comment_post.is_success());

let add_attestation_post = contract
.call("add_post")
.args_json(json!({
"parent_id": 1,
"labels": [],
"body": {
"name": "Attestation",
"description": "Description",
"attestation_version": "V1",
"post_type": "Attestation"
}
}))
.deposit(deposit_amount)
.max_gas()
.transact()
.await?;
assert!(add_attestation_post.is_success());

let add_sponsorship_post = contract
.call("add_post")
.args_json(json!({
"parent_id": 1,
"labels": [],
"body": {
"name": "Contributor fellowship",
"description": "Funding approved",
"amount": "1000",
"sponsorship_token": "Near",
"supervisor": "john.near",
"sponsorship_version": "V1",
"post_type": "Sponsorship"
}
}))
.deposit(deposit_amount)
.max_gas()
.transact()
.await?;
assert!(add_sponsorship_post.is_success());

// Add a community
let create_community = contract
.call("create_community")
.args_json(json!({
"inputs": {
"handle": "gotham",
"name": "Gotham",
"tag": "some",
"description": "This is a test community.",
"bio_markdown": "This is a sample text about your community.\nYou can change it on the community configuration page.",
"logo_url": "https://ipfs.near.social/ipfs/bafkreibysr2mkwhb4j36h2t7mqwhynqdy4vzjfygfkfg65kuspd2bawauu",
"banner_url": "https://ipfs.near.social/ipfs/bafkreic4xgorjt6ha5z4s5e3hscjqrowe5ahd7hlfc5p4hb6kdfp6prgy4"
}
}))
.max_gas()
.transact()
.await?;
assert!(create_community.is_success());

// Call self upgrade with current branch code
// compile the current code
let wasm = near_workspaces::compile_project("./").await?;

let mut contract_upgrade_result =
contract.call("unsafe_self_upgrade").args(wasm).max_gas().transact().await?;

while contract_upgrade_result.json::<String>()? == "needs-migration" {
contract_upgrade_result =
contract.call("unsafe_migrate").args_json(json!({})).max_gas().transact().await?;
}

let get_idea_post: serde_json::Value = contract
.call("get_post")
.args_json(json!({
"post_id" : 0
}))
.view()
.await?
.json()?;
insta::assert_json_snapshot!(get_idea_post, {".snapshot.timestamp" => "[timestamp]"});

let get_comment_posts: serde_json::Value = contract
.call("get_posts")
.args_json(json!({
"parent_id" : 0
}))
.view()
.await?
.json()?;
insta::assert_json_snapshot!(get_comment_posts, {"[].snapshot.timestamp" => "[timestamp]"});

let get_submission_post: serde_json::Value = contract
.call("get_post")
.args_json(json!({
"post_id" : 1
}))
.view()
.await?
.json()?;
insta::assert_json_snapshot!(get_submission_post, {".snapshot.timestamp" => "[timestamp]"});

let get_attestation_sponsorship_posts: serde_json::Value = contract
.call("get_posts")
.args_json(json!({
"parent_id" : 1
}))
.view()
.await?
.json()?;
insta::assert_json_snapshot!(get_attestation_sponsorship_posts, {"[].snapshot.timestamp" => "[timestamp]"});

let get_community: serde_json::Value = contract
.call("get_community")
.args_json(json!({
"handle" : "gotham"
}))
.view()
.await?
.json()?;
insta::assert_json_snapshot!(get_community);

Ok(())
}

async fn init_contracts() -> anyhow::Result<near_workspaces::Contract> {
let worker = near_workspaces::sandbox().await?;
let mainnet = near_workspaces::mainnet_archival().await?;

// NEAR social deployment
let near_social_id: AccountId = NEAR_SOCIAL.parse()?;
let near_social = worker
.import_contract(&near_social_id, &mainnet)
.initial_balance(parse_near!("10000 N"))
.transact()
.await?;
near_social.call("new").transact().await?.into_result()?;

// Devhub contract deployment
let contract_id: AccountId = DEVHUB_CONTRACT.parse()?;
let contract = worker
.import_contract(&contract_id, &mainnet)
.initial_balance(parse_near!("1000 N"))
.transact()
.await?;
let outcome = contract.call("new").args_json(json!({})).transact().await?;
assert!(outcome.is_success());
assert!(format!("{:?}", outcome).contains("Migrated to version:"));

Ok(contract)
}
23 changes: 23 additions & 0 deletions tests/snapshots/migration__deploy_contract_self_upgrade-2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: tests/migration.rs
expression: get_comment_posts
---
[
{
"post_version": "V0",
"id": {
"$serde_json::private::Number": "2"
},
"author_id": "devgovgigs.near",
"likes": [],
"snapshot": {
"editor_id": "devgovgigs.near",
"timestamp": "[timestamp]",
"labels": [],
"post_type": "Comment",
"comment_version": "V2",
"description": "This is test Comment."
},
"snapshot_history": []
}
]
22 changes: 22 additions & 0 deletions tests/snapshots/migration__deploy_contract_self_upgrade-3.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: tests/migration.rs
expression: get_submission_post
---
{
"post_version": "V0",
"id": {
"$serde_json::private::Number": "1"
},
"author_id": "devgovgigs.near",
"likes": [],
"snapshot": {
"editor_id": "devgovgigs.near",
"timestamp": "[timestamp]",
"labels": [],
"post_type": "Submission",
"submission_version": "V1",
"name": "Solution Test",
"description": "###### Requested amount: 100 NEAR\n###### Requested sponsor: @neardevgov.near\nThis is a test submission. "
},
"snapshot_history": []
}
45 changes: 45 additions & 0 deletions tests/snapshots/migration__deploy_contract_self_upgrade-4.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: tests/migration.rs
expression: get_attestation_sponsorship_posts
---
[
{
"post_version": "V0",
"id": {
"$serde_json::private::Number": "3"
},
"author_id": "devgovgigs.near",
"likes": [],
"snapshot": {
"editor_id": "devgovgigs.near",
"timestamp": "[timestamp]",
"labels": [],
"post_type": "Attestation",
"attestation_version": "V1",
"name": "Attestation",
"description": "Description"
},
"snapshot_history": []
},
{
"post_version": "V0",
"id": {
"$serde_json::private::Number": "4"
},
"author_id": "devgovgigs.near",
"likes": [],
"snapshot": {
"editor_id": "devgovgigs.near",
"timestamp": "[timestamp]",
"labels": [],
"post_type": "Sponsorship",
"sponsorship_version": "V1",
"name": "Contributor fellowship",
"description": "Funding approved",
"sponsorship_token": "Near",
"amount": "1000",
"supervisor": "john.near"
},
"snapshot_history": []
}
]
30 changes: 30 additions & 0 deletions tests/snapshots/migration__deploy_contract_self_upgrade-5.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: tests/migration.rs
expression: get_community
---
{
"admins": [
"devgovgigs.near"
],
"handle": "gotham",
"name": "Gotham",
"tag": "some",
"description": "This is a test community.",
"logo_url": "https://ipfs.near.social/ipfs/bafkreibysr2mkwhb4j36h2t7mqwhynqdy4vzjfygfkfg65kuspd2bawauu",
"banner_url": "https://ipfs.near.social/ipfs/bafkreic4xgorjt6ha5z4s5e3hscjqrowe5ahd7hlfc5p4hb6kdfp6prgy4",
"bio_markdown": "This is a sample text about your community.\nYou can change it on the community configuration page.",
"github_handle": null,
"telegram_handle": [],
"twitter_handle": null,
"website_url": null,
"github": null,
"board": null,
"wiki1": null,
"wiki2": null,
"features": {
"telegram": true,
"github": true,
"board": true,
"wiki": true
}
}
Loading
Loading