Skip to content

Commit

Permalink
mock_macro: Use initialize_database everywhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyhunsen committed Dec 12, 2024
1 parent dd0091d commit 7402354
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 48 deletions.
43 changes: 0 additions & 43 deletions core/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions core/src/mock/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions core/src/mock_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions core/src/rpc/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/rpc/watchtower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions core/src/watchtower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7402354

Please sign in to comment.