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
//! Module exposing a [Scenario] type to test [Aggregate]s using
//! the [given-then-when canvas](https://www.agilealliance.org/glossary/gwt/).
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::Arc;
use crate::aggregate::{Aggregate, Root};
use crate::event;
/// A test scenario that can be used to test an [Aggregate] and [Aggregate Root][Root]
/// using a [given-then-when canvas](https://www.agilealliance.org/glossary/gwt/) approach.
#[derive(Clone, Copy)]
pub struct Scenario<T>(PhantomData<T>)
where
T: Aggregate,
T::Id: Clone,
T::Event: Debug + PartialEq,
T::Error: Debug;
impl<T> Scenario<T>
where
T: Aggregate,
T::Id: Clone,
T::Event: Debug + PartialEq,
T::Error: Debug,
{
/// Creates a new [Scenario] instance.
#[must_use]
pub fn new() -> Self {
Self(PhantomData)
}
/// Specifies the precondition for the test [Scenario].
///
/// In other words, it can be used to specify all the Domain [Event][event::Envelope]s
/// that make up the state of the [Aggregate Root][Root].
#[must_use]
pub fn given(self, events: Vec<event::Envelope<T::Event>>) -> ScenarioGiven<T> {
ScenarioGiven {
events,
marker: PhantomData,
}
}
/// Specifies the action/mutation to execute in this [Scenario].
///
/// Use this branch when testing actions/mutations that create new [Aggregate Root][Root]
/// instances, i.e. with no prior Domain Events recorded.
#[must_use]
pub fn when<R, F, Err>(self, f: F) -> ScenarioWhen<T, R, F, Err>
where
R: From<Root<T>>,
F: Fn() -> Result<R, Err>,
{
ScenarioWhen {
mutate: f,
marker: PhantomData,
err_marker: PhantomData,
root_marker: PhantomData,
}
}
}
impl<T> Default for Scenario<T>
where
T: Aggregate,
T::Id: Clone,
T::Event: Debug + PartialEq,
T::Error: Debug,
{
fn default() -> Self {
Self::new()
}
}
#[doc(hidden)]
pub struct ScenarioGiven<T>
where
T: Aggregate,
T::Id: Clone,
T::Event: Debug + PartialEq,
T::Error: Debug,
{
events: Vec<event::Envelope<T::Event>>,
marker: PhantomData<T>,
}
impl<T> ScenarioGiven<T>
where
T: Aggregate,
T::Id: Clone,
T::Event: Debug + PartialEq,
T::Error: Debug,
{
/// Specifies the action/mutation to execute in this [Scenario].
///
/// Use this branch when testing actions/mutations that modify the state
/// of an [Aggregate Root][Root] that already exists, by specifying its
/// current state using [`Scenario::given`].
///
/// # Panics
///
/// Please note: as this method expects that an [Aggregate Root][Root] instance
/// is available when executing the domain method, it will panic if a `Root` instance
/// could not be obtained by rehydrating the [`Aggregate`] state through the events
/// provided in [`Scenario::given`].
#[must_use]
pub fn when<R, F, Err>(self, f: F) -> ScenarioWhen<T, R, impl Fn() -> Result<R, Err>, Err>
where
R: From<Root<T>>,
F: Fn(&mut R) -> Result<(), Err>,
{
let events = Arc::new(self.events);
ScenarioWhen {
marker: PhantomData,
err_marker: PhantomData,
root_marker: PhantomData,
mutate: move || -> Result<R, Err> {
let mut root: R = Root::<T>::rehydrate(events.iter().cloned())
.expect(
"no error is expected when applying domain events from a 'given' clause",
)
.expect("an aggregate root instance is expected, but none was produced")
.into();
match f(&mut root) {
Ok(()) => Ok(root),
Err(err) => Err(err),
}
},
}
}
}
#[doc(hidden)]
pub struct ScenarioWhen<T, R, F, Err>
where
T: Aggregate,
T::Event: Debug + PartialEq,
R: From<Root<T>>,
F: Fn() -> Result<R, Err>,
{
mutate: F,
marker: PhantomData<T>,
err_marker: PhantomData<Err>,
root_marker: PhantomData<R>,
}
impl<T, R, F, Err> ScenarioWhen<T, R, F, Err>
where
T: Aggregate,
T::Event: Debug + PartialEq,
R: From<Root<T>> + Deref<Target = Root<T>>,
F: Fn() -> Result<R, Err>,
{
/// Specifies that the outcome of the [Scenario] is positive, and
/// should result in the creation of the specified Domain Events.
#[must_use]
pub fn then(self, result: Vec<event::Envelope<T::Event>>) -> ScenarioThen<T, R, F, Err> {
ScenarioThen {
mutate: self.mutate,
expected: Ok(result),
marker: PhantomData,
}
}
/// Specified that the outcome of the [Scenario] is negative.
///
/// Use this method to assert the specific Error value that the
/// [Aggregate Root][Root] method should return.
#[must_use]
pub fn then_error(self, err: Err) -> ScenarioThen<T, R, F, Err> {
ScenarioThen {
mutate: self.mutate,
expected: Err(err),
marker: PhantomData,
}
}
}
#[doc(hidden)]
pub struct ScenarioThen<T, R, F, Err>
where
T: Aggregate,
T::Event: Debug + PartialEq,
R: From<Root<T>> + Deref<Target = Root<T>>,
F: Fn() -> Result<R, Err>,
{
mutate: F,
expected: Result<Vec<event::Envelope<T::Event>>, Err>,
marker: PhantomData<R>,
}
impl<T, R, F, Err> ScenarioThen<T, R, F, Err>
where
T: Aggregate,
T::Event: Debug + PartialEq,
R: From<Root<T>> + Deref<Target = Root<T>>,
F: Fn() -> Result<R, Err>,
Err: PartialEq + Debug,
{
/// Runs the [Scenario] and performs the various assertion for the test.
///
/// # Panics
///
/// This method will panic if the assertions have not passed, making
/// the test fail.
pub fn assert(self) {
let result = (self.mutate)().map(|root| root.recorded_events.clone());
assert_eq!(self.expected, result);
}
}