Exemplo n.º 1
0
        public async Task When_an_exception_is_thrown_during_a_read_model_update_then_it_is_logged_on_its_bus()
        {
            var projector = new Projector <Order.ItemAdded>(() => new ReadModelDbContext())
            {
                OnUpdate = (work, e) => { throw new Exception("oops!"); }
            };

            var itemAdded = new Order.ItemAdded
            {
                AggregateId    = Any.Guid(),
                SequenceNumber = 1,
                ProductName    = Any.AlphanumericString(10, 20),
                Price          = Any.Decimal(0.01m),
                Quantity       = 100
            };

            var errors = new List <Domain.EventHandlingError>();

            using (var catchup = CreateReadModelCatchup(projector))
                using (var db = new EventStoreDbContext())
                {
                    db.Events.Add(itemAdded.ToStorableEvent());
                    db.SaveChanges();
                    catchup.EventBus.Errors.Subscribe(errors.Add);
                    await catchup.Run();
                }

            var error = errors.Single(e => e.AggregateId == itemAdded.AggregateId);

            error.SequenceNumber.Should().Be(itemAdded.SequenceNumber);
            error.Event
            .ShouldHave()
            .Properties(p => p.SequenceNumber)
            .EqualTo(itemAdded);
        }
Exemplo n.º 2
0
        public override async Task Events_at_the_end_of_the_sequence_that_cannot_be_deserialized_due_to_unknown_type_do_not_cause_Version_to_be_incorrect()
        {
            var orderId = Guid.NewGuid();
            var events  = new List <StorableEvent>
            {
                new Order.CustomerInfoChanged
                {
                    CustomerName = "Waylon Jennings",
                    AggregateId  = orderId
                }.ToStorableEvent(),
                new StorableEvent
                {
                    StreamName = "Order",
                    Type       = "UKNOWN",
                    Body       = new { ShoeSize = 10.5 }.ToJson(),
                SequenceNumber = 2,
                AggregateId    = orderId,
                UtcTime        = DateTime.UtcNow
                }
            };

            using (var db = new EventStoreDbContext())
            {
                db.Events.AddOrUpdate(events.ToArray());
                db.SaveChanges();
            }

            var repository = CreateRepository <Order>();

            var order = await repository.GetLatest(orderId);

            order.Version.Should().Be(2);
        }
Exemplo n.º 3
0
        private static void AddSubscriptions(EventStoreDbContext context)
        {
            GetSubscriptions().ForEach(s => context.Subscriptions.Add(s));
            context.SaveChanges();

            Console.WriteLine("Susbscriptions added!");
        }
Exemplo n.º 4
0
        public void A_dynamic_projector_can_access_the_properties_of_unknown_event_types_dynamically()
        {
            using (var db = new EventStoreDbContext())
            {
                Enumerable.Range(1, 10).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = Guid.NewGuid(),
                    StreamName  = Any.CamelCaseName(),
                    Type        = Any.CamelCaseName(),
                    Body        = new { SomeValue = i }.ToJson()
                }));
                db.SaveChanges();
            }
            var total = 0;

            using (var catchup = CreateReadModelCatchup(Projector.CreateDynamic(e =>
            {
                total += (int)e.SomeValue;
            })))
            {
                catchup.Run();
            }

            total.Should().Be(55);
        }
Exemplo n.º 5
0
        public override void Events_that_cannot_be_deserialized_due_to_unknown_type_do_not_cause_sourcing_to_fail()
        {
            Guid orderId = Guid.NewGuid();
            var  events  = new List <StorableEvent>
            {
                new StorableEvent
                {
                    StreamName = "Order",
                    Type       = "UKNOWN",
                    Body       = new { ShoeSize = 10.5 }.ToJson(),
                 SequenceNumber = 1,
                 AggregateId    = orderId,
                 UtcTime        = DateTime.UtcNow
                },
                new Order.CustomerInfoChanged
                {
                    CustomerName = "Waylon Jennings",
                    AggregateId  = orderId
                }.ToStorableEvent()
            };

            using (var db = new EventStoreDbContext())
            {
                db.Events.AddOrUpdate(events.ToArray());
                db.SaveChanges();
            }

            var repository = CreateRepository <Order>();

            var order = repository.GetLatest(orderId);

            order.CustomerName.Should().Be("Waylon Jennings");
        }
