nautilus_testkit/
common.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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 Posei-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#[must_use]
59pub fn get_test_data_large_checksums_filepath() -> PathBuf {
60    get_test_data_path().join("large").join("checksums.json")
61}
62
63/// Ensures that the specified test data file exists locally by downloading it if necessary, using the provided `url`.
64///
65/// # Panics
66///
67/// Panics if the download or checksum verification fails, or if the resulting path cannot be represented as a valid UTF-8 string.
68#[must_use]
69pub fn ensure_test_data_exists(filename: &str, url: &str) -> PathBuf {
70    let filepath = get_test_data_path().join("large").join(filename);
71    let checksums_filepath = get_test_data_large_checksums_filepath();
72    ensure_file_exists_or_download_http(&filepath, url, Some(&checksums_filepath)).unwrap();
73    filepath
74}
75
76#[must_use]
77pub fn ensure_data_exists_tardis_deribit_book_l2() -> PathBuf {
78    let filename = "tardis_deribit_incremental_book_L2_2020-04-01_BTC-PERPETUAL.csv.gz";
79    let base_url = "https://datasets.tardis.dev";
80    let url = format!("{base_url}/v1/deribit/incremental_book_L2/2020/04/01/BTC-PERPETUAL.csv.gz");
81    ensure_test_data_exists(filename, &url)
82}
83
84#[must_use]
85pub fn ensure_data_exists_tardis_binance_snapshot5() -> PathBuf {
86    let filename = "tardis_binance-futures_book_snapshot_5_2020-09-01_BTCUSDT.csv.gz";
87    let base_url = "https://datasets.tardis.dev";
88    let url = format!("{base_url}/v1/binance-futures/book_snapshot_5/2020/09/01/BTCUSDT.csv.gz");
89    ensure_test_data_exists(filename, &url)
90}
91
92#[must_use]
93pub fn ensure_data_exists_tardis_binance_snapshot25() -> PathBuf {
94    let filename = "tardis_binance-futures_book_snapshot_25_2020-09-01_BTCUSDT.csv.gz";
95    let base_url = "https://datasets.tardis.dev";
96    let url = format!("{base_url}/v1/binance-futures/book_snapshot_25/2020/09/01/BTCUSDT.csv.gz");
97    ensure_test_data_exists(filename, &url)
98}
99
100#[must_use]
101pub fn ensure_data_exists_tardis_huobi_quotes() -> PathBuf {
102    let filename = "tardis_huobi-dm-swap_quotes_2020-05-01_BTC-USD.csv.gz";
103    let base_url = "https://datasets.tardis.dev";
104    let url = format!("{base_url}/v1/huobi-dm-swap/quotes/2020/05/01/BTC-USD.csv.gz");
105    ensure_test_data_exists(filename, &url)
106}
107
108#[must_use]
109pub fn ensure_data_exists_tardis_bitmex_trades() -> PathBuf {
110    let filename = "tardis_bitmex_trades_2020-03-01_XBTUSD.csv.gz";
111    let base_url = "https://datasets.tardis.dev";
112    let url = format!("{base_url}/v1/bitmex/trades/2020/03/01/XBTUSD.csv.gz");
113    ensure_test_data_exists(filename, &url)
114}