-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing TUs for ConfigNameGenerator and PerformanceResultsSaver
- Loading branch information
1 parent
09a0a2b
commit 40e0a87
Showing
2 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
from unittest.mock import Mock, patch | ||
from utils.config_name_generator import generate_config_name | ||
|
||
@pytest.fixture | ||
def mock_config_manager(): | ||
mock_manager = Mock() | ||
mock_manager.get_base_currency.return_value = "BTC" | ||
mock_manager.get_quote_currency.return_value = "USD" | ||
mock_manager.get_trading_mode.return_value.name = "LIVE" | ||
mock_manager.get_strategy_type.return_value.name = "GRID" | ||
mock_manager.get_spacing_type.return_value.name = "PERCENTAGE" | ||
mock_manager.get_num_grids.return_value = 10 | ||
mock_manager.get_top_range.return_value = 50000 | ||
mock_manager.get_bottom_range.return_value = 30000 | ||
return mock_manager | ||
|
||
@patch("utils.config_name_generator.datetime") | ||
def test_generate_config_name(mock_datetime, mock_config_manager): | ||
mock_datetime.now.return_value.strftime.return_value = "20241220_1200" | ||
|
||
result = generate_config_name(mock_config_manager) | ||
|
||
expected_name = ( | ||
"bot_BTC_USD_LIVE_strategyGRID_spacingPERCENTAGE_size10_range30000-50000_20241220_1200" | ||
) | ||
assert result == expected_name | ||
mock_config_manager.get_base_currency.assert_called_once() | ||
mock_config_manager.get_quote_currency.assert_called_once() | ||
mock_config_manager.get_trading_mode.assert_called_once() | ||
mock_config_manager.get_strategy_type.assert_called_once() | ||
mock_config_manager.get_spacing_type.assert_called_once() | ||
mock_config_manager.get_num_grids.assert_called_once() | ||
mock_config_manager.get_top_range.assert_called_once() | ||
mock_config_manager.get_bottom_range.assert_called_once() | ||
|
||
def test_generate_config_name_edge_cases(mock_config_manager): | ||
mock_config_manager.get_base_currency.return_value = "ETH" | ||
mock_config_manager.get_quote_currency.return_value = "EUR" | ||
mock_config_manager.get_num_grids.return_value = 0 | ||
mock_config_manager.get_top_range.return_value = 0 | ||
mock_config_manager.get_bottom_range.return_value = 0 | ||
|
||
result = generate_config_name(mock_config_manager) | ||
|
||
assert "bot_ETH_EUR" in result | ||
assert "_size0_range0-0" in result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
from unittest.mock import patch, mock_open | ||
from utils.performance_results_saver import save_or_append_performance_results | ||
from datetime import datetime, timedelta | ||
|
||
@pytest.fixture | ||
def new_results_fixture(): | ||
return { | ||
"config": "config.json", | ||
"performance_summary": { | ||
"start_time": datetime(2024, 12, 20, 10, 0, 0).isoformat(), | ||
"end_time": datetime(2024, 12, 20, 12, 0, 0).isoformat(), | ||
"total_profit": 500.0, | ||
"runtime": str(timedelta(hours=2)), | ||
}, | ||
"orders": [ | ||
["BUY", "LIMIT", "FILLED", 1000.0, 0.5, datetime(2024, 12, 20, 10, 30, 0).isoformat(), "Level 1", 0.1], | ||
["SELL", "LIMIT", "FILLED", 1500.0, 0.5, datetime(2024, 12, 20, 11, 30, 0).isoformat(), "Level 2", 0.05], | ||
], | ||
} | ||
|
||
def test_save_or_append_performance_results_invalid_json(new_results_fixture): | ||
with patch("builtins.open", mock_open(read_data="INVALID_JSON")) as mocked_file, \ | ||
patch("os.path.exists", return_value=True), \ | ||
patch("utils.performance_results_saver.logging.warning") as mock_logger_warning: | ||
|
||
save_or_append_performance_results(new_results_fixture, "results.json") | ||
|
||
mocked_file.assert_any_call("results.json", "r") | ||
mocked_file.assert_any_call("results.json", "w") | ||
mock_logger_warning.assert_called_once_with("Could not decode JSON from results.json. Overwriting the file.") | ||
|
||
def test_save_or_append_performance_results_os_error(new_results_fixture): | ||
with patch("builtins.open", side_effect=OSError("Test OS Error")), \ | ||
patch("utils.performance_results_saver.logging.error") as mock_logger_error: | ||
|
||
save_or_append_performance_results(new_results_fixture, "results.json") | ||
|
||
mock_logger_error.assert_called_once_with("Failed to save performance metrics to results.json: Test OS Error") | ||
|
||
def test_save_or_append_performance_results_unexpected_exception(new_results_fixture): | ||
with patch("builtins.open", side_effect=Exception("Unexpected Error")), \ | ||
patch("utils.performance_results_saver.logging.error") as mock_logger_error: | ||
|
||
save_or_append_performance_results(new_results_fixture, "results.json") | ||
|
||
mock_logger_error.assert_any_call("An unexpected error occurred while saving performance metrics: Unexpected Error") |