Exemplo n.º 6
0
        public void Timestamp_round_trips_correctly_to_database()
        {
            var id  = Guid.NewGuid();
            var now = Clock.Now();

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new TestEvent
                {
                    AggregateId = id,
                    Timestamp   = now
                }.ToStorableEvent());

                db.SaveChanges();
            }

            using (var db = new EventStoreDbContext())
            {
                var @event = db.Events.Single(e => e.AggregateId == id).ToDomainEvent();

                // the database is not saving the offset, but the two dates should be equivalent UTC times
                @event.Timestamp
                .UtcDateTime
                .Should()
                .BeCloseTo(now.UtcDateTime,
                           // there's a slight loss of precision saving to the db, but we should be within 3ms
                           precision: 3
                           );
            }
        }
Exemplo n.º 7
0
 void TreatBufferEvents(object state)
 {
     s_Lock.Wait();
     {
         try
         {
             if (s_Events.Count > 0)
             {
                 using (var innerCtx = new EventStoreDbContext(_dbContextOptions, _archiveBehavior))
                 {
                     innerCtx.AddRange(s_Events);
                     s_Events = new ConcurrentBag <Event>();
                     innerCtx.SaveChanges();
                 }
             }
         }
         finally
         {
             s_BufferEnteredTimeAbsolute = null;
             s_BufferEnteredTimeSliding  = null;
             s_AbsoluteTimer.Change(Timeout.Infinite, Timeout.Infinite);
             s_SlidingTimer.Change(Timeout.Infinite, Timeout.Infinite);
             s_Lock.Release();
         }
     }
 }
