public void Add_ValidProjectionView_ProjectionViewCanBeRetreived() { // Arrange var correlationIdHelper = new CorrelationIdHelper(new ThreadedContextItemCollectionFactory()); correlationIdHelper.SetCorrelationId(Guid.NewGuid()); var logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper); var connectionStringFactory = new TestMongoDataStoreConnectionStringFactory(); TestMongoDataStoreConnectionStringFactory.DatabaseName = string.Format("Test-{0}", new Random().Next(0, 9999)); var factory = new TestMongoDbDataStoreFactory(logger, connectionStringFactory); IMongoCollection <TestEvent> collection = factory.GetTestEventCollection(); try { // Arrange var dataStore = new MongoDbDataStore <TestEvent>(logger, collection); var event1 = new TestEvent { Rsn = Guid.NewGuid(), Id = Guid.NewGuid(), CorrelationId = correlationIdHelper.GetCorrelationId(), Frameworks = new List <string> { "Test 1" }, TimeStamp = DateTimeOffset.UtcNow }; // Act dataStore.Add(event1); // Assert var timer = new Stopwatch(); timer.Start(); TestEvent view = dataStore.SingleOrDefault(e => e.Rsn == event1.Rsn); timer.Stop(); Console.WriteLine("Load operation took {0}", timer.Elapsed); Assert.IsNotNull(view); Assert.AreEqual(event1.Id, view.Id); } finally { // Clean-up collection.Database.Client.DropDatabase(TestMongoDataStoreConnectionStringFactory.DatabaseName); } }
public void Should_load_events() { // Arrange TestMongoDbSnapshotStoreConnectionStringFactory.DatabaseName = string.Format("Test-{0}", new Random().Next(0, 9999)); NinjectDependencyResolver.ModulesToLoad.Add(new CqrsModule <int, DefaultAuthenticationTokenHelper>()); NinjectDependencyResolver.ModulesToLoad.Add(new InProcessEventBusModule <int>()); NinjectDependencyResolver.ModulesToLoad.Add(new TestMongoDbModule <int>()); NinjectDependencyResolver.Start(); var unitOfWork = new UnitOfWork <int>(DependencyResolver.Current.Resolve <ISnapshotAggregateRepository <int> >()); var aggregate = DependencyResolver.Current.Resolve <IAggregateFactory>().Create <TestAggregate>(Guid.NewGuid()); unitOfWork.Add(aggregate); try { int count = 0; do { aggregate.GenerateRandomNumber(); if (count % 10 == 0) { unitOfWork.Commit(); unitOfWork.Add(aggregate); } } while (count++ <= 20); unitOfWork.Commit(); // Act var aggregate2 = unitOfWork.Get <TestAggregate>(aggregate.Rsn); // Assert Assert.AreEqual(22, aggregate2.Version); Assert.AreEqual(aggregate.CurrentRandomNumber, aggregate2.CurrentRandomNumber); } finally { // Clean-up TestMongoDataStoreConnectionStringFactory.DatabaseName = TestMongoDbSnapshotStoreConnectionStringFactory.DatabaseName; var factory = new TestMongoDbDataStoreFactory(DependencyResolver.Current.Resolve <ILogger>(), new TestMongoDataStoreConnectionStringFactory()); IMongoCollection <TestEvent> collection = factory.GetTestEventCollection(); collection.Database.Client.DropDatabase(TestMongoDataStoreConnectionStringFactory.DatabaseName); } }
public void Save_ValidEvent_EventCanBeRetreived() { // Arrange var correlationIdHelper = new CorrelationIdHelper(new ContextItemCollectionFactory()); correlationIdHelper.SetCorrelationId(Guid.NewGuid()); var logger = new ConsoleLogger(new LoggerSettings(), correlationIdHelper); try { // Arrange var connectionStringFactory = new TestMongoEventStoreConnectionStringFactory(); TestMongoEventStoreConnectionStringFactory.DatabaseName = string.Format("Test-{0}", new Random().Next(0, 9999)); var eventStore = new MongoDbEventStore <Guid>(new MongoDbEventBuilder <Guid>(), new MongoDbEventDeserialiser <Guid>(), logger, connectionStringFactory, new ConfigurationManager()); var event1 = new TestEvent { Rsn = Guid.NewGuid(), Id = Guid.NewGuid(), CorrelationId = correlationIdHelper.GetCorrelationId(), Frameworks = new List <string> { "Test 1" }, TimeStamp = DateTimeOffset.UtcNow }; var event2 = new TestEvent { Rsn = Guid.NewGuid(), Id = Guid.NewGuid(), CorrelationId = correlationIdHelper.GetCorrelationId(), Frameworks = new List <string> { "Test 2" }, TimeStamp = DateTimeOffset.UtcNow }; // Act eventStore.Save <TestEvent>(event1); eventStore.Save <TestEvent>(event2); // Assert var timer = new Stopwatch(); IList <IEvent <Guid> > events = eventStore.Get <TestEvent>(event1.Id).ToList(); timer.Stop(); Console.WriteLine("Load one operation took {0}", timer.Elapsed); Assert.AreEqual(1, events.Count); Assert.AreEqual(event1.Id, events.Single().Id); Assert.AreEqual(event1.Frameworks.Single(), events.Single().Frameworks.Single()); timer.Restart(); events = eventStore.Get <TestEvent>(event2.Id).ToList(); timer.Stop(); Console.WriteLine("Load one operation took {0}", timer.Elapsed); Assert.AreEqual(1, events.Count); Assert.AreEqual(event2.Id, events.Single().Id); Assert.AreEqual(event2.Frameworks.Single(), events.Single().Frameworks.Single()); timer.Restart(); IList <EventData> correlatedEvents = eventStore.Get(event1.CorrelationId).ToList(); timer.Stop(); Console.WriteLine("Load several correlated operation took {0}", timer.Elapsed); Assert.AreEqual(2, correlatedEvents.Count); } finally { // Clean-up TestMongoDataStoreConnectionStringFactory.DatabaseName = TestMongoEventStoreConnectionStringFactory.DatabaseName; var factory = new TestMongoDbDataStoreFactory(logger, new TestMongoDataStoreConnectionStringFactory()); IMongoCollection <TestEvent> collection = factory.GetTestEventCollection(); collection.Database.Client.DropDatabase(TestMongoDataStoreConnectionStringFactory.DatabaseName); } }