Skip to content

Commit

Permalink
Merge branch 'feature/1018-tests' of github.com:NEAR-DevHub/neardevhu…
Browse files Browse the repository at this point in the history
…b-api-rs into feature/1019-tests
  • Loading branch information
Tguntenaar committed Dec 29, 2024
2 parents 47c6d4a + 0fe9f1b commit bdd1a79
Showing 1 changed file with 88 additions and 45 deletions.
133 changes: 88 additions & 45 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use futures::future::join_all;
use near_api::prelude::Contract;

use crate::rpc_service::RpcService;
use crate::{db::db_types::ProposalWithLatestSnapshotView, types::PaginatedResponse, Env};
use crate::{separate_number_and_text, timestamp_to_date_string};
use near_sdk::AccountId;
use serde_json::{json, Value};

/**
* Test Nearblocks mocked transactions
* Test that the proposal ids are continuous, and the name and status matches
*/

/**
Expand Down Expand Up @@ -46,7 +47,7 @@ async fn test_new_proposals_appear_in_cache() {
}

#[rocket::async_test]
async fn test_proposal_ids_are_continuous_and_name_and_status_matches() {
async fn test_proposal_ids_continuous_name_status_matches() {
use rocket::local::asynchronous::Client;

let client = Client::tracked(super::rocket())
Expand All @@ -71,7 +72,7 @@ async fn test_proposal_ids_are_continuous_and_name_and_status_matches() {
result
};

assert_eq!(result.records.len(), 50);
assert_eq!(result.records.len(), 50, "Result records should be 50");

eprintln!(
"Results {:?}",
Expand Down Expand Up @@ -136,66 +137,108 @@ async fn test_proposal_ids_are_continuous_and_name_and_status_matches() {

#[rocket::async_test]
async fn test_all_proposals_are_indexed() {
use near_sdk::AccountId;
use reqwest;
use serde_json::Value;
use std::collections::HashMap;

let contract_strings = vec![
"devhub.near",
"infrastructure-committee.near",
"events-committee.near",
];

let urls = [
let mut map = HashMap::new();
map.insert(
"devhub.near",
"https://devhub-cache-api-rs.fly.dev/proposals",
);
map.insert(
"infrastructure-committee.near",
"https://infra-cache-api-rs.fly.dev/proposals",
);
map.insert(
"events-committee.near",
"https://events-cache-api-rs.fly.dev/proposals",
];
);

// Create a reqwest client
let client = reqwest::Client::new();
for contract_string in contract_strings {
let account_id = contract_string.parse::<AccountId>().unwrap();
let url = *map.get(contract_string).expect("Contract string not found");

// Make the HTTP request to the deployed API
let response = client
.get(urls[2])
.send()
.await
.expect("Failed to get response");
// Create a reqwest client
let client = reqwest::Client::new();

// Ensure the request was successful
assert!(response.status().is_success());
// Make the HTTP request to the deployed API
let response = client
.get(url)
.send()
.await
.expect("Failed to get response");

// Parse the response body as JSON
let result: Value = response
.json()
.await
.expect("Failed to parse response as JSON");
// Ensure the request was successful
assert!(response.status().is_success());

println!("Result: {:?}", result);
// Parse the response body as JSON
let result: Value = response
.json()
.await
.expect("Failed to parse response as JSON");

// Extract total count and records
let total = result["total_records"]
.as_i64()
.expect("Failed to get total count");
println!("Result for {}: {:?}", contract_string, result);

let records = result["records"]
.as_array()
.expect("Failed to get records array");
// Extract total count and records
let total = result["total_records"]
.as_i64()
.expect("Failed to get total count");

// Ensure we have records
assert!(!records.is_empty(), "No records found");
let records = result["records"]
.as_array()
.expect("Failed to get records array");

// Get the last proposal ID
let last_proposal = records.first().expect("Failed to get last record");
// Ensure we have records
assert!(!records.is_empty(), "No records found");

let last_id = last_proposal["proposal_id"]
.as_i64()
.expect("Failed to get proposal_id");
// Get the last proposal ID
let last_proposal = records.first().expect("Failed to get last record");

// Compare the last ID with the total count
// They should be equal if all proposals are properly indexed
assert_eq!(
last_id,
total - 1,
"Last proposal ID ({}) doesn't match total count ({})",
last_id,
total - 1
);
let last_id_api = last_proposal["proposal_id"]
.as_i64()
.expect("Failed to get proposal_id");

eprintln!("Total count: {}, Last ID: {}", total, last_id);
// rpc service
let rpc_service = RpcService::new(&account_id);

// Get all proposal ids
let proposal_ids = rpc_service.get_all_proposal_ids().await;
let proposal_ids = proposal_ids.unwrap();
let total_ids_rpc = proposal_ids.len() as i64;
let last_id_rpc = proposal_ids.last().copied().unwrap();
assert_eq!(
last_id_rpc, last_id_api as i32,
"Last proposal ID from the RPC {:?} doesn't match the API {:?} on contract {:?}",
last_id_rpc, last_id_api, contract_string
);

// Compare the last ID with the total count
// They should be equal if all proposals are properly indexed
assert_eq!(
last_id_api,
total - 1,
"Last proposal ID from the API ({}) doesn't match total count ({}) on contract {:?}",
last_id_api,
total - 1,
contract_string
);

assert_eq!(
total_ids_rpc, total,
"Total count from the RPC {:?} doesn't match the API {:?} on contract {:?}",
total_ids_rpc, total, contract_string
);

eprintln!("Total count: {}, Last ID: {}", total, last_id_api);
}
}

#[test]
Expand Down

0 comments on commit bdd1a79

Please sign in to comment.