public async Task Should_persist_events()
        {
            var aggregate = new TestAggregate(_id);
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id);
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id);

            aggregate.Version.Should().Be(2);
        }
        public async Task Can_create_save_load_and_update()
        {
            var id = "someaggregate-" + Guid.NewGuid();
            var aggregate = new TestAggregate(id);
            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(id);
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(id);

            aggregate.Version.Should().Be(3);
        }
        public async Task Can_create_save_load_and_update()
        {
            await _eventStoreInitialized;
            var aggregate = new TestAggregate(_id);
            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id);
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id);

            aggregate.Version.Should().Be(3);
        }
 public When_saving_aggregate()
 {
     _testRep   = new TestRepository();
     _rep       = new CacheRepository(_testRep, new TestInMemoryEventStore(), new TestMemoryCache());
     _aggregate = _testRep.Get <TestAggregate>(Guid.NewGuid());
     _aggregate.DoSomething();
     _rep.Save(_aggregate, -1);
 }
Exemple #5
0
        public async Task Should_update_version_number()
        {
            var aggregate = new TestAggregate(_id);

            aggregate.DoSomething();
            await _rep.Save(aggregate);

            Assert.Equal(_eventStore.Events.Last().Version, _eventStore.Events.Count);
        }
Exemple #6
0
        public async Task Can_create_save_load_and_update()
        {
            var id        = "someaggregate-" + Guid.NewGuid();
            var aggregate = new TestAggregate(id);

            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById <TestAggregate>(id);

            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById <TestAggregate>(id);

            aggregate.Version.Should().Be(3);
        }
        public async Task Should_throw_an_exception_on_duplicate_write()
        {
            await _eventStoreInitialized;
            var   aggregate = new TestAggregate(_id);

            aggregate.DoSomething();
            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById <TestAggregate>(_id, 2);

            aggregate.DoSomething();

            Func <Task> act = () => _sut.Save(aggregate, Guid.NewGuid());

            act.ShouldThrow <Exception>();
        }
Exemple #8
0
 public When_saving_fails()
 {
     _testRep   = new TestRepository();
     _cache     = new TestMemoryCache();
     _rep       = new CacheRepository(_testRep, new TestInMemoryEventStore(), _cache);
     _aggregate = _testRep.Get <TestAggregate>(Guid.NewGuid());
     _aggregate.DoSomething();
     try { _rep.Save(_aggregate, 100); }  catch (Exception) {}
 }
        public async Task Can_create_save_load_and_update()
        {
            await _eventStoreInitialized;
            var   aggregate = new TestAggregate(_id);

            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById <TestAggregate>(_id);

            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById <TestAggregate>(_id);

            aggregate.Version.Should().Be(3);
        }
Exemple #10
0
        public async Task Should_throw_concurrency_exception_from_session()
        {
            await _session.Add(_aggregate);

            _aggregate.DoSomething();
            await _rep.Save(_aggregate);

            await Assert.ThrowsAsync <ConcurrencyException>(async() => await _session.Commit());
        }
Exemple #11
0
        public When_saving_stale_data()
        {
            _eventStore = new TestInMemoryEventStore();
            _rep        = new Repository(_eventStore);
            _session    = new Session(_rep);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _aggregate.DoSomething();
            _rep.Save(_aggregate);
        }
Exemple #12
0
        public void GettingChanges()
        {
            var aggregate = new TestAggregate();

            using var _ = UnitOfWork.Start();
            UnitOfWork.Current.Attach(new ("stream", aggregate, Optional <long> .Empty));
            aggregate.DoSomething();
            Assert.True(UnitOfWork.Current.HasChanges);
            Assert.Single(UnitOfWork.Current.GetChanges());
        }
Exemple #13
0
        public void Setup()
        {
            _eventStore = new TestInMemoryEventStore();
            _rep        = new Repository(_eventStore);
            _session    = new Session(_rep);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _aggregate.DoSomething();
            _rep.Save(_aggregate);
        }
Exemple #14
0
        public SaveStaleDataTest()
        {
            _eventStore = new TestInMemoryEventStore();
            _rep        = new Repository(_eventStore);
            _session    = new Session(_rep);

            _aggregate = new TestAggregate(Guid.NewGuid().ToString());
            _aggregate.DoSomething();
            _rep.SaveAsync(_aggregate).Wait();
        }
Exemple #15
0
        public async Task Should_forward_cancellation_token()
        {
            var token = new CancellationToken();
            var agg   = new TestAggregate(Guid.NewGuid());
            await _session.Add(agg, token);

            agg.DoSomething();
            await _session.Commit(token);

            Assert.Equal(token, _eventStore.Token);
        }
Exemple #16
0
        public void Should_clear_tracked_aggregates()
        {
            var agg = new TestAggregate(Guid.NewGuid());

            _unitOfWork.Add(agg);
            agg.DoSomething();
            _unitOfWork.Commit();
            _eventStore.Events.Clear();

            Assert.Throws <AggregateNotFoundException <TestAggregate, ISingleSignOnToken> >(() => _unitOfWork.Get <TestAggregate>(agg.Id));
        }
Exemple #17
0
        public void Should_clear_tracked_aggregates()
        {
            var agg = new TestAggregate(Guid.NewGuid());

            _session.Add(agg);
            agg.DoSomething();
            _session.Commit();
            _eventStore.Events.Clear();

            Assert.Throws <AggregateNotFoundException>(() => _session.Get <TestAggregate>(agg.Id));
        }
Exemple #18
0
        public When_saving_stale_data()
        {
            _eventStore = new TestInMemoryEventStore();
            _rep        = new Repository(_eventStore);
            _session    = new Session(_rep);

            _id        = Guid.NewGuid();
            _aggregate = new TestAggregate(_id);
            _aggregate.DoSomething();
            Task.Run(async() => await _rep.Save(_aggregate)).Wait();
        }
