public async Task IntegrationWithRepo() { BsonMapRegistrationHelpers.AddBsonMapFor <TestEventEventStore>(); var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var entityId = Guid.NewGuid(); var eventStore = new EventStore( new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)), snapShotRepo.Object, new SnapShotConfig()); await eventStore.AppendAsync(new List <IDomainEvent> { new TestEventEventStore(entityId, "Test") }, 0); var loadAsync = await eventStore.LoadAsync <TestEntity>(entityId.ToString()); var loadAsync2 = await eventStore.LoadAsync <TestEntity>(entityId.ToString()); Assert.AreEqual(entityId, loadAsync.Value.Id); Assert.AreEqual("Test", loadAsync.Value.Name); Assert.AreEqual(entityId, loadAsync2.Value.Id); Assert.AreEqual("Test", loadAsync2.Value.Name); }
public async Task AddEvents_ForeachMethod() { BsonMapRegistrationHelpers.AddBsonMapFor <TestEventAllOk>(); var eventRepository = new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)); var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var eventStore = new EventStore(eventRepository, snapShotRepo.Object, new SnapShotConfig()); var newGuid = Guid.NewGuid(); await eventStore.AppendAsync( new List <IDomainEvent> { new TestEventAllOk(newGuid, "Simon") }, 0); var result = await eventRepository.LoadEvents(); Assert.AreEqual(1, result.Value.Count()); Assert.AreEqual(newGuid.ToString(), result.Value.Single().DomainEvent.EntityId); Assert.AreEqual("Simon", ((TestEventAllOk)result.Value.Single().DomainEvent).Name); }
public async Task AddEvents_AutoProperty_NotNamedEntityId() { BsonMapRegistrationHelpers.AddBsonMapFor <TestEvent_BsonBug_AutoProperty_NotWithEntityIdName>(); var eventRepository = new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)); var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var eventStore = new EventStore(eventRepository, snapShotRepo.Object, new SnapShotConfig()); await eventStore.AppendAsync(new List <IDomainEvent> { new TestEvent_BsonBug_AutoProperty_NotWithEntityIdName("whatever", "Peter") }, 0); var result = await eventRepository.LoadEvents(); Assert.AreEqual(1, result.Value.Count()); var domainEvent = result.Value.Single().DomainEvent as TestEvent_BsonBug_AutoProperty_NotWithEntityIdName; Assert.AreEqual("whatever", domainEvent.EntityId); Assert.AreEqual("Peter", domainEvent.Name); }
public Task <SnapShotResult <T> > LoadSnapShot <T>(string entityId) where T : new() { if (entityId == null) { return(Task.FromResult(SnapShotResult <T> .NotFound(null))); } if (!_snapShots.TryGetValue(entityId, out var entity)) { return(Task.FromResult(SnapShotResult <T> .Default())); } var snapShotWrapper = (SnapShotWrapper <T>)entity; return(Task.FromResult(new SnapShotResult <T>(snapShotWrapper.Entity, snapShotWrapper.Version))); }
public async Task NotFoundExceptionIsWithCorrectT() { var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var entityId = Guid.NewGuid(); var eventStore = new EventStore(new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)), snapShotRepo.Object, new SnapShotConfig()); var result = await eventStore.LoadAsync <TestEntity>(entityId.ToString()); var exception = Assert.ThrowsException <NotFoundException>(() => result.Value); Assert.IsTrue(exception.Message.StartsWith("Could not find TestEntity")); }
public async Task IntegrationWithRepo_NotFound() { var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var entityId = Guid.NewGuid(); var eventStore = new EventStore(new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)), snapShotRepo.Object, new SnapShotConfig()); await eventStore.AppendAsync(new List <IDomainEvent> { new TestEventEventStore(entityId, "Test") }, 0); var loadAsync = await eventStore.LoadAsync <TestEntity>(Guid.NewGuid().ToString()); Assert.IsTrue(loadAsync.Is <NotFound>()); }
public async Task DifferentIdsInEventsDefined() { var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity> .Default()); var entityId = Guid.NewGuid(); var entityId2 = Guid.NewGuid(); var eventStore = new EventStore(new EventRepositoryMongoDb(EventMongoDb, new VersionCache(EventMongoDb)), snapShotRepo.Object, new SnapShotConfig()); await Assert.ThrowsExceptionAsync <DifferentIdsException>(async() => await eventStore.AppendAsync(new List <IDomainEvent> { new TestEventEventStore(entityId, "Test"), new TestEventEventStore(entityId2, "Test") }, 0)); }
public async Task <SnapShotResult <T> > LoadSnapShot <T>(string entityId) where T : new() { if (entityId == null) { return(SnapShotResult <T> .NotFound(null)); } var mongoCollection = _context.GetCollection <SnapShotDbo <T> >(_snapShotCollectionName); var asyncCursor = await mongoCollection.FindAsync(r => r.Id == entityId); var snapShot = asyncCursor.ToList().FirstOrDefault(); if (snapShot == null) { return(SnapShotResult <T> .Default()); } return(new SnapShotResult <T>(snapShot.Payload, snapShot.Version)); }
public async Task ApplyMethod_WrongIfDeclared() { var entityStremRepo = new Mock <IEventRepository>(); var snapShotRepo = new Mock <ISnapShotRepository>(); snapShotRepo.Setup(re => re.LoadSnapShot <TestEntity_NoIApply>(It.IsAny <string>())) .ReturnsAsync(SnapShotResult <TestEntity_NoIApply> .Default()); var entityId = Guid.NewGuid(); var testEventEventStore = new TestEventEventStore(entityId); var domainEventWrapper = new DomainEventWrapper { DomainEvent = testEventEventStore }; entityStremRepo.Setup(ev => ev.LoadEventsByEntity(It.IsAny <string>(), It.IsAny <long>())) .ReturnsAsync(Result <IEnumerable <DomainEventWrapper> > .Ok(new[] { domainEventWrapper })); var eventStore = new EventStore(entityStremRepo.Object, snapShotRepo.Object, new SnapShotConfig()); var loadAsync = await eventStore.LoadAsync <TestEntity_NoIApply>(entityId.ToString()); Assert.AreEqual(Guid.Empty, loadAsync.Value.Id); }