Skip to content

Commit

Permalink
FIx for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BHouwens committed Aug 20, 2024
1 parent 26634d6 commit a090697
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern_port = 3030
cache_url = "redis://0.0.0.0"
cache_port = "6379"
cache_password = ""
db_protocol = "mongodb://0.0.0.0"
db_protocol = "mongodb://127.0.0.1"
db_url = ""
db_port = "27017"
db_user = ""
Expand Down
15 changes: 9 additions & 6 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::api::utils::{retrieve_from_db, delete_from_db, serialize_all_entries};
use crate::interfaces::{SetRequestData, SetSaveData};
use crate::api::utils::{delete_from_db, retrieve_from_db, serialize_all_entries};
use crate::db::handler::{CacheHandler, KvStoreConnection};
use crate::interfaces::{SetRequestData, SetSaveData};
use futures::lock::Mutex;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{error, info, debug};
use tracing::{debug, error, info};
use valence_core::api::errors::ApiErrorType;
use valence_core::api::interfaces::CFilterConnection;
use valence_core::api::responses::{json_serialize_embed, CallResponse, JsonReply};
Expand Down Expand Up @@ -42,7 +42,7 @@ pub async fn get_data_handler<

// Check if address is in cuckoo filter
if !c_filter.lock().await.contains(&address) {
error!("{}", ApiErrorType::CuckooFilterLookupFailed );
error!("{}", ApiErrorType::CuckooFilterLookupFailed);
return r.into_err_internal(ApiErrorType::CuckooFilterLookupFailed);
}

Expand Down Expand Up @@ -79,7 +79,10 @@ pub async fn get_data_handler<
}
None => {
// Default to checking from DB if cache is empty
debug!("Cache lookup failed for address: {}, attempting to retrieve data from DB", address);
debug!(
"Cache lookup failed for address: {}, attempting to retrieve data from DB",
address
);
retrieve_from_db(db, address, value_id.as_deref()).await
}
}
Expand Down Expand Up @@ -211,7 +214,7 @@ pub async fn del_data_handler<
match cache_result {
Ok(_) => {
debug!("Data deleted from cache");
return delete_from_db(db, address, value_id.as_deref()).await
return delete_from_db(db, address, value_id.as_deref()).await;
}
Err(_) => {
error!("Cache deletion failed for address: {}", address);
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::api::handlers::{get_data_handler, set_data_handler, del_data_handler};
use crate::api::handlers::{del_data_handler, get_data_handler, set_data_handler};
use crate::db::handler::{CacheHandler, KvStoreConnection};
use futures::lock::Mutex;
use std::sync::Arc;
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// main.rs
pub mod api;
pub mod db;
pub mod constants;
pub mod db;
pub mod interfaces;
pub mod utils;

Expand Down Expand Up @@ -34,7 +34,7 @@ async fn main() {

info!("Connecting to Redis at {}", cache_addr);
info!("Connecting to MongoDB at {}", db_addr);

let cache_conn = construct_redis_conn(&cache_addr).await;
let db_conn = construct_mongodb_conn(&db_addr).await;

Expand Down
32 changes: 19 additions & 13 deletions src/tests/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::{collections::HashMap, hash::Hash};

use crate::db::handler::{CacheHandler, KvStoreConnection};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::db::handler::{CacheHandler, KvStoreConnection};
use valence_core::utils::serialize_data;

//========== STUB INTERFACES ==========//
Expand Down Expand Up @@ -43,19 +43,15 @@ impl KvStoreConnection for DbStub {
_key: &str,
_value_id: Option<&str>,
) -> Result<Option<HashMap<String, T>>, Box<dyn std::error::Error + Send + Sync>> {
if self.data.is_none() {
return Ok(None);
}

let data = match self.data.clone() {
Some(d) => d,
None => return Ok(None),
None => {
println!("No data found");
return Ok(None);
}
};

match get_de_data::<HashMap<String, T>>(data) {
Ok(d) => Ok(Some(d)),
Err(_) => Ok(None),
}
Ok(Some(get_de_data(data)))
}

async fn del_data(
Expand Down Expand Up @@ -92,6 +88,16 @@ impl KvStoreConnection for DbStub {
}
}

fn get_de_data<T: DeserializeOwned>(v: String) -> Result<T, serde_json::Error> {
serde_json::from_str(&v)
fn get_de_data<T: DeserializeOwned>(v: String) -> HashMap<String, T> {
let value: serde_json::Value = match serde_json::from_str(&v) {
Ok(v) => v,
Err(_) => {
println!("Failed to deserialize data");
return HashMap::new();
}
};
let de_map: HashMap<String, T> =
serde_json::from_str(&value.as_str().unwrap()).unwrap_or(HashMap::new());

de_map
}
6 changes: 4 additions & 2 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ pub mod constants;
pub mod interfaces;

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 crate::db::handler::KvStoreConnection;
use futures::lock::Mutex;
use std::sync::Arc;
use valence_core::api::utils::handle_rejection;
Expand Down Expand Up @@ -82,13 +82,15 @@ async fn test_get_data() {
let filter = routes::get_data(db_stub, cache_stub, cfilter).recover(handle_rejection);
let res = request.reply(&filter).await;

println!("{:?}", res.body());

//
// Assert
//
assert_eq!(res.status(), 200);
assert_eq!(
res.body(),
"{\"status\":\"Success\",\"reason\":\"Data retrieved successfully\",\"route\":\"get_data\",\"content\":null}"
"{\"status\":\"Success\",\"reason\":\"Data retrieved successfully\",\"route\":\"get_data\",\"content\":{}}"
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use crate::constants::{
SETTINGS_CACHE_URL, SETTINGS_DB_PASSWORD, SETTINGS_DB_PORT, SETTINGS_DB_PROTOCOL,
SETTINGS_DB_URL, SETTINGS_DEBUG, SETTINGS_EXTERN_PORT,
};
use crate::interfaces::EnvConfig;
use crate::db::handler::KvStoreConnection;
use crate::db::mongo_db::MongoDbConn;
use crate::db::redis_cache::RedisCacheConn;
use crate::interfaces::EnvConfig;
use chrono::prelude::*;
use cuckoofilter::{CuckooFilter, ExportedCuckooFilter};
use futures::lock::Mutex;
Expand Down

0 comments on commit a090697

Please sign in to comment.