tsuzuri 0.1.285

Tsuzuri is a Event Sourcing framework for Rust, designed to be simple and easy to use.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//! Test framework for tsuzuri aggregates
//!
//! This module provides a fluent test framework for testing aggregates, commands, and events
//! using a Given-When-Then pattern similar to behavior-driven development (BDD).

use crate::aggregate::AggregateRoot;
use std::fmt::Debug;
use std::marker::PhantomData;

/// Test framework for testing aggregates with a Given-When-Then pattern
pub struct TestFramework<A: AggregateRoot> {
    aggregate: A,
    _phantom: PhantomData<A>,
}

impl<A: AggregateRoot> TestFramework<A> {
    /// Creates a test framework with a custom aggregate instance
    pub fn with(aggregate: A) -> Self {
        Self {
            aggregate,
            _phantom: PhantomData,
        }
    }
}

/// Given phase - setup initial state
impl<A: AggregateRoot> TestFramework<A> {
    /// Start with no previous events (clean state)
    pub fn given_no_previous_events(self) -> WhenPhase<A> {
        WhenPhase {
            aggregate: self.aggregate,
            initial_events: Vec::new(),
        }
    }

    /// Start with a set of previous events
    pub fn given(mut self, events: Vec<A::DomainEvent>) -> WhenPhase<A> {
        // Apply all events to aggregate to build up state
        for event in &events {
            self.aggregate.apply(event.clone());
        }

        WhenPhase {
            aggregate: self.aggregate,
            initial_events: events,
        }
    }

    /// Start with a single previous event
    pub fn given_event(self, event: A::DomainEvent) -> WhenPhase<A> {
        self.given(vec![event])
    }
}

/// When phase - execute command
pub struct WhenPhase<A: AggregateRoot> {
    aggregate: A,
    initial_events: Vec<A::DomainEvent>,
}

impl<A: AggregateRoot> WhenPhase<A> {
    /// Execute a command on the aggregate
    pub fn when(mut self, command: A::Command) -> ThenPhase<A> {
        let result = self.aggregate.handle(command);

        ThenPhase {
            aggregate: self.aggregate,
            initial_events: self.initial_events,
            result,
        }
    }
}

/// Then phase - verify outcomes
pub struct ThenPhase<A: AggregateRoot> {
    aggregate: A,
    #[allow(dead_code)]
    initial_events: Vec<A::DomainEvent>,
    result: Result<Vec<A::DomainEvent>, A::Error>,
}

