コード例 #1
0
        public void GetAll(MongoEventStore store, IEventStoreTransaction tx, List <EventBatch> exp, List <EventBatch> act)
        {
            MongoFake db = default;

            GIVEN["a configured store"] = () => store = CreateStore(
                new List <StreamConfiguration> {
                new StreamConfiguration(typeof(Customer), "customer"),
                new StreamConfiguration(typeof(Order), "order")
            },
                db = new MongoFake());

            Given["a transaction"] = async() => tx = store.UseTransaction(await db.StartTransactionAsync());


            WHEN["storing some streams"] = () => {
                exp = new List <EventBatch>();
                Guid customerID = Guid.NewGuid();

                Save(Order.CreateOrderWithProducts());
                Save(Customer.CreateCustomer(customerID));
                Save(Order.CreateOrderWithProducts());
                Save(CreateStream <Customer>(customerID, new Customer.Promoted()));

                void Save <T>(EventBatch <T> batch)
                {
                    tx.Save(batch).Wait();
                    exp.Add(batch);
                };
            };

            AND["calling GetAll"] = () => {
                db.BatchSize = 2;
                act          = tx.GetAll().Result.ToList().Result;
            };

            THEN["all stored events are returned in original order"] = () =>
                                                                       act.Should().BeEquivalentTo(exp, o => o.WithStrictOrdering());
        }
コード例 #2
0
        internal void Save(
            MongoFake db,
            MongoEventStore store,
            IEventStoreTransaction tx,
            PresetIDGenerator generator,
            EventBatch <Customer> s,
            BatchID batch
            )
        {
            GIVEN["a configured store"] = () => store = CreateStore(
                new List <StreamConfiguration> {
                new StreamConfiguration(typeof(Customer), "customer"),
                new StreamConfiguration(typeof(OrderProcessor), "order_processor")
            },
                db        = new MongoFake(),
                generator = new PresetIDGenerator()
                );

            Given["a transaction"] = async() => tx = store.UseTransaction(await db.StartTransactionAsync());

            Then["saving an unregistered stream type", ThrowsA <EventStoreConfigurationException>()] = () =>
                                                                                                       tx.Save(CreateStream <Order>(streamID: Guid.NewGuid()));

            GIVEN["a stream with some events"] = () => s = CreateStream <Customer>(
                streamID: Guid.NewGuid(),
                new Customer.Created(),
                new Customer.Relocated {
                OldAddress = "ADR 1", NewAddress = "ADR 2"
            }
                );

            When["saving the stream"] = () => {
                batch = new BatchID(DateTime.UtcNow);
                generator.Enqueue(batch);
                return(tx.Save(s));
            };

            THEN["an EventID is assigned to each event"] = () => {
                EventIDGenerator gen = new EventIDGenerator(batch);

                s.Events.Select(x => x.ID).Should()
                .AllBeOfType <EventID>().And
                .ContainInOrder(gen.Next(), gen.Next());
            };

            AND["the events are persisted properly"] = () =>
                                                       db.Log.Should().BeExactly(b => b
                                                                                 .Transaction(t => t
                                                                                              .InsertMany("Events", s.Events.ToArray()) // TODO: Better interface on Fake...
                                                                                              .Upsert("customer_Info", s.StreamID, new StreamInfo(s.StreamID))
                                                                                              )
                                                                                 );

            List <RecordedEvent> act = default;

            WHEN["getting the saved stream"]             = () =>
                                                     act = tx.Get(new StreamLocator <Customer>(s.StreamID)).Result.ToList().Result;

            THEN["it contains the original events"] = () =>
                                                      act.Should().BeEquivalentTo(s.Events);
        }
コード例 #3
0
        internal void SaveAndGet(
            MongoEventStore s,
            ITransaction tx,
            IEventStoreTransaction storeTx,
            List <RecordedEvent> expEvents,
            List <BsonDocument> actEvents,
            List <RecordedEvent> filteredEvents,
            Customer cust,
            Order firstOrder,
            Order secondOrder
            )
        {
            Given["a configured event store"] = async() => {
                ConfigureSerialization(new GuidSerializer());
                s = await CreateStore();
            };

            USING["a transaction"] = () => {
                tx      = DB.StartTransactionAsync().Result;
                storeTx = s.UseTransaction(tx);
                return(tx);
            };

            When["saving a new customer"] = () => Save(cust = Customer.CreateExampleCustomer());
            THEN["all events are properly serialized and stored"] = () => AssertCorrectlySaved(cust);

            When["saving an order"]              = () => Save(firstOrder = Order.CreateExampleOrder());
            And["saving a second order"]         = () => Save(secondOrder = Order.CreateExampleOrder());
            THEN["the order is properly stored"] = () => AssertCorrectlySaved(secondOrder);

            WHEN["changing the customer"] = () => {
                cust.ClearChanges();
                cust.AddEvent(new Customer.Promoted());
            };

            And["saving it"]     = () => Save(cust);
            THEN["it is stored"] = () => AssertCorrectlySaved(cust);

            Given["a new store instance"] = async() => s = await CreateStore();

            USING["a transaction"] = () => {
                tx      = DB.StartTransactionAsync().Result;
                storeTx = s.UseTransaction(tx);
                return(tx);
            };

            Given["a third order"] = () => Save(Order.CreateExampleOrder());

            WHEN["getting all events"] = () => actEvents = GetEvents();

            THEN["they are returned in chronological order"] = () =>
                                                               actEvents.Should().BeEquivalentTo(expEvents.Select(r => GetExpectedBson(r)), o => o.WithStrictOrdering());

            When["getting only certain events"] = async() => filteredEvents = await(await storeTx.Get(
                                                                                        s.CreateCriteria(c => {
                c.Stream(cust.ID);
                c.Stream(firstOrder.ID);
                c.Type <Customer.Created>();
                c.Type <Order.Created>();
            }))).ToList();

            THEN["Get only returns matching events in order"] = () => {
                RecordedEvent[] exp = expEvents.Where(e =>
                                                      (e.Event is Customer.Created || e.Event is Order.Created) &&
                                                      (e.StreamID.Equals(cust.ID) || e.StreamID.Equals(firstOrder.ID)))
                                      .ToArray();

                filteredEvents.Should().BeEquivalentTo(exp, o => o.WithStrictOrdering());
            };

            async Task Save <T>(Aggregate <T> x) where T : class