From 7402354550f5ac056911c47add70fcdb36cf3de9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ceyhun=20=C5=9Een?= Date: Thu, 12 Dec 2024 12:23:37 +0300 Subject: [PATCH] mock_macro: Use initialize_database everywhere. --- core/src/database/mod.rs | 43 -------------------------------------- core/src/mock/database.rs | 4 ++-- core/src/mock_macro.rs | 5 ++--- core/src/rpc/aggregator.rs | 1 + core/src/rpc/watchtower.rs | 1 + core/src/watchtower.rs | 1 + 6 files changed, 7 insertions(+), 48 deletions(-) diff --git a/core/src/database/mod.rs b/core/src/database/mod.rs index a9486de1..e84c70bd 100644 --- a/core/src/database/mod.rs +++ b/core/src/database/mod.rs @@ -39,29 +39,6 @@ impl Database { self.connection.close().await; } - /// Initializes a new database with given configuration. If the database is - /// already initialized, it will be dropped before initialization. Meaning, - /// a clean state is guaranteed. - /// - /// [`Database::new`] must be called after this to connect to the - /// initialized database. - /// - /// **Warning:** This must not be used in release environments and is only - /// suitable for testing. - /// - /// TODO: This function must be marked with `#[cfg(test)]` to prevent it - /// from infiltrating the binaries. See: - /// https://github.com/chainwayxyz/clementine/issues/181 - pub async fn initialize_database(config: &BridgeConfig) -> Result<(), BridgeError> { - Database::drop_database(config).await?; - - Database::create_database(config).await?; - - Database::run_schema_script(config).await?; - - Ok(()) - } - /// Creates a new database with given configuration. /// /// # Errors @@ -200,26 +177,6 @@ mod tests { assert!(Database::new(&config).await.is_err()); } - #[tokio::test] - async fn initialize_database() { - let mut config = common::get_test_config("test_config.toml").unwrap(); - config.db_name = "initialize_database".to_string(); - - // Drop database (clear previous test run artifacts) and check that - // connection can't be established. - Database::drop_database(&config).await.unwrap(); - assert!(Database::new(&config).await.is_err()); - - // It should be possible to initialize and connect to the new database. - Database::initialize_database(&config).await.unwrap(); - Database::new(&config).await.unwrap(); - - // Dropping database again should result connection to not be - // established. - Database::drop_database(&config).await.unwrap(); - assert!(Database::new(&config).await.is_err()); - } - #[test] fn get_postgresql_url() { let mut config = BridgeConfig::new(); diff --git a/core/src/mock/database.rs b/core/src/mock/database.rs index d74c4007..7bd567cb 100644 --- a/core/src/mock/database.rs +++ b/core/src/mock/database.rs @@ -3,7 +3,7 @@ //! This module provides mock database interfaces, for testing. use super::common; -use crate::{config::BridgeConfig, database::Database, utils::initialize_logger}; +use crate::{config::BridgeConfig, utils::initialize_logger}; use std::thread; /// Creates a temporary database for testing. @@ -21,7 +21,7 @@ async fn create_test_config(db_name: &str, config_file: &str) -> BridgeConfig { let mut config = common::get_test_config(config_file).unwrap(); config.db_name = db_name.to_string(); - Database::initialize_database(&config).await.unwrap(); + // Database::initialize_database(&config).await.unwrap(); config } diff --git a/core/src/mock_macro.rs b/core/src/mock_macro.rs index 39ded46a..ecb30c6f 100644 --- a/core/src/mock_macro.rs +++ b/core/src/mock_macro.rs @@ -128,6 +128,7 @@ macro_rules! initialize_database { /// config::BridgeConfig, /// database::Database, /// errors::BridgeError, +/// initialize_database, /// extended_rpc::ExtendedRpc, /// servers::{ /// create_aggregator_grpc_server, create_operator_grpc_server, @@ -172,9 +173,7 @@ macro_rules! create_actors { let mut config_with_new_db = $config.clone(); async move { config_with_new_db.db_name += &i; - Database::initialize_database(&config_with_new_db) - .await - .unwrap(); + initialize_database!(&config_with_new_db); let verifier = create_verifier_grpc_server( BridgeConfig { diff --git a/core/src/rpc/aggregator.rs b/core/src/rpc/aggregator.rs index f31ccdbd..c28b811f 100644 --- a/core/src/rpc/aggregator.rs +++ b/core/src/rpc/aggregator.rs @@ -407,6 +407,7 @@ mod tests { config::BridgeConfig, database::Database, errors::BridgeError, + initialize_database, servers::{ create_aggregator_grpc_server, create_operator_grpc_server, create_verifier_grpc_server, create_watchtower_grpc_server, diff --git a/core/src/rpc/watchtower.rs b/core/src/rpc/watchtower.rs index 5e2d7770..5f2766e5 100644 --- a/core/src/rpc/watchtower.rs +++ b/core/src/rpc/watchtower.rs @@ -35,6 +35,7 @@ mod tests { database::Database, errors::BridgeError, extended_rpc::ExtendedRpc, + initialize_database, servers::{ create_aggregator_grpc_server, create_operator_grpc_server, create_verifier_grpc_server, create_watchtower_grpc_server, diff --git a/core/src/watchtower.rs b/core/src/watchtower.rs index 47703772..966b4fc5 100644 --- a/core/src/watchtower.rs +++ b/core/src/watchtower.rs @@ -78,6 +78,7 @@ mod tests { database::Database, errors::BridgeError, extended_rpc::ExtendedRpc, + initialize_database, servers::{ create_aggregator_grpc_server, create_operator_grpc_server, create_verifier_grpc_server, create_watchtower_grpc_server,