impl<A: AggregateRoot> ThenPhase<A>
where
    A::DomainEvent: Debug + PartialEq,
    A::Error: Debug,
{
    /// Verify that the expected events were produced
    pub fn then_expect_events(self, expected_events: Vec<A::DomainEvent>) {
        match self.result {
            Ok(actual_events) => {
                assert_eq!(
                    actual_events, expected_events,
                    "Expected events do not match actual events.\nExpected: {expected_events:?}\nActual: {actual_events:?}"
                );
            }
            Err(e) => {
                panic!("Expected events but got error: {e:?}");
            }
        }
    }

    /// Verify that a single event was produced
    pub fn then_expect_event(self, expected_event: A::DomainEvent) {
        self.then_expect_events(vec![expected_event])
    }

    /// Verify that no events were produced
    pub fn then_expect_no_events(self) {
        self.then_expect_events(vec![])
    }

    /// Verify that an error was produced
    pub fn then_expect_error<E>(self) -> E
    where
        E: Debug,
        A::Error: Into<E>,
    {
        match self.result {
            Ok(events) => {
                panic!("Expected error but got events: {events:?}");
            }
            Err(error) => error.into(),
        }
    }

    /// Verify that a specific error matches
    pub fn then_expect_error_matches<F>(self, predicate: F)
    where
        F: FnOnce(&A::Error) -> bool,
    {
        match self.result {
            Ok(events) => {
                panic!("Expected error but got events: {events:?}");
            }
            Err(ref error) => {
                assert!(predicate(error), "Error does not match expected predicate: {error:?}");
            }
        }
    }

    /// Get the final aggregate state after command execution
    pub fn then_aggregate_state<F>(mut self, assertion: F)
    where
        F: FnOnce(&A),
    {
        // Apply resulting events if successful
        if let Ok(events) = &self.result {
            for event in events {
                self.aggregate.apply(event.clone());
            }
        }

        assertion(&self.aggregate);
    }

    /// Get access to the result for custom assertions
    pub fn then_verify<F>(self, verification: F)
    where
        F: FnOnce(Result<Vec<A::DomainEvent>, A::Error>),
    {
        verification(self.result);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        aggregate_id::{AggregateId, HasIdPrefix},
        command::Command,
        domain_event::DomainEvent,
        event_id::EventIdType,
        integration_event::IntegrationEvent,
        message::Message,
        AggregateRoot,
    };

    // Test ID type
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    struct TestId;

    impl HasIdPrefix for TestId {
        const PREFIX: &'static str = "test";
    }

    // Test aggregate for verifying the test framework
    #[derive(Debug, Clone, PartialEq)]
    struct TestAggregate {
        id: AggregateId<TestId>,
        value: i32,
        is_active: bool,
    }

    #[derive(Debug, Clone, PartialEq)]
    enum TestCommand {
        Create { id: AggregateId<TestId> },
        UpdateValue { value: i32 },
        Deactivate,
    }

    impl Message for TestCommand {
        fn name(&self) -> &'static str {
            match self {
                TestCommand::Create { .. } => "Create",
                TestCommand::UpdateValue { .. } => "UpdateValue",
                TestCommand::Deactivate => "Deactivate",
            }
        }
    }

    impl Command for TestCommand {
        type ID = TestId;

        fn id(&self) -> AggregateId<Self::ID> {
            match self {
                TestCommand::Create { id } => *id,
                TestCommand::UpdateValue { .. } => panic!("UpdateValue command requires aggregate to exist"),
                TestCommand::Deactivate => panic!("Deactivate command requires aggregate to exist"),
            }
        }
    }

    #[derive(Debug, Clone, PartialEq)]
    enum TestEvent {
        Created { id: AggregateId<TestId> },
        ValueUpdated { value: i32 },
        Deactivated,
    }

    impl Message for TestEvent {
        fn name(&self) -> &'static str {
            match self {
                TestEvent::Created { .. } => "Created",
                TestEvent::ValueUpdated { .. } => "ValueUpdated",
                TestEvent::Deactivated => "Deactivated",
            }
        }
    }

    impl DomainEvent for TestEvent {
        fn id(&self) -> EventIdType {
            EventIdType::new()
        }

        fn event_type(&self) -> &'static str {
            self.name()
        }
    }

    // Integration event for test
    #[allow(dead_code)]
    #[derive(Debug, Clone)]
    struct TestIntegrationEvent {
        #[allow(dead_code)]
        message: String,
    }

    impl Message for TestIntegrationEvent {
        fn name(&self) -> &'static str {
            "TestIntegrationEvent"
        }
    }

    impl IntegrationEvent for TestIntegrationEvent {
        fn id(&self) -> String {
            ulid::Ulid::new().to_string()
        }

        fn event_type(&self) -> &'static str {
            "TestIntegrationEvent"
        }
    }

    #[derive(Debug, thiserror::Error)]
    enum TestError {
        #[error("Already created")]
        AlreadyCreated,
        #[error("Not active")]
        NotActive,
        #[error("Invalid value")]
        InvalidValue,
    }

    impl AggregateRoot for TestAggregate {
        const TYPE: &'static str = "TestAggregate";
        type ID = TestId;
        type Command = TestCommand;
        type DomainEvent = TestEvent;
        type Error = TestError;

        fn init(id: AggregateId<Self::ID>) -> Self {
            Self {
                id,
                value: 0,
                is_active: false,
            }
        }

        fn id(&self) -> &AggregateId<Self::ID> {
            &self.id
        }

        fn handle(&mut self, command: Self::Command) -> Result<Vec<Self::DomainEvent>, Self::Error> {
            match command {
                TestCommand::Create { id } => {
                    if self.is_active {
                        return Err(TestError::AlreadyCreated);
                    }
                    Ok(vec![TestEvent::Created { id }])
                }
                TestCommand::UpdateValue { value } => {
                    if !self.is_active {
                        return Err(TestError::NotActive);
                    }
                    if value < 0 {
                        return Err(TestError::InvalidValue);
                    }
                    Ok(vec![TestEvent::ValueUpdated { value }])
                }
                TestCommand::Deactivate => {
                    if !self.is_active {
                        return Err(TestError::NotActive);
                    }
                    Ok(vec![TestEvent::Deactivated])
                }
            }
        }

        fn apply(&mut self, event: Self::DomainEvent) {
            match event {
                TestEvent::Created { id } => {
                    self.id = id;
                    self.is_active = true;
                }
                TestEvent::ValueUpdated { value } => {
                    self.value = value;
                }
                TestEvent::Deactivated => {
                    self.is_active = false;
                }
            }
        }
    }

    #[test]
    fn test_given_no_previous_events() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given_no_previous_events()
            .when(TestCommand::Create { id })
            .then_expect_event(TestEvent::Created { id });
    }

    #[test]
    fn test_given_with_events() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given(vec![TestEvent::Created { id }])
            .when(TestCommand::UpdateValue { value: 42 })
            .then_expect_event(TestEvent::ValueUpdated { value: 42 });
    }

    #[test]
    fn test_expect_error() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given_no_previous_events()
            .when(TestCommand::UpdateValue { value: 10 })
            .then_expect_error::<TestError>();
    }

    #[test]
    fn test_expect_error_matches() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given_no_previous_events()
            .when(TestCommand::UpdateValue { value: 10 })
            .then_expect_error_matches(|e| matches!(e, TestError::NotActive));
    }

    #[test]
    fn test_aggregate_state_assertion() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given(vec![TestEvent::Created { id }])
            .when(TestCommand::UpdateValue { value: 99 })
            .then_aggregate_state(|agg| {
                assert_eq!(*agg.id(), id);
                assert_eq!(agg.value, 99);
                assert!(agg.is_active);
            });
    }

    #[test]
    fn test_deactivate_already_inactive() {
        let id = AggregateId::<TestId>::new();
        let aggregate = TestAggregate::init(id);

        TestFramework::with(aggregate)
            .given(vec![TestEvent::Created { id }, TestEvent::Deactivated])
            .when(TestCommand::Deactivate)
            .then_expect_error_matches(|e| matches!(e, TestError::NotActive));
    }
}