Skip to main content

nautilus_model/events/order/spec/
pending_update.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 nautilus_core::{UUID4, UnixNanos};
17
18use crate::{
19    events::OrderPendingUpdate,
20    identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
21    stubs::{TestDefault, test_uuid},
22};
23
24/// Test-only fluent spec for [`OrderPendingUpdate`].
25///
26/// All fields carry sensible defaults so callers only set what differs.
27/// `build()` constructs the event through [`OrderPendingUpdate::new`] so any future invariants
28/// added to the production constructor are exercised by tests built on this spec.
29#[derive(Debug, Clone, bon::Builder)]
30#[builder(finish_fn = into_spec)]
31pub struct OrderPendingUpdateSpec {
32    #[builder(default = TraderId::test_default())]
33    pub trader_id: TraderId,
34    #[builder(default = StrategyId::test_default())]
35    pub strategy_id: StrategyId,
36    #[builder(default = InstrumentId::test_default())]
37    pub instrument_id: InstrumentId,
38    #[builder(default = ClientOrderId::test_default())]
39    pub client_order_id: ClientOrderId,
40    #[builder(default = AccountId::test_default())]
41    pub account_id: AccountId,
42    #[builder(default = test_uuid())]
43    pub event_id: UUID4,
44    #[builder(default = UnixNanos::default())]
45    pub ts_event: UnixNanos,
46    #[builder(default = UnixNanos::default())]
47    pub ts_init: UnixNanos,
48    #[builder(default = false)]
49    pub reconciliation: bool,
50    pub venue_order_id: Option<VenueOrderId>,
51}
52
53impl<S: order_pending_update_spec_builder::IsComplete> OrderPendingUpdateSpecBuilder<S> {
54    /// Builds the spec and constructs an [`OrderPendingUpdate`] through its production constructor.
55    #[must_use]
56    pub fn build(self) -> OrderPendingUpdate {
57        let spec = self.into_spec();
58        OrderPendingUpdate::new(
59            spec.trader_id,
60            spec.strategy_id,
61            spec.instrument_id,
62            spec.client_order_id,
63            spec.account_id,
64            spec.event_id,
65            spec.ts_event,
66            spec.ts_init,
67            spec.reconciliation,
68            spec.venue_order_id,
69        )
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use rstest::rstest;
76
77    use super::*;
78    use crate::stubs::reset_test_uuid_rng;
79
80    #[rstest]
81    fn defaults_are_sensible() {
82        // Pin the spec's no-arg defaults so accidental drift in any individual default surfaces here,
83        // rather than as silent behavior change in downstream tests.
84        let event = OrderPendingUpdateSpec::builder().build();
85        assert_eq!(event.trader_id, TraderId::test_default());
86        assert_eq!(event.strategy_id, StrategyId::test_default());
87        assert_eq!(event.instrument_id, InstrumentId::test_default());
88        assert_eq!(event.client_order_id, ClientOrderId::test_default());
89        assert_eq!(event.account_id, AccountId::test_default());
90        assert_eq!(event.ts_event, UnixNanos::default());
91        assert_eq!(event.ts_init, UnixNanos::default());
92        assert_eq!(event.reconciliation, 0);
93        assert_eq!(event.venue_order_id, None);
94    }
95
96    #[rstest]
97    fn overrides_apply_through_constructor() {
98        let event = OrderPendingUpdateSpec::builder()
99            .venue_order_id(VenueOrderId::from("V-1"))
100            .reconciliation(true)
101            .build();
102
103        assert_eq!(event.venue_order_id, Some(VenueOrderId::from("V-1")));
104        // Production constructor stores the bool as a u8; assert against the encoded value.
105        assert_eq!(event.reconciliation, 1);
106        assert_eq!(event.trader_id, TraderId::test_default());
107    }
108
109    #[rstest]
110    fn event_ids_are_unique_within_a_run() {
111        reset_test_uuid_rng();
112        let a = OrderPendingUpdateSpec::builder().build();
113        let b = OrderPendingUpdateSpec::builder().build();
114        let c = OrderPendingUpdateSpec::builder().build();
115        assert_ne!(a.event_id, b.event_id);
116        assert_ne!(b.event_id, c.event_id);
117        assert_ne!(a.event_id, c.event_id);
118    }
119
120    #[rstest]
121    fn event_id_sequence_is_reproducible() {
122        // Reset before each draw so the comparison is run-order independent.
123        reset_test_uuid_rng();
124        let first_run: Vec<_> = (0..3)
125            .map(|_| OrderPendingUpdateSpec::builder().build().event_id)
126            .collect();
127
128        reset_test_uuid_rng();
129        let second_run: Vec<_> = (0..3)
130            .map(|_| OrderPendingUpdateSpec::builder().build().event_id)
131            .collect();
132
133        assert_eq!(first_run, second_run);
134    }
135}