Skip to main content

longbridge_candlesticks/
testutil.rs

1use time::{Date, Month, OffsetDateTime, Time};
2use time_tz::PrimitiveDateTimeExt;
3
4use crate::{Market, Period, TradeSessionKind};
5
6pub struct TestCandlestickTime<'a> {
7    market: &'a Market,
8    period: Period,
9}
10
11impl<'a> TestCandlestickTime<'a> {
12    #[inline]
13    pub fn new(market: &'a Market, period: Period) -> Self {
14        Self { market, period }
15    }
16
17    #[track_caller]
18    pub fn check_time(&self, ts: TradeSessionKind, input: Time, expected: impl Into<Option<Time>>) {
19        let date = Date::from_calendar_date(2024, Month::January, 1).unwrap();
20        assert_eq!(
21            self.market.candlestick_time(
22                ts,
23                false,
24                self.period,
25                date.with_time(input)
26                    .assume_timezone(self.market.timezone)
27                    .unwrap_first(),
28            ),
29            expected.into().map(|expected| date
30                .with_time(expected)
31                .assume_timezone(self.market.timezone)
32                .unwrap_first())
33        );
34    }
35
36    #[track_caller]
37    pub fn check_datetime(
38        &self,
39        ts: TradeSessionKind,
40        input: OffsetDateTime,
41        expected: impl Into<Option<OffsetDateTime>>,
42    ) {
43        assert_eq!(
44            self.market.candlestick_time(ts, false, self.period, input),
45            expected.into()
46        );
47    }
48}