Exemple #19
0
        public async Task Should_clear_tracked_aggregates()
        {
            var agg = new TestAggregate(Guid.NewGuid());
            await _session.Add(agg);

            agg.DoSomething();
            await _session.Commit();

            _eventStore.Events.Clear();

            await Assert.ThrowsAsync <AggregateNotFoundException>(async() => await _session.Get <TestAggregate>(agg.Id));
        }
 public void Setup()
 {
     _testRep   = new TestAggregateRepository();
     _rep       = new CacheRepository <ISingleSignOnToken>(_testRep, new TestInMemoryEventStore());
     _aggregate = _testRep.Get <TestAggregate>(Guid.NewGuid());
     _aggregate.DoSomething();
     try
     {
         _rep.Save(_aggregate, 100);
     }
     catch (Exception) {}
 }
        public void Setup()
        {
            _eventStore     = new TestInMemoryEventStore();
            _eventPublisher = new TestEventPublisher();
            var aggregateFactory = new AggregateFactory(new TestDependencyResolver());

            _rep        = new Repository <ISingleSignOnToken>(aggregateFactory, _eventStore, _eventPublisher, new NullCorrelationIdHelper());
            _unitOfWork = new UnitOfWork <ISingleSignOnToken>(_rep);

            _aggregate = new TestAggregate(Guid.NewGuid());
            _aggregate.DoSomething();
            _rep.Save(_aggregate);
        }
Exemple #22
0
 public When_saving_fails()
 {
     _cache     = new MemoryCache();
     _testRep   = new TestRepository();
     _rep       = new CacheRepository(_testRep, new TestInMemoryEventStore(), _cache);
     _aggregate = _testRep.Get <TestAggregate>(Guid.NewGuid()).Result;
     _aggregate.DoSomething();
     _testRep.Throw = true;
     try
     {
         _rep.Save(_aggregate).Wait();
     }
     catch (Exception) { }
 }
Exemple #23
0
        public async Task ThrowConcurrencyExceptionFromSession()
        {
            _session.Add(_aggregate);
            _aggregate.DoSomething();
            await _rep.SaveAsync(_aggregate);

            try
            {
                await _session.CommitAsync();

                Assert.Fail();
            }
            catch (ConcurrencyException)
            {
                Assert.IsTrue(true);
            }
        }
        public SaveNotSnapshotableAggregate()
        {
            _eventStore    = new TestInMemoryEventStore();
            _snapshotStore = new TestSnapshotStore();
            var snapshotStrategy = new DefaultSnapshotStrategy(_snapshotStore);
            var repository       = new SnapshotRepository(_snapshotStore, snapshotStrategy, new Repository(_eventStore), _eventStore);
            var session          = new Session(repository);
            var aggregate        = new TestAggregate(Guid.NewGuid().ToString());

            for (var i = 0; i < 200; i++)
            {
                aggregate.DoSomething();
            }
            Task.Run(async() =>
            {
                session.Add(aggregate);
                await session.CommitAsync();
            }).Wait();
        }
Exemple #25
0
        public async Task ClearsTrackedAggregates()
        {
            var agg = new TestAggregate(Guid.NewGuid().ToString());

            _session.Add(agg);
            agg.DoSomething();
            await _session.CommitAsync();

            _eventStore.Events.Clear();

            try
            {
                var result = await _session.GetAsync <TestAggregate>(agg.Id);

                Assert.IsNull(result);
            }
            catch (AggregateNotFoundException)
            {
                Assert.IsTrue(true);
            }
        }
Exemple #26
0
        public When_saving_a_not_snapshotable_aggregate()
        {
            _eventStore    = new TestInMemoryEventStore();
            _snapshotStore = new TestSnapshotStore();
            var snapshotStrategy = new DefaultSnapshotStrategy();
            var repository       = new SnapshotRepository(_snapshotStore, snapshotStrategy, new Repository(_eventStore), _eventStore);
            var session          = new Session(repository);
            var aggregate        = new TestAggregate(Guid.NewGuid());

            _token = new CancellationToken();

            for (var i = 0; i < 200; i++)
            {
                aggregate.DoSomething();
            }
            Task.Run(async() =>
            {
                await session.Add(aggregate, _token);
                await session.Commit(_token);
            }).Wait();
        }
        public async Task Should_throw_an_exception_on_duplicate_write()
        {
            await _eventStoreInitialized;
            var aggregate = new TestAggregate(_id);

            aggregate.DoSomething();
            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id, 2);
            aggregate.DoSomething();

            Func<Task> act = () => _sut.Save(aggregate, Guid.NewGuid());

            act.ShouldThrow<Exception>();
        }
        public async Task Can_load_the_requested_version()
        {
            await _eventStoreInitialized;
            var aggregate = new TestAggregate(_id);
            aggregate.DoSomething();
            aggregate.DoSomething();
            aggregate.DoSomething();
            await _sut.Save(aggregate, Guid.NewGuid());

            aggregate = await _sut.GetById<TestAggregate>(_id, 2);

            aggregate.Version.Should().Be(2);
        }
Exemple #29
0
 public void Should_throw_concurrency_exception_from_repository()
 {
     _aggregate.DoSomething();
     Assert.Throws <ConcurrencyException>(() => _rep.Save(_aggregate, 0));
 }
 public void Should_call_apply_if_exist()
 {
     _aggregate.DoSomething();
     Assert.AreEqual(1, _aggregate.DidSomethingCount);
 }
 public void CallsApplyIfExist()
 {
     var aggregate = new TestAggregate(Guid.NewGuid().ToString());
     aggregate.DoSomething();
     Assert.AreEqual(1, aggregate.DidSomethingCount);
 }