Exemplo n.º 8
0
        public async Task RelatedEvents_handles_circular_references()
        {
            var relatedId1 = Any.Guid();
            var relatedId2 = Any.Guid();
            var relatedId3 = Any.Guid();

            Console.WriteLine(new
            {
                relatedId1,
                relatedId2,
                relatedId3,
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId1,
                    SequenceNumber = 1,
                    Body           = new { relatedId2 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "one",
                    Type           = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId2,
                    SequenceNumber = 1,
                    Body           = new { relatedId3 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "two",
                    Type           = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId3,
                    SequenceNumber = 1,
                    Body           = new { relatedId1 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event"
                });

                db.SaveChanges();
            }

            // assert
            using (var db = new EventStoreDbContext())
            {
                var events = (await db.Events.RelatedEvents(relatedId1)).ToArray();

                events.Count().Should().Be(3);
                events.Should().Contain(e => e.AggregateId == relatedId1);
                events.Should().Contain(e => e.AggregateId == relatedId2);
                events.Should().Contain(e => e.AggregateId == relatedId3);
            }
        }
 private void CleanDatabases()
 {
     using (var ctx = new EventStoreDbContext(GetDbOptions()))
     {
         ctx.RemoveRange(ctx.Set <Event>());
         ctx.RemoveRange(ctx.Set <Snapshot>());
         ctx.SaveChanges();
     }
 }
Exemplo n.º 10
0
 protected override void SaveEventsDirectly(params object[] events)
 {
     using (var db = new EventStoreDbContext())
     {
         events
         .Select(e => e.IfTypeIs <StorableEvent>()
                 .Else(() => e.IfTypeIs <IEvent>()
                       .Then(ee => ee.ToStorableEvent()))
                 .ElseDefault())
         .ForEach(e => { db.Events.Add(e); });
         db.SaveChanges();
     }
 }
        private StoredEvent SaveIntegrationEventLogContext(IDomainEvent domainEvent)
        {
            var eventLogEntry = FromDomainEvent(domainEvent);

            eventLogEntry.Version++;
            eventLogEntry.State = EventStateEnum.Published;

            _eventStoreDbContext.Events.Add(eventLogEntry);

            int result = _eventStoreDbContext.SaveChanges();

            return(new StoredEvent(domainEvent.GetType().FullName, domainEvent.TimeStamp.DateTime, JsonConvert.SerializeObject(domainEvent)));
        }
Exemplo n.º 12
0
        public static TAggregate SaveToEventStore <TAggregate>(this TAggregate aggregate) where TAggregate : EventSourcedAggregate
        {
            using (var db = new EventStoreDbContext())
            {
                foreach (var e in aggregate.EventHistory.OfType <IEvent <TAggregate> >())
                {
                    var storableEvent = e.ToStorableEvent();
                    db.Events.Add(storableEvent);
                }
                db.SaveChanges();
            }

            return(aggregate);
        }
Exemplo n.º 13
0
        public void Duck_projectors_can_access_the_event_properties_of_received_events()
        {
            var expectedTimestamp       = DateTimeOffset.Parse("2014-07-03");
            var expectedAggregateId     = Any.Guid();
            var receivedAggregateId     = new Guid();
            var expectedSequenceNumber  = Any.PositiveInt();
            int receivedSequenceNumber  = 0;
            var receivedTimestamp       = new DateTimeOffset();
            var receivedEventStreamName = "";
            var receivedEventTypeName   = "";

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new StorableEvent
                {
                    AggregateId    = expectedAggregateId,
                    SequenceNumber = expectedSequenceNumber,
                    Timestamp      = expectedTimestamp,
                    StreamName     = "Reporting",
                    Type           = "DuckEvent",
                    Body           = new { Quacks = 9000 }.ToJson()
                });

                db.SaveChanges();
            }

            var projector = Projector.CreateFor <Reporting.DuckEvent>(e =>
            {
                receivedAggregateId     = e.AggregateId;
                receivedSequenceNumber  = e.SequenceNumber;
                receivedTimestamp       = e.Timestamp;
                receivedEventStreamName = e.EventStreamName;
                receivedEventTypeName   = e.EventTypeName;
            });

            using (var catchup = CreateReadModelCatchup(projector))
            {
                catchup.Run();
            }

            receivedAggregateId.Should().Be(expectedAggregateId);
            receivedSequenceNumber.Should().Be(expectedSequenceNumber);
            receivedTimestamp.Should().Be(expectedTimestamp);
            receivedEventStreamName.Should().Be("Reporting");
            receivedEventTypeName.Should().Be("DuckEvent");
        }
Exemplo n.º 14
0
        protected override void Dispose(bool disposing)
        {
            var options = new DbContextOptionsBuilder()
                          .UseSqlServer(_configuration[DataDefaultConnectionString])
                          .Options;

            var context = new EventStoreDbContext(options);

            foreach (var storedEvent in context.StoredEvents.Where(x => x.CorrelationId == _correlationId))
            {
                context.Remove(storedEvent);
            }

            context.SaveChanges();

            base.Dispose(disposing);
        }
Exemplo n.º 15
0
        public void When_classes_not_implementing_IEvent_are_used_to_query_the_event_store_then_nested_classes_can_be_used_to_specify_stream_names()
        {
            using (var db = new EventStoreDbContext())
            {
                Enumerable.Range(1, 5).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = Guid.NewGuid(),
                    StreamName  = "Reporting",
                    Type        = "DuckEvent",
                    Body        = new { Quacks = i }.ToJson()
                }));

                Enumerable.Range(1, 5).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = Guid.NewGuid(),
                    StreamName  = Any.CamelCaseName(),
                    Type        = "DuckEvent",
                    Body        = new { Quacks = i }.ToJson()
                }));

                db.SaveChanges();
            }
            var unNestedDucks = 0;
            var nestedDucks   = 0;

            var projector1 = Projector.CreateFor <DuckEvent>(e =>
            {
                unNestedDucks++;
            });
            var projector2 = Projector.CreateFor <Reporting.DuckEvent>(e =>
            {
                nestedDucks++;
            });

            using (var catchup = CreateReadModelCatchup(projector1, projector2))
            {
                catchup.Run();
            }

            nestedDucks.Should().Be(5);
            unNestedDucks.Should().Be(10);
        }
Exemplo n.º 16
0
 protected override async Task SaveEventsDirectly(params IStoredEvent[] events)
 {
     using (var db = new EventStoreDbContext())
     {
         foreach (var e in events)
         {
             db.Events.Add(new StorableEvent
             {
                 AggregateId    = Guid.Parse(e.AggregateId),
                 Body           = e.Body,
                 ETag           = e.ETag,
                 SequenceNumber = e.SequenceNumber,
                 StreamName     = e.StreamName,
                 Timestamp      = e.Timestamp,
                 Type           = e.Type,
                 UtcTime        = e.Timestamp.UtcDateTime
             });
         }
         db.SaveChanges();
     }
 }
Exemplo n.º 17
0
        public void Events_that_cannot_be_deserialized_to_the_expected_type_are_logged_as_EventHandlingErrors()
        {
            var badEvent = new StorableEvent
            {
                Actor      = Any.Email(),
                StreamName = typeof(Order).Name,
                Type       = typeof(Order.ItemAdded).Name,
                Body       = new { Price = "oops this is not a number" }.ToJson(),
                SequenceNumber = Any.PositiveInt(),
                AggregateId    = Any.Guid(),
                UtcTime        = DateTime.UtcNow
            };

            using (var eventStore = new EventStoreDbContext())
            {
                eventStore.Events.Add(badEvent);
                eventStore.SaveChanges();
            }

            using (var catchup = CreateReadModelCatchup(new Projector1()))
            {
                catchup.Run();
            }

            using (var readModels = new ReadModelDbContext())
            {
                var failure = readModels.Set <EventHandlingError>()
                              .OrderByDescending(e => e.Id)
                              .First(e => e.AggregateId == badEvent.AggregateId);
                failure.Error.Should().Contain("JsonReaderException");
                failure.SerializedEvent.Should().Contain(badEvent.Body);
                failure.Actor.Should().Be(badEvent.Actor);
                failure.OriginalId.Should().Be(badEvent.Id);
                failure.AggregateId.Should().Be(badEvent.AggregateId);
                failure.SequenceNumber.Should().Be(badEvent.SequenceNumber);
                failure.StreamName.Should().Be(badEvent.StreamName);
                failure.EventTypeName.Should().Be(badEvent.Type);
            }
        }
Exemplo n.º 18
0
        public async Task RelatedEvents_returns_all_events_in_related_aggregates()
        {
            // arrange
            var relatedId1 = Any.Guid();
            var relatedId2 = Any.Guid();
            var relatedId3 = Any.Guid();
            var relatedId4 = Any.Guid();
            var unrelatedId = Any.Guid();

            Console.WriteLine(new
            {
                relatedId1,
                relatedId2,
                relatedId3,
                relatedId4,
                unrelatedId
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId1,
                    SequenceNumber = i,
                    Body = new { relatedId2 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "one",
                    Type = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId2,
                    SequenceNumber = i,
                    Body = new { relatedId3 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "two",
                    Type = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId3,
                    SequenceNumber = i,
                    Body = new { relatedId4 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "three",
                    Type = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId4,
                    SequenceNumber = i,
                    Body = new { }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "three",
                    Type = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId = unrelatedId,
                    SequenceNumber = i,
                    Body = new { }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "three",
                    Type = "Event" + i.ToString()
                }));

                db.SaveChanges();
            }

            using (var db = new EventStoreDbContext())
            {
                //act
                var events = (await db.Events.RelatedEvents(relatedId1)).ToArray();

                // assert
                events.Count().Should().Be(80);
                events.Should().Contain(e => e.AggregateId == relatedId1);
                events.Should().Contain(e => e.AggregateId == relatedId2);
                events.Should().Contain(e => e.AggregateId == relatedId3);
                events.Should().Contain(e => e.AggregateId == relatedId4);
                events.Should().NotContain(e => e.AggregateId == unrelatedId);
            }
        }
Exemplo n.º 19
0
        public async Task RelatedEvents_can_return_several_nonintersecting_graphs_at_once()
        {
            var graph1Id1 = Any.Guid();
            var graph1Id2 = Any.Guid();
            var graph2Id1 = Any.Guid();
            var graph2Id2 = Any.Guid();

            Console.WriteLine(new
            {
                graph1Id1,
                graph1Id2,
                graph2Id1,
                graph2Id2
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new StorableEvent
                {
                    AggregateId = graph1Id1,
                    SequenceNumber = 1,
                    Body = new { graph1Id2 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "one",
                    Type = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId = graph1Id2,
                    SequenceNumber = 1,
                    Body = new { }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "one",
                    Type = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId = graph2Id1,
                    SequenceNumber = 1,
                    Body = new { graph2Id2 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "two",
                    Type = "Event"
                });
                db.Events.Add(new StorableEvent
                {
                    AggregateId = graph2Id2,
                    SequenceNumber = 1,
                    Body = new { }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "two",
                    Type = "Event"
                });

                db.SaveChanges();
            }

            // assert
            using (var db = new EventStoreDbContext())
            {
                var events = (await db.Events.RelatedEvents(graph1Id1, graph2Id1)).ToArray();

                events.Count().Should().Be(4);
                events.Should().Contain(e => e.AggregateId == graph1Id1);
                events.Should().Contain(e => e.AggregateId == graph1Id2);
                events.Should().Contain(e => e.AggregateId == graph2Id1);
                events.Should().Contain(e => e.AggregateId == graph2Id2);
            }
        }
Exemplo n.º 20
0
        public async Task RelatedEvents_handles_circular_references()
        {
            var relatedId1 = Any.Guid();
            var relatedId2 = Any.Guid();
            var relatedId3 = Any.Guid();
            Console.WriteLine(new
            {
                relatedId1,
                relatedId2,
                relatedId3,
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId1,
                    SequenceNumber = 1,
                    Body = new { relatedId2 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "one",
                    Type = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId2,
                    SequenceNumber = 1,
                    Body = new { relatedId3 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "two",
                    Type = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId = relatedId3,
                    SequenceNumber = 1,
                    Body = new { relatedId1 }.ToJson(),
                    Timestamp = Clock.Now(),
                    StreamName = "three",
                    Type = "Event"
                });

                db.SaveChanges();
            }

            // assert
            using (var db = new EventStoreDbContext())
            {
                var events = (await db.Events.RelatedEvents(relatedId1)).ToArray();

                events.Count().Should().Be(3);
                events.Should().Contain(e => e.AggregateId == relatedId1);
                events.Should().Contain(e => e.AggregateId == relatedId2);
                events.Should().Contain(e => e.AggregateId == relatedId3);
            }
        }
Exemplo n.º 21
0
        static public void Seed(EventStoreDbContext context)
        {
            var seedRevision = new Revision()
            {
                AggregateId             = new Guid("19e2a7fc-d0eb-44b9-8348-e7678ccd37bc"),
                AggregateType           = "Test aggregate 1",
                CausationId             = "Test command",
                Changes                 = Encoding.UTF8.GetBytes("These are some fake events."),
                ChangesContentEncoding  = Encoding.UTF8.HeaderName,
                ChangesContentType      = "text/plain",
                CorrelationId           = "Test message",
                Metadata                = Encoding.UTF8.GetBytes("This is some fake metadata."),
                MetadataContentEncoding = Encoding.UTF8.HeaderName,
                MetadataContentType     = "text/plain",
                OccurrenceDate          = DateTime.UtcNow,
                RevisionId              = 0,
                CommitId                = default(long)
            };

            // Aggreggate 1
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 1;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 2;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 3;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 4;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 5;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 6;
            context.StoredRevisions.Add(seedRevision);

            // Aggregate 2
            seedRevision               = CreateCopy(seedRevision);
            seedRevision.AggregateId   = new Guid("f552b2d2-4b30-4447-b187-14cc8b572379");
            seedRevision.AggregateType = "Test aggregate 2";
            seedRevision.RevisionId    = 0;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 1;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 2;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 3;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 4;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 5;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 6;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 7;
            context.StoredRevisions.Add(seedRevision);

            // Aggregate 3
            seedRevision               = CreateCopy(seedRevision);
            seedRevision.AggregateId   = new Guid("1f02431f-5716-4189-bd5b-529ed837503c");
            seedRevision.AggregateType = "Test aggregate 3";
            seedRevision.RevisionId    = 0;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 1;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 2;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 3;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 4;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 5;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 6;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 7;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 8;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 9;
            context.StoredRevisions.Add(seedRevision);

            seedRevision            = CreateCopy(seedRevision);
            seedRevision.RevisionId = 10;
            context.StoredRevisions.Add(seedRevision);

            // Aggregate 4
            seedRevision               = CreateCopy(seedRevision);
            seedRevision.AggregateId   = new Guid("cc09d9d1-62fa-432c-aa21-98b70eb4cff1");
            seedRevision.AggregateType = "Test aggregate 4";
            seedRevision.RevisionId    = 0;
            context.StoredRevisions.Add(seedRevision);

            context.SaveChanges();
        }
Exemplo n.º 22
0
        public async Task RelatedEvents_returns_all_events_in_related_aggregates()
        {
            // arrange
            var relatedId1  = Any.Guid();
            var relatedId2  = Any.Guid();
            var relatedId3  = Any.Guid();
            var relatedId4  = Any.Guid();
            var unrelatedId = Any.Guid();

            Console.WriteLine(new
            {
                relatedId1,
                relatedId2,
                relatedId3,
                relatedId4,
                unrelatedId
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId1,
                    SequenceNumber = i,
                    Body           = new { relatedId2 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "one",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId2,
                    SequenceNumber = i,
                    Body           = new { relatedId3 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "two",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId3,
                    SequenceNumber = i,
                    Body           = new { relatedId4 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId4,
                    SequenceNumber = i,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = unrelatedId,
                    SequenceNumber = i,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                db.SaveChanges();
            }

            using (var db = new EventStoreDbContext())
            {
                //act
                var events = (await db.Events.RelatedEvents(relatedId1)).ToArray();

                // assert
                events.Count().Should().Be(80);
                events.Should().Contain(e => e.AggregateId == relatedId1);
                events.Should().Contain(e => e.AggregateId == relatedId2);
                events.Should().Contain(e => e.AggregateId == relatedId3);
                events.Should().Contain(e => e.AggregateId == relatedId4);
                events.Should().NotContain(e => e.AggregateId == unrelatedId);
            }
        }
Exemplo n.º 23
0
        public async Task RelatedEvents_can_return_several_nonintersecting_graphs_at_once()
        {
            var graph1Id1 = Any.Guid();
            var graph1Id2 = Any.Guid();
            var graph2Id1 = Any.Guid();
            var graph2Id2 = Any.Guid();

            Console.WriteLine(new
            {
                graph1Id1,
                graph1Id2,
                graph2Id1,
                graph2Id2
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                db.Events.Add(new StorableEvent
                {
                    AggregateId    = graph1Id1,
                    SequenceNumber = 1,
                    Body           = new { graph1Id2 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "one",
                    Type           = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId    = graph1Id2,
                    SequenceNumber = 1,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "one",
                    Type           = "Event"
                });

                db.Events.Add(new StorableEvent
                {
                    AggregateId    = graph2Id1,
                    SequenceNumber = 1,
                    Body           = new { graph2Id2 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "two",
                    Type           = "Event"
                });
                db.Events.Add(new StorableEvent
                {
                    AggregateId    = graph2Id2,
                    SequenceNumber = 1,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "two",
                    Type           = "Event"
                });

                db.SaveChanges();
            }

            // assert
            using (var db = new EventStoreDbContext())
            {
                var events = (await db.Events.RelatedEvents(graph1Id1, graph2Id1)).ToArray();

                events.Count().Should().Be(4);
                events.Should().Contain(e => e.AggregateId == graph1Id1);
                events.Should().Contain(e => e.AggregateId == graph1Id2);
                events.Should().Contain(e => e.AggregateId == graph2Id1);
                events.Should().Contain(e => e.AggregateId == graph2Id2);
            }
        }
Exemplo n.º 24
0
        public void Related_events_are_available_via_diagnostics_endpoint()
        {
            // arrange
            var relatedId1  = Any.Guid();
            var relatedId2  = Any.Guid();
            var relatedId3  = Any.Guid();
            var relatedId4  = Any.Guid();
            var unrelatedId = Any.Guid();

            Console.WriteLine(new
            {
                relatedId1,
                relatedId2,
                relatedId3,
                relatedId4,
                unrelatedId
            }.ToLogString());

            using (var db = new EventStoreDbContext())
            {
                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId1,
                    SequenceNumber = i,
                    Body           = new { relatedId2 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "one",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId2,
                    SequenceNumber = i,
                    Body           = new { relatedId3 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "two",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId3,
                    SequenceNumber = i,
                    Body           = new { relatedId4 }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = relatedId4,
                    SequenceNumber = i,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                Enumerable.Range(1, 20).ForEach(i => db.Events.Add(new StorableEvent
                {
                    AggregateId    = unrelatedId,
                    SequenceNumber = i,
                    Body           = new { }.ToJson(),
                    Timestamp      = Clock.Now(),
                    StreamName     = "three",
                    Type           = "Event" + i.ToString()
                }));

                db.SaveChanges();
            }

            var client = CreateClient();

            // act
            var response = client.GetAsync("http://contoso.com/api/events/related/" + relatedId1)
                           .Result
                           .ShouldSucceed()
                           .JsonContent();

            Console.WriteLine(response);

            // assert
            //            events.Count().Should().Be(80);
            //            events.Should().Contain(e => e.AggregateId == relatedId1);
            //            events.Should().Contain(e => e.AggregateId == relatedId2);
            //            events.Should().Contain(e => e.AggregateId == relatedId3);
            //            events.Should().Contain(e => e.AggregateId == relatedId4);
            //            events.Should().NotContain(e => e.AggregateId == unrelatedId);

            // TODO: (Related_events_are_available_via_diagnostics_endpoint) write test
            Assert.Fail("Test not written yet.");
        }
Exemplo n.º 25
0
        static public void Seed(EventStoreDbContext dbContext)
        {
            dbContext.Database.EnsureDeleted();
            dbContext.Database.EnsureCreated();

            // Aggreggate 1
            string aggregate01Id = "19e2a7fc-d0eb-44b9-8348-e7678ccd37bc";
            var    seedRevision  = GetSeedRevision(aggregate01Id, "Test aggregate 1", 0);

            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 1);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 2);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 3);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 4);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 5);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate01Id, "Test aggregate 1", 6);
            dbContext.StoredRevisions.Add(seedRevision);

            // Aggregate 2
            string aggregate02Id = "f552b2d2-4b30-4447-b187-14cc8b572379";

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 0);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 1);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 2);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 3);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 4);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 5);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 6);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate02Id, "Test aggregate 2", 7);
            dbContext.StoredRevisions.Add(seedRevision);

            // Aggregate 3
            string aggregate03Id = "1f02431f-5716-4189-bd5b-529ed837503c";

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 0);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 1);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 2);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 3);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 4);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 5);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 6);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 7);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 8);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 9);
            dbContext.StoredRevisions.Add(seedRevision);

            seedRevision = GetSeedRevision(aggregate03Id, "Test aggregate 3", 10);
            dbContext.StoredRevisions.Add(seedRevision);

            // Aggregate 4
            string aggregate04Id = "cc09d9d1-62fa-432c-aa21-98b70eb4cff1";

            seedRevision = GetSeedRevision(aggregate04Id, "Test aggregate 4", 0);
            dbContext.StoredRevisions.Add(seedRevision);

            dbContext.SaveChanges();
        }