From 402a508d517d83e362588be6e9d44f86ac088b72 Mon Sep 17 00:00:00 2001 From: kalm Date: Fri, 22 Nov 2024 22:48:23 -0800 Subject: [PATCH] explorer/test_util: add initial implementation of test_util module for reusable testing utilities This commit introduces the initial implementation a test_util module, providing reusable testing utilities for the explorer. The first version includes a function to initialize logging for test cases, which aids in debugging issues encountered during testing. This module will be expanded as we progress with both unit and integration testing. Summary of Updates: - Added the init_logger function that configures the logger based on a provided list of targets to ignore and the desired log level --- .../research/blockchain-explorer/src/main.rs | 3 ++ .../blockchain-explorer/src/test_utils/mod.rs | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 script/research/blockchain-explorer/src/test_utils/mod.rs diff --git a/script/research/blockchain-explorer/src/main.rs b/script/research/blockchain-explorer/src/main.rs index 7e3c77e75..7d55d291f 100644 --- a/script/research/blockchain-explorer/src/main.rs +++ b/script/research/blockchain-explorer/src/main.rs @@ -56,6 +56,9 @@ mod transactions; /// Database functionality related to statistics mod statistics; +/// Test utilities used for unit and integration testing +mod test_utils; + const CONFIG_FILE: &str = "blockchain_explorer_config.toml"; const CONFIG_FILE_CONTENTS: &str = include_str!("../blockchain_explorer_config.toml"); diff --git a/script/research/blockchain-explorer/src/test_utils/mod.rs b/script/research/blockchain-explorer/src/test_utils/mod.rs new file mode 100644 index 000000000..5b160fff3 --- /dev/null +++ b/script/research/blockchain-explorer/src/test_utils/mod.rs @@ -0,0 +1,45 @@ +/* This file is part of DarkFi (https://dark.fi) + * + * Copyright (C) 2020-2024 Dyne.org foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +/// Initializes logging for test cases, which is useful for debugging issues encountered during testing. +/// The logger is configured based on the provided list of targets to ignore and the desired log level. +#[cfg(test)] +pub fn init_logger(log_level: simplelog::LevelFilter, ignore_targets: Vec<&str>) { + let mut cfg = simplelog::ConfigBuilder::new(); + + // Add targets to ignore + for target in ignore_targets { + cfg.add_filter_ignore(target.to_string()); + } + + // Set log level + cfg.set_target_level(log_level); + + // initialize the logger + if simplelog::TermLogger::init( + log_level, + cfg.build(), + simplelog::TerminalMode::Mixed, + simplelog::ColorChoice::Auto, + ) + .is_err() + { + // Print an error message if logger failed to initialize + eprintln!("Logger failed to initialize"); + } +}