Beispiel #1
0
        public async Task OnFindAsync_WithVersion_BuildAggregateFromEventStoreAndSnapshotStore()
        {
            var fakeAggregate = new FakeAggregate(Guid.Empty);

            var fakeEvent1 = new FakeEvent(Guid.Empty, 1, "Version 1");
            var fakeEvent2 = new FakeEvent(Guid.Empty, 2, "Version 2");
            var fakeEvent3 = new FakeEvent(Guid.Empty, 3, "Version 3");

            fakeAggregate.Hydrate(new Event[] { fakeEvent1 });

            var mockEventStore    = new Mock <IEventStore>();
            var mockSnapshotStore = new Mock <ISnapshotStore>();

            mockSnapshotStore.Setup(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty, 2)).ReturnsAsync(fakeAggregate);
            mockEventStore.Setup(t => t.LoadByMaxVersionAsync <FakeAggregate>(Guid.Empty, 1, 2)).ReturnsAsync(new Event[] { fakeEvent2 });

            var sut = new Repository(
                mockEventStore.Object,
                mockSnapshotStore.Object,
                Mock.Of <IEventBus>(),
                Mock.Of <ILogger>()
                );


            var aggregate = await sut.GetByIdAsync <FakeAggregate>(Guid.Empty, 2);

            mockSnapshotStore.Verify(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty, 2));
            mockEventStore.Verify(t => t.LoadByMaxVersionAsync <FakeAggregate>(Guid.Empty, 1, 2));

            Assert.Same(fakeAggregate, aggregate);
            Assert.Equal(fakeEvent2.Value, fakeAggregate.Value);
        }
Beispiel #2
0
        public async Task OnFindAsync_BuildAggregateFromEventStoreAndSnapshotStore()
        {
            var fakeAggregate = new FakeAggregate(Guid.Empty);
            var fakeEvent     = new FakeEvent(Guid.Empty, 1, "Fake Value");

            var mockEventStore    = new Mock <IEventStore>();
            var mockSnapshotStore = new Mock <ISnapshotStore>();

            mockSnapshotStore.Setup(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty)).ReturnsAsync(fakeAggregate);
            mockEventStore.Setup(t => t.LoadAsync <FakeAggregate>(Guid.Empty, 0)).ReturnsAsync(Enumerable.Repeat(fakeEvent, 1));

            var sut = new Repository(
                mockEventStore.Object,
                mockSnapshotStore.Object,
                Mock.Of <IEventBus>(),
                Mock.Of <ILogger>()
                );

            var aggregate = await sut.GetByIdAsync <FakeAggregate>(Guid.Empty);

            mockSnapshotStore.Verify(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty));
            mockEventStore.Verify(t => t.LoadAsync <FakeAggregate>(Guid.Empty, 0));

            Assert.Same(fakeAggregate, aggregate);
            Assert.Equal(fakeEvent.Value, fakeAggregate.Value);
        }
Beispiel #3
0
        public void JsonAggregateSerializer_SerializesAggregateProperly()
        {
            var sut       = new JsonAggregateSerializer();
            var aggregate = new FakeAggregate(Guid.Empty);

            var bytes = sut.Serialize(aggregate);

            Assert.NotNull(bytes);
            Assert.NotEmpty(bytes);
        }
Beispiel #4
0
        public async Task OnFindAsync_BuildAggregateFromSnapshotStore()
        {
            var fakeAggregate     = new FakeAggregate(Guid.Empty);
            var mockSnapshotStore = new Mock <ISnapshotStore>();

            mockSnapshotStore.Setup(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty)).ReturnsAsync(fakeAggregate);

            var sut = new Repository(
                Mock.Of <IEventStore>(),
                mockSnapshotStore.Object,
                Mock.Of <IEventBus>(),
                Mock.Of <ILogger>()
                );

            var aggregate = await sut.GetByIdAsync <FakeAggregate>(Guid.Empty);

            mockSnapshotStore.Verify(t => t.GetByIdAsync <FakeAggregate>(Guid.Empty));

            Assert.Same(fakeAggregate, aggregate);
        }
Beispiel #5
0
        public async Task OnSaveChangesAsync_EventsPublishedToBus()
        {
            var fakeAggregate = new FakeAggregate(Guid.Empty);

            var mockBus = new Mock <IEventBus>();

            mockBus.Setup(t => t.PublishEventsAsync(It.IsAny <FakeEvent>())).Returns(Task.FromResult(0));

            var sut = new Repository(
                Mock.Of <IEventStore>(),
                Mock.Of <ISnapshotStore>(),
                mockBus.Object,
                Mock.Of <ILogger>()
                );

            fakeAggregate.Fire("Fake Value");

            sut.Save(fakeAggregate);

            await sut.SaveChangesAsync();

            mockBus.Verify(t => t.PublishEventsAsync(It.IsAny <FakeEvent>()));
        }
Beispiel #6
0
        public async Task OnSaveChangesAsync_EventsSentToEventStore()
        {
            var mockEventStore = new Mock <IEventStore>();
            var fakeAggregate  = new FakeAggregate(Guid.Empty);

            mockEventStore.Setup(t => t.Save(typeof(FakeAggregate), It.IsAny <FakeEvent>()));
            mockEventStore.Setup(t => t.SaveChangesAsync()).Returns(Task.FromResult(0));

            var sut = new Repository(
                mockEventStore.Object,
                Mock.Of <ISnapshotStore>(),
                Mock.Of <IEventBus>(),
                Mock.Of <ILogger>()
                );

            fakeAggregate.Fire("Fake Value");

            sut.Save(fakeAggregate);

            await sut.SaveChangesAsync();

            mockEventStore.Verify(t => t.Save(typeof(FakeAggregate), (It.IsAny <FakeEvent>())));
            mockEventStore.Verify(t => t.SaveChangesAsync());
        }