Exemplo n.º 1
0
        public async Task Save(T @object)
        {
            Check.NotNull <ArgumentException>(@object.ID, "The 'ID' of 'object' cannot be null.");

            Changeset cs = @object.GetChanges();

            bool isNewInStore = !await _transaction.Exists(new StreamLocator <T>(@object.ID));

            if (cs.IsNew != isNewInStore)
            {
                throw new InvalidOperationException("An object with the same ID already exists!");
            }

            RecordedEvent[] changes = cs
                                      .Changes
                                      .Select(e => new RecordedEvent(@object.ID, e, _metadataProvider.Provide()))
                                      .ToArray();

            await _transaction.Save(new EventBatch <T>(@object.ID, changes));

            @object.ClearChanges();
        }
        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());
        }
        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);
        }