Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ jobs:
run: |
source $VENV
coverage run -m unittest discover -s tests
- name: Generate coverage report
if: always()
continue-on-error: true
run: |
source $VENV
coverage xml
#----------------------------------------------
# upload coverage stats
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

41 changes: 20 additions & 21 deletions tests/app/reporting/test_backtest_report.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
from datetime import datetime, timezone
from unittest import TestCase

Expand All @@ -23,6 +24,11 @@ def setUp(self):
"resources"
)
)
self.output_path = os.path.join(self.resource_dir, "backtest_report")

def tearDown(self):
if os.path.exists(self.output_path):
shutil.rmtree(self.output_path)

def test_save_without_algorithm(self):
"""
Expand All @@ -47,8 +53,8 @@ def test_save_without_algorithm(self):
)
]
backtest_date_range = BacktestDateRange(
start_date="2023-08-07 07:00:00",
end_date="2023-12-02 00:00:00",
start_date=datetime(2023, 8, 7, 7, 0, tzinfo=timezone.utc),
end_date=datetime(2023, 12, 2, 0, 0, tzinfo=timezone.utc),
name="Test Backtest Date Range"
)
run = BacktestRun(
Expand All @@ -64,36 +70,32 @@ def test_save_without_algorithm(self):
initial_unallocated=1000,
created_at=datetime.now(tz=timezone.utc)
)
data_files = [
os.path.join("tests", "resources", "market_data_sources_for_testing", "OHLCV_BTC-EUR_BINANCE_2h_2023-08-07-07-59_2023-12-02-00-00.csv"),
os.path.join("tests", "resources", "market_data_sources_for_testing", "OHLCV_BTC-EUR_BINANCE_15m_2023-12-14-22-00_2023-12-25-00-00.csv"),
]

backtest = Backtest(
algorithm_id="alg-025",
backtest_runs=[run],
)
output_path = os.path.join(self.resource_dir, "backtest_report")
backtest.save(output_path)
backtest.save(self.output_path)

# Check if the report was saved correctly
self.assertTrue(os.path.exists(output_path))
self.assertTrue(os.path.exists(self.output_path))

# Check if the runs directory exists
runs_dir = os.path.join(output_path, "runs")
runs_dir = os.path.join(self.output_path, "runs")
self.assertTrue(os.path.exists(runs_dir))

# Check if the backtest run directory exists
backtest_run_dir = os.path.join(
runs_dir, "backtest_EUR_20230807_20231201"
runs_dir, "backtest_EUR_20230807_20231202"
)
self.assertTrue(os.path.exists(backtest_run_dir))

# Check if the results were saved correctly
self.assertTrue(
os.path.exists(os.path.join(backtest_run_dir, "run.json"))
)
self.assertTrue(
# No backtest_metrics provided, so metrics.json should not exist
self.assertFalse(
os.path.exists(os.path.join(backtest_run_dir, "metrics.json"))
)

Expand All @@ -118,8 +120,8 @@ def test_save_with_strategies_directory(self):
)
]
backtest_date_range = BacktestDateRange(
start_date="2023-08-07 07:00:00",
end_date="2023-12-02 00:00:00",
start_date=datetime(2023, 8, 7, 7, 0, tzinfo=timezone.utc),
end_date=datetime(2023, 12, 2, 0, 0, tzinfo=timezone.utc),
name="Test Backtest Date Range"
)
results = BacktestRun(
Expand Down Expand Up @@ -160,21 +162,18 @@ def test_save_with_strategies_directory(self):
backtest_runs=[results],
risk_free_rate=0.0
)
output_path = os.path.join(self.resource_dir, "backtest_report")
backtest.save(output_path)

print(output_path)
backtest.save(self.output_path)

# Check if the report was saved correctly
self.assertTrue(os.path.exists(output_path))
self.assertTrue(os.path.exists(self.output_path))

# Check if the runs directory exists
runs_dir = os.path.join(output_path, "runs")
runs_dir = os.path.join(self.output_path, "runs")
self.assertTrue(os.path.exists(runs_dir))

# Check if the backtest run directory exists
backtest_run_dir = os.path.join(
runs_dir, "backtest_EUR_20230807_20231201"
runs_dir, "backtest_EUR_20230807_20231202"
)
self.assertTrue(os.path.exists(backtest_run_dir))

Expand Down
8 changes: 4 additions & 4 deletions tests/domain/backtests/test_backtest_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def _create_snapshots(self):

def _create_date_range(self):
return BacktestDateRange(
start_date="2023-08-07 07:00:00",
end_date="2023-12-02 00:00:00",
start_date=datetime(2023, 8, 7, 7, 0, tzinfo=timezone.utc),
end_date=datetime(2023, 12, 2, 0, 0, tzinfo=timezone.utc),
name="Test Backtest Date Range"
)

Expand Down Expand Up @@ -80,7 +80,7 @@ def test_save_without_metrics(self):
self.assertTrue(os.path.exists(runs_dir))

backtest_run_dir = os.path.join(
runs_dir, "backtest_EUR_20230807_20231201"
runs_dir, "backtest_EUR_20230807_20231202"
)
self.assertTrue(os.path.exists(backtest_run_dir))
self.assertTrue(
Expand Down Expand Up @@ -144,7 +144,7 @@ def test_save_with_precomputed_metrics(self):
self.assertTrue(os.path.exists(runs_dir))

backtest_run_dir = os.path.join(
runs_dir, "backtest_EUR_20230807_20231201"
runs_dir, "backtest_EUR_20230807_20231202"
)
self.assertTrue(os.path.exists(backtest_run_dir))
self.assertTrue(
Expand Down
7 changes: 5 additions & 2 deletions tests/domain/utils/test_polars.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ def test_convert_pandas_to_polars(self):
column_names = polars_df_converted.columns.tolist()
self.assertEqual(set(column_names), {'Close'})

# Check if the index is a datetime object
self.assertEqual(polars_df_converted.index.dtype, "datetime64[us]")
# Check if the index is a datetime object (accept both ns and us
# resolution since it depends on pandas version)
self.assertTrue(
str(polars_df_converted.index.dtype).startswith("datetime64")
)
self.assertEqual(
polars_df_converted.index[0], Timestamp('2021-01-01 00:00:00')
)
22 changes: 11 additions & 11 deletions tests/infrastructure/data_providers/test_csv_ohlcv_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_right_columns(self):
file_name = "OHLCV_BTC-EUR_BINANCE" \
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=f"{self.resource_dir}/market_data_sources/"
storage_path=f"{self.resource_dir}/test_data/ohlcv/"
f"{file_name}",
window_size=10,
market="binance",
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_throw_exception_when_missing_column_names_columns(self):
CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir,
"market_data_sources_for_testing",
"test_data", "ohlcv",
file_name
),
window_size=10,
Expand All @@ -85,7 +85,7 @@ def test_has_data(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
window_size=10,
market="binance",
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_has_data_backtest_mode(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
window_size=10,
market="binance",
Expand Down Expand Up @@ -167,7 +167,7 @@ def test_get_data_start_date(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
csv_ohlcv_market_data_source = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
window_size=200,
market="binance",
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_get_data_end_date(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
csv_ohlcv_market_data_source = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
window_size=200,
market="binance",
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_get_identifier(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
data_provider_identifier="test",
window_size=10,
Expand All @@ -254,7 +254,7 @@ def test_get_market(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
market="test",
symbol="BTC/EUR",
Expand All @@ -268,7 +268,7 @@ def test_get_symbol(self):
"_2h_2023-08-07-07-59_2023-12-02-00-00.csv"
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
symbol="BTC/EUR",
window_size=10,
Expand All @@ -289,7 +289,7 @@ def test_prepare_backtest_data(self):
)
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
data_provider_identifier="test",
market="binance",
Expand Down Expand Up @@ -345,7 +345,7 @@ def test_get_backtest_data(self):
)
data_provider = CSVOHLCVDataProvider(
storage_path=os.path.join(
self.resource_dir, "market_data_sources", file_name
self.resource_dir, "test_data", "ohlcv", file_name
),
data_provider_identifier="test",
market="binance",
Expand Down
Loading
Loading