diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 5d367aa..12251df 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -6,7 +6,7 @@ }, "customizations": { "vscode": { - "extensions": ["rust-lang.rust-analyzer", "github.vscode-github-actions"] + "extensions": ["rust-lang.rust-analyzer", "github.vscode-github-actions","ms-python.python"] } }, "postCreateCommand": "./.devcontainer/install-dependencies.sh" diff --git a/.devcontainer/install-dependencies.sh b/.devcontainer/install-dependencies.sh index b905091..b8dd60d 100755 --- a/.devcontainer/install-dependencies.sh +++ b/.devcontainer/install-dependencies.sh @@ -1,6 +1,7 @@ #!/bin/bash sudo apt update sudo apt install -y postgresql +sudo apt install libudev-dev cargo install sqlx-cli diff --git a/readme.md b/readme.md index 424c16f..6e9afcd 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,8 @@ cargo watch -q -c -w src/ -x 'run ' ### Create and run migrations +*Github codespace*: If the database server is not running, you can start it by typing `sudo service postgresql start`. + ```bash sqlx migrate add ``` diff --git a/src/tests.rs b/src/tests.rs index 2a313ce..a35b103 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,3 +1,11 @@ +use near_api::prelude::{Contract, Reference}; + +use crate::{ + db::db_types::ProposalWithLatestSnapshotView, rpc_service, types::PaginatedResponse, Env, +}; +use near_sdk::AccountId; +use serde_json::{json, Value}; + /** * Test Nearblocks mocked transactions */ @@ -22,3 +30,67 @@ fn test_index() { let response = client.get("/").dispatch(); assert_eq!(response.into_string().unwrap(), "Welcome from fly.io!!!!!"); } + +#[rocket::async_test] +async fn test_proposal_ids_are_continuous_and_name_and_status_matches() { + use rocket::local::asynchronous::Client; + + let client = Client::tracked(super::rocket()) + .await + .expect("valid `Rocket`"); + + let response = client + .get("/proposals?order=id_asc&limit=50&offset=0}`") + .dispatch(); + let result = response + .await + .into_json::>() + .await + .unwrap(); + assert_eq!(result.records.len(), 50); + + let env: Env = envy::from_env::().expect("Failed to load environment variables"); + let contract_account_id: AccountId = env.contract.parse().unwrap(); + let contract = Contract(contract_account_id); + + for ndx in 0..result.records.len() { + let proposal_id: i32 = ndx as i32; + assert_eq!(result.records[ndx].proposal_id, proposal_id); + + let call = contract + .call_function("get_proposal", json!({"proposal_id": proposal_id})) + .unwrap(); + let proposal: Value = call + .read_only() + .fetch_from_mainnet() + .await + .unwrap() + .data; + eprintln!( + "proposal {:?}, {:?}, {:?}, {:?}", + proposal_id, + result.records[ndx].block_height.unwrap(), + proposal["snapshot"]["name"], + proposal["snapshot"]["timeline"]["status"] + ); + assert_eq!( + proposal["snapshot"]["name"].as_str().unwrap(), + result.records[ndx].clone().name.unwrap() + ); + + let timeline: Value = serde_json::from_str( + result.records[ndx] + .clone() + .timeline + .unwrap() + .as_str() + .unwrap(), + ) + .unwrap(); + eprint!("timeline {:?}", timeline); + assert_eq!( + proposal["snapshot"]["timeline"]["status"], + timeline["status"] + ); + } +}