nautilus_testkit/
common.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::path::PathBuf;
17
18use nautilus_core::paths::get_test_data_path;
19
20use crate::files::ensure_file_exists_or_download_http;
21
22/// Returns the full path to the test data file at the specified relative `path` within the standard test data directory.
23///
24/// # Panics
25///
26/// Panics if the computed path cannot be represented as a valid UTF-8 string.
27#[must_use]
28pub fn get_test_data_file_path(path: &str) -> String {
29    get_test_data_path()
30        .join(path)
31        .to_str()
32        .unwrap()
33        .to_string()
34}
35
36/// Returns the full path to the Nautilus-specific test data file given by `filename`, within the configured precision directory ("64-bit" or "128-bit").
37///
38/// # Panics
39///
40/// Panics if the computed path cannot be represented as a valid UTF-8 string.
41#[must_use]
42#[allow(unused_mut)]
43pub fn get_nautilus_test_data_file_path(filename: &str) -> String {
44    let mut path = get_test_data_path().join("nautilus");
45
46    #[cfg(feature = "high-precision")]
47    {
48        path = path.join("128-bit");
49    }
50    #[cfg(not(feature = "high-precision"))]
51    {
52        path = path.join("64-bit");
53    }
54
55    path.join(filename).to_str().unwrap().to_string()
56}
57
58/// Returns the path to the checksums file for large test data files.
59#[must_use]
60pub fn get_test_data_large_checksums_filepath() -> PathBuf {
61    get_test_data_path().join("large").join("checksums.json")
62}
63
64/// Ensures that the specified test data file exists locally by downloading it if necessary, using the provided `url`.
65///
66/// # Panics
67///
68/// Panics if the download or checksum verification fails, or if the resulting path cannot be represented as a valid UTF-8 string.
69#[must_use]
70pub fn ensure_test_data_exists(filename: &str, url: &str) -> PathBuf {
71    let filepath = get_test_data_path().join("large").join(filename);
72    let checksums_filepath = get_test_data_large_checksums_filepath();
73    ensure_file_exists_or_download_http(&filepath, url, Some(&checksums_filepath), None).unwrap();
74    filepath
75}
76
77/// Returns the path to the Tardis Deribit incremental book L2 test data.
78#[must_use]
79pub fn get_tardis_deribit_book_l2_path() -> PathBuf {
80    get_test_data_path()
81        .join("tardis")
82        .join("deribit_incremental_book_L2_BTC-PERPETUAL.csv")
83}
84
85/// Returns the path to the Tardis Binance Futures book snapshot (depth 5) test data.
86#[must_use]
87pub fn get_tardis_binance_snapshot5_path() -> PathBuf {
88    get_test_data_path()
89        .join("tardis")
90        .join("binance-futures_book_snapshot_5_BTCUSDT.csv")
91}
92
93/// Returns the path to the Tardis Binance Futures book snapshot (depth 25) test data.
94#[must_use]
95pub fn get_tardis_binance_snapshot25_path() -> PathBuf {
96    get_test_data_path()
97        .join("tardis")
98        .join("binance-futures_book_snapshot_25_BTCUSDT.csv")
99}
100
101/// Returns the path to the Tardis Huobi quotes test data.
102#[must_use]
103pub fn get_tardis_huobi_quotes_path() -> PathBuf {
104    get_test_data_path()
105        .join("tardis")
106        .join("huobi-dm-swap_quotes_BTC-USD.csv")
107}
108
109/// Returns the path to the Tardis Bitmex trades test data.
110#[must_use]
111pub fn get_tardis_bitmex_trades_path() -> PathBuf {
112    get_test_data_path()
113        .join("tardis")
114        .join("bitmex_trades_XBTUSD.csv")
115}