Line data Source code
1 : using System;
2 : using cdmdotnet.Logging;
3 : using Cqrs.Domain;
4 : using Cqrs.Domain.Exceptions;
5 : using Cqrs.Domain.Factories;
6 : using Cqrs.Authentication;
7 : using Cqrs.Tests.Substitutes;
8 : using NUnit.Framework;
9 :
10 : namespace Cqrs.Tests.Domain
11 : {
12 : [TestFixture]
13 : public class When_getting_an_aggregate
14 0 : {
15 : private IUnitOfWork<ISingleSignOnToken> _unitOfWork;
16 :
17 : private TestDependencyResolver _dependencyResolver;
18 :
19 : [SetUp]
20 0 : public void Setup()
21 : {
22 : var eventStore = new TestEventStore();
23 : _dependencyResolver = new TestDependencyResolver(eventStore);
24 : var aggregateFactory = new AggregateFactory(_dependencyResolver, _dependencyResolver.Resolve<ILogger>());
25 : var testEventPublisher = new TestEventPublisher();
26 : _unitOfWork = new UnitOfWork<ISingleSignOnToken>(new Repository<ISingleSignOnToken>(aggregateFactory, eventStore, testEventPublisher, new NullCorrelationIdHelper()));
27 : }
28 :
29 : [Test]
30 0 : public void Should_get_aggregate_from_eventstore()
31 : {
32 : _dependencyResolver.UseTestEventStoreGuid = false;
33 : var aggregate = _unitOfWork.Get<TestAggregate>(Guid.NewGuid());
34 : Assert.NotNull(aggregate);
35 : }
36 :
37 : [Test]
38 0 : public void Should_apply_events()
39 : {
40 : var aggregate = _unitOfWork.Get<TestAggregate>(Guid.NewGuid());
41 : Assert.AreEqual(2, aggregate.DidSomethingCount);
42 : }
43 :
44 : [Test]
45 0 : public void Should_fail_if_aggregate_do_not_exist()
46 : {
47 : _dependencyResolver.UseTestEventStoreGuid = true;
48 : Assert.Throws<AggregateNotFoundException<TestAggregate, ISingleSignOnToken>>(() => _unitOfWork.Get<TestAggregate>(Guid.Empty));
49 : }
50 :
51 : [Test]
52 0 : public void Should_track_changes()
53 : {
54 : var agg = new TestAggregate(Guid.NewGuid());
55 : _unitOfWork.Add(agg);
56 : var aggregate = _unitOfWork.Get<TestAggregate>(agg.Id);
57 : Assert.AreEqual(agg,aggregate);
58 : }
59 :
60 : [Test]
61 0 : public void Should_get_from_session_if_tracked()
62 : {
63 : var id = Guid.NewGuid();
64 : _dependencyResolver.NewAggregateGuid = id;
65 : var aggregate = _unitOfWork.Get<TestAggregate>(id);
66 : var aggregate2 = _unitOfWork.Get<TestAggregate>(id);
67 :
68 : Assert.AreEqual(aggregate, aggregate2);
69 : }
70 :
71 : [Test]
72 0 : public void Should_throw_concurrency_exception_if_tracked()
73 : {
74 : var id = Guid.NewGuid();
75 : _unitOfWork.Get<TestAggregate>(id);
76 :
77 : Assert.Throws<ConcurrencyException>(() => _unitOfWork.Get<TestAggregate>(id, 100));
78 : }
79 :
80 : [Test]
81 0 : public void Should_get_correct_version()
82 : {
83 : var id = Guid.NewGuid();
84 : var aggregate = _unitOfWork.Get<TestAggregate>(id);
85 :
86 : Assert.AreEqual(4, aggregate.Version);
87 : }
88 :
89 : [Test]
90 0 : public void Should_throw_concurrency_exception()
91 : {
92 : var id = Guid.NewGuid();
93 : Assert.Throws<ConcurrencyException>(() => _unitOfWork.Get<TestAggregate>(id, 1));
94 : }
95 : }
96 : }
|