refactor tests

modularised common code
cleaned grpc_test file
This commit is contained in:
Noor 2025-04-24 19:19:35 +05:30
parent 351f0c5462
commit 34bd7617bc
Signed by: noormohammedb
GPG Key ID: D83EFB8B3B967146
4 changed files with 63 additions and 59 deletions

2
tests/common/mod.rs Normal file

@ -0,0 +1,2 @@
pub mod prepare_test_env;
pub mod test_utils;

@ -0,0 +1,55 @@
use super::test_utils::{get_pub_key, sign_request};
use detee_shared::{
general_proto::brain_general_cli_server::BrainGeneralCliServer,
vm_proto::brain_vm_cli_server::BrainVmCliServer,
};
use surreal_brain::grpc::{BrainGeneralCliMock, BrainVmCliMock};
use tokio::sync::OnceCell;
use tonic::transport::Channel;
pub const DB_URL: &str = "localhost:8000";
pub const DB_NS: &str = "test_brain";
pub const DB_NAME: &str = "test_migration_db";
pub const GRPC_ADDR: &str = "127.0.0.1:31337";
pub static TEST_STATE: OnceCell<Channel> = OnceCell::const_new();
pub async fn prepare_test_db() {
surreal_brain::db::init(DB_URL, DB_NS, DB_NAME)
.await
.expect("Failed to initialize the database");
let old_brain_data = surreal_brain::old_brain::BrainData::load_from_disk().unwrap();
// cleanup old brain data
surreal_brain::db::migration0(&old_brain_data).await.unwrap();
}
pub async fn fake_grpc_server() {
tonic::transport::Server::builder()
.add_service(BrainGeneralCliServer::new(BrainGeneralCliMock {}))
.add_service(BrainVmCliServer::new(BrainVmCliMock {}))
.serve(GRPC_ADDR.parse().unwrap())
.await
.unwrap();
}
pub async fn fake_grpc_client() -> Channel {
let url = format!("http://{GRPC_ADDR}");
Channel::from_shared(url).unwrap().connect().await.unwrap()
}
pub async fn prepare_test_setup() {
TEST_STATE
.get_or_init(|| async {
prepare_test_db().await;
tokio::spawn(async {
fake_grpc_server().await;
});
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
fake_grpc_client().await
})
.await;
}

@ -1,64 +1,9 @@
use detee_shared::{
common_proto::Pubkey,
general_proto::{
brain_general_cli_client::BrainGeneralCliClient,
brain_general_cli_server::BrainGeneralCliServer,
},
vm_proto::brain_vm_cli_server::BrainVmCliServer,
common_proto::Pubkey, general_proto::brain_general_cli_client::BrainGeneralCliClient,
};
use surreal_brain::grpc::{BrainGeneralCliMock, BrainVmCliMock};
use test_utils::{get_pub_key, sign_request};
use tokio::sync::OnceCell;
use tonic::transport::Channel;
mod test_utils;
const DB_URL: &str = "localhost:8000";
const DB_NS: &str = "test_brain";
const DB_NAME: &str = "test_migration_db";
const GRPC_ADDR: &str = "127.0.0.1:31337";
static TEST_STATE: OnceCell<Channel> = OnceCell::const_new();
async fn prepare_test_db() {
surreal_brain::db::init(DB_URL, DB_NS, DB_NAME)
.await
.expect("Failed to initialize the database");
let old_brain_data = surreal_brain::old_brain::BrainData::load_from_disk().unwrap();
// cleanup old brain data
surreal_brain::db::migration0(&old_brain_data).await.unwrap();
}
async fn fake_grpc_server() {
tonic::transport::Server::builder()
.add_service(BrainGeneralCliServer::new(BrainGeneralCliMock {}))
.add_service(BrainVmCliServer::new(BrainVmCliMock {}))
.serve(GRPC_ADDR.parse().unwrap())
.await
.unwrap();
}
async fn fake_grpc_client() -> Channel {
let url = format!("http://{GRPC_ADDR}");
Channel::from_shared(url).unwrap().connect().await.unwrap()
}
async fn prepare_test_setup() {
TEST_STATE
.get_or_init(|| async {
prepare_test_db().await;
tokio::spawn(async {
fake_grpc_server().await;
});
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
fake_grpc_client().await
})
.await;
}
mod common;
use common::prepare_test_env::{prepare_test_setup, TEST_STATE};
use common::test_utils::{get_pub_key, sign_request};
#[tokio::test]
async fn test_general_balance() {
@ -75,6 +20,8 @@ async fn test_general_balance() {
.unwrap()
.into_inner();
// verify it in db also
assert_eq!(acc_bal.balance, 0);
assert_eq!(acc_bal.tmp_locked, 0);
}