Skip to content

Commit

Permalink
Add a unit test to src/tests/mod.rs that will test sending 3 requests…
Browse files Browse the repository at this point in the history
… with new, random data each time to the same address on the /set_data api route. The response should contain all data entries that were sent and fail otherwise. Only work on src/tests/mod.rs, no other files
  • Loading branch information
Sophie-Ag00001 committed Jan 26, 2025
1 parent f232d66 commit 7eba3c0
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
pub mod constants;
pub mod interfaces;

use crate::api::request::SetRequestData;
use crate::api::routes;
use crate::db::handler::KvStoreConnection;
use crate::tests::constants::{TEST_VALID_ADDRESS, TEST_VALID_PUB_KEY, TEST_VALID_SIG};
use crate::tests::interfaces::DbStub;
use futures::lock::Mutex;
use serde_json::json;
use std::sync::Arc;
use uuid::Uuid;
use valence_core::api::utils::handle_rejection;
use warp::Filter;

Expand Down Expand Up @@ -129,3 +132,50 @@ async fn test_set_data() {
"{\"status\":\"Success\",\"reason\":\"Data set successfully\",\"route\":\"set_data\",\"content\":\"0x123\"}"
);
}

#[tokio::test(flavor = "current_thread")]
async fn test_set_data_multiple_requests() {
let db = Arc::new(Mutex::new(DbStub::init("").await.unwrap()));
let cache = Arc::new(Mutex::new(DbStub::init("").await.unwrap()));
let cfilter = Arc::new(Mutex::new(cuckoofilter::CuckooFilter::new()));
let mut expected_entries = std::collections::HashMap::new();

for _ in 0..3 {
let key = Uuid::new_v4().to_string();
let value = Uuid::new_v4().to_string();
let data_id = Uuid::new_v4().to_string();

let set_request = SetRequestData {
address: TEST_VALID_ADDRESS.to_string(),
data: json!({ &key: &value }).to_string(),
data_id: data_id.clone(),
};

let request = warp::test::request()
.method("POST")
.header("public_key", TEST_VALID_PUB_KEY)
.header("address", TEST_VALID_ADDRESS)
.header("signature", TEST_VALID_SIG)
.path("/set_data")
.json(&set_request);

let filter = routes::set_data(db.clone(), cache.clone(), cfilter.clone(), 1000, 600)
.recover(handle_rejection);
let res = request.reply(&filter).await;

assert_eq!(res.status(), 200);
let res_body: serde_json::Value = serde_json::from_str(res.body()).unwrap();
assert_eq!(res_body["status"], "Success");
assert_eq!(res_body["reason"], "Data set successfully");
assert_eq!(res_body["route"], "set_data");
assert_eq!(res_body["content"], TEST_VALID_ADDRESS);

expected_entries.insert(data_id.clone(), (key, value));

let db_lock = db.lock().await;
for (id, (expected_key, expected_value)) in &expected_entries {
let stored_data = db_lock.get_data(TEST_VALID_ADDRESS, id).await.unwrap();
assert_eq!(stored_data, json!({ expected_key: expected_value }).to_string());
}
}
}

0 comments on commit 7eba3c0

Please sign in to comment.