nautilus_testkit/common.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 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/// Ensures the NASDAQ ITCH AAPL deltas Parquet file exists locally, downloading from R2 if necessary.
78///
79/// # Panics
80///
81/// Panics if the download or checksum verification fails.
82#[must_use]
83pub fn ensure_itch_aapl_deltas_parquet() -> PathBuf {
84 ensure_test_data_exists(
85 "itch_AAPL.XNAS_2019-01-30_deltas.parquet",
86 "https://test-data.nautechsystems.io/large/itch_AAPL.XNAS_2019-01-30_deltas.parquet",
87 )
88}
89
90/// Ensures the Tardis Deribit BTC-PERPETUAL deltas Parquet file exists locally, downloading from R2 if necessary.
91///
92/// # Panics
93///
94/// Panics if the download or checksum verification fails.
95#[must_use]
96pub fn ensure_tardis_deribit_deltas_parquet() -> PathBuf {
97 ensure_test_data_exists(
98 "tardis_BTC-PERPETUAL.DERIBIT_2020-04-01_deltas.parquet",
99 "https://test-data.nautechsystems.io/large/tardis_BTC-PERPETUAL.DERIBIT_2020-04-01_deltas.parquet",
100 )
101}
102
103/// Returns the path to the Tardis Deribit incremental book L2 test data.
104#[must_use]
105pub fn get_tardis_deribit_book_l2_path() -> PathBuf {
106 get_test_data_path()
107 .join("tardis")
108 .join("deribit_incremental_book_L2_BTC-PERPETUAL.csv")
109}
110
111/// Returns the path to the Tardis Binance Futures book snapshot (depth 5) test data.
112#[must_use]
113pub fn get_tardis_binance_snapshot5_path() -> PathBuf {
114 get_test_data_path()
115 .join("tardis")
116 .join("binance-futures_book_snapshot_5_BTCUSDT.csv")
117}
118
119/// Returns the path to the Tardis Binance Futures book snapshot (depth 25) test data.
120#[must_use]
121pub fn get_tardis_binance_snapshot25_path() -> PathBuf {
122 get_test_data_path()
123 .join("tardis")
124 .join("binance-futures_book_snapshot_25_BTCUSDT.csv")
125}
126
127/// Returns the path to the Tardis Huobi quotes test data.
128#[must_use]
129pub fn get_tardis_huobi_quotes_path() -> PathBuf {
130 get_test_data_path()
131 .join("tardis")
132 .join("huobi-dm-swap_quotes_BTC-USD.csv")
133}
134
135/// Returns the path to the Tardis Bitmex trades test data.
136#[must_use]
137pub fn get_tardis_bitmex_trades_path() -> PathBuf {
138 get_test_data_path()
139 .join("tardis")
140 .join("bitmex_trades_XBTUSD.csv")
141}