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

Proposal tests #6

Closed
Closed
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
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions .devcontainer/install-dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
sudo apt update
sudo apt install -y postgresql
sudo apt install libudev-dev

cargo install sqlx-cli

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>
```
Expand Down
72 changes: 72 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -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
*/
Expand All @@ -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::<PaginatedResponse<ProposalWithLatestSnapshotView>>()
.await
.unwrap();
assert_eq!(result.records.len(), 50);

let env: Env = envy::from_env::<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"]
);
}
}
Loading