[][src]Function cqrs_proptest::arb_aggregate

pub fn arb_aggregate<A, E, S>(events_strategy: S) -> impl Strategy<Value = A> where
    A: Aggregate + Debug,
    E: AggregateEvent<A> + Debug,
    S: Strategy<Value = Vec<E>>, 

Produces a strategy to generate an arbitrary aggregate, given a strategy to generate an arbitrary vector of events

Examples

use cqrs_core::{Aggregate, AggregateEvent, Event};
use cqrs_proptest::{arb_aggregate, arb_events};
use proptest::{prelude::*, strategy::{TupleUnion, ValueTree, W}, test_runner::TestRunner, prop_oneof};

#[derive(Debug, Default)]
struct MyAggregate {
    active: bool
}

impl Aggregate for MyAggregate {
    fn aggregate_type() -> &'static str {
        "my_aggregate"
    }
}

#[derive(Clone, Copy, Debug)]
struct CreatedEvent{};

impl Event for CreatedEvent {
    fn event_type(&self) -> &'static str {
        "created"
    }
}

impl AggregateEvent<MyAggregate> for CreatedEvent {
    fn apply_to(self, aggregate: &mut MyAggregate) {
        aggregate.active = true;
    }
}

#[derive(Clone, Copy, Debug)]
struct DeletedEvent{};

impl Event for DeletedEvent {
    fn event_type(&self) -> &'static str {
        "deleted"
    }
}

impl AggregateEvent<MyAggregate> for DeletedEvent {
    fn apply_to(self, aggregate: &mut MyAggregate) {
        aggregate.active = false;
    }
}

#[derive(Clone, Copy, Debug)]
enum MyEvents {
    Created(CreatedEvent),
    Deleted(DeletedEvent),
}

impl Event for MyEvents {
    fn event_type(&self) -> &'static str {
        match *self {
            MyEvents::Created(ref e) => e.event_type(),
            MyEvents::Deleted(ref e) => e.event_type(),
        }
    }
}

impl AggregateEvent<MyAggregate> for MyEvents {
    fn apply_to(self, aggregate: &mut MyAggregate) {
        match self {
            MyEvents::Created(e) => e.apply_to(aggregate),
            MyEvents::Deleted(e) => e.apply_to(aggregate),
        }
    }
}

impl Arbitrary for MyEvents {
    type Parameters = ();

    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        prop_oneof![
            Just(MyEvents::Created(CreatedEvent{})),
            Just(MyEvents::Deleted(DeletedEvent{})),
        ]
    }

    type Strategy = TupleUnion<(W<Just<Self>>, W<Just<Self>>)>;
}

arb_aggregate::<MyAggregate, MyEvents, _>(arb_events(any::<MyEvents>(), 0..10))
    .new_tree(&mut TestRunner::default())
    .unwrap()
    .current();