PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/iSynaptic.Core.UnitTests/Modeling/Domain/AggregateEventStreamTests.cs

https://github.com/iSynaptic/iSynaptic.Core
C# | 227 lines | 157 code | 49 blank | 21 comment | 0 complexity | 7cd450a8fcc1362062a24cf92f14f1ee MD5 | raw file
  1. // The MIT License
  2. //
  3. // Copyright (c) 2013 Jordan E. Terrell
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. using System;
  23. using System.Linq;
  24. using FluentAssertions;
  25. using NUnit.Framework;
  26. using iSynaptic.Commons;
  27. using iSynaptic.TestDomain;
  28. namespace iSynaptic.Modeling.Domain
  29. {
  30. [TestFixture]
  31. public class AggregateEventStreamTests
  32. {
  33. private static readonly Guid _id = Guid.NewGuid();
  34. private static readonly ServiceCase.Opened _openedEvent
  35. = new ServiceCase.Opened(ServiceCase.SampleContent.Title, ServiceCase.SampleContent.Description, ServiceCase.Priority.Normal, ServiceCase.SampleContent.ResponsibleParty, _id, 1);
  36. [Test]
  37. public void Stream_DefaultsCorrect()
  38. {
  39. var stream = new AggregateEventStream<Guid>();
  40. stream.IsTruncated.Should().BeFalse();
  41. stream.Version.Should().Be(0);
  42. stream.CommittedVersion.Should().Be(0);
  43. }
  44. [Test]
  45. public void Appending_OutOfOrder_ThrowsException()
  46. {
  47. var stream = new AggregateEventStream<ServiceCaseId>();
  48. var e2 = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 2);
  49. stream.AppendEvent(e2);
  50. stream.Invoking(x => x.AppendEvent(_openedEvent))
  51. .ShouldThrow<InvalidOperationException>();
  52. }
  53. [Test]
  54. public void Appending_WithGaps_ThrowsException()
  55. {
  56. var stream = new AggregateEventStream<ServiceCaseId>();
  57. var outOfOrderEvent = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 3);
  58. stream.AppendEvent(_openedEvent);
  59. stream.Invoking(x => x.AppendEvent(outOfOrderEvent))
  60. .ShouldThrow<InvalidOperationException>();
  61. }
  62. [Test]
  63. public void AppendingInitialEvent_WithVersionEqualToOne_IsNotTruncated()
  64. {
  65. var stream = new AggregateEventStream<ServiceCaseId>();
  66. stream.AppendEvent(_openedEvent);
  67. stream.IsTruncated.Should().BeFalse();
  68. }
  69. [Test]
  70. public void AppendingInitialEvent_WithVersionGreaterThanOne_IsTruncated()
  71. {
  72. var stream = new AggregateEventStream<ServiceCaseId>();
  73. stream.AppendEvent(new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 7));
  74. stream.IsTruncated.Should().BeTrue();
  75. }
  76. [Test]
  77. public void AppendingInitialEvent_EventStreamsAndVersionCorrect()
  78. {
  79. var stream = new AggregateEventStream<ServiceCaseId>();
  80. stream.AppendEvent(_openedEvent);
  81. stream.Events.Count().Should().Be(1);
  82. stream.UncommittedEvents.Count().Should().Be(1);
  83. stream.CommittedEvents.Count().Should().Be(0);
  84. stream.CommittedVersion.Should().Be(0);
  85. stream.Version.Should().Be(1);
  86. stream.UncommittedEvents.First().Should().Be(_openedEvent);
  87. }
  88. [Test]
  89. public void CommittingInitialEvent_EventStreamsAndVersionCorrect()
  90. {
  91. var stream = new AggregateEventStream<ServiceCaseId>();
  92. stream.AppendEvent(_openedEvent);
  93. stream.CommitEvents();
  94. stream.Events.Count().Should().Be(1);
  95. stream.UncommittedEvents.Count().Should().Be(0);
  96. stream.CommittedEvents.Count().Should().Be(1);
  97. stream.CommittedVersion.Should().Be(1);
  98. stream.Version.Should().Be(1);
  99. stream.CommittedEvents.First().Should().Be(_openedEvent);
  100. }
  101. [Test]
  102. public void AppendingManyEvents_EventStreamsAndVersionCorrect()
  103. {
  104. var stream = new AggregateEventStream<ServiceCaseId>();
  105. var e2 = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 2);
  106. var e3 = new ServiceCase.CommunicationRecorded(1, CommunicationDirection.Incoming, ServiceCase.SampleContent.CommunicationContent, SystemClock.UtcNow, ServiceCase.SampleContent.CommunicationDuration, ServiceCase.SampleContent.ResponsibleParty, _id, 3);
  107. stream.AppendEvent(_openedEvent);
  108. stream.AppendEvent(e2);
  109. stream.AppendEvent(e3);
  110. stream.Events.Count().Should().Be(3);
  111. stream.UncommittedEvents.Count().Should().Be(3);
  112. stream.CommittedEvents.Count().Should().Be(0);
  113. stream.CommittedVersion.Should().Be(0);
  114. stream.Version.Should().Be(3);
  115. stream.UncommittedEvents.ElementAt(0).Should().Be(_openedEvent);
  116. stream.UncommittedEvents.ElementAt(1).Should().Be(e2);
  117. stream.UncommittedEvents.ElementAt(2).Should().Be(e3);
  118. }
  119. [Test]
  120. public void CommittingManyEvents_EventStreamsAndVersionCorrect()
  121. {
  122. var stream = new AggregateEventStream<ServiceCaseId>();
  123. var e2 = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 2);
  124. var e3 = new ServiceCase.CommunicationRecorded(1, CommunicationDirection.Incoming, ServiceCase.SampleContent.CommunicationContent, SystemClock.UtcNow, ServiceCase.SampleContent.CommunicationDuration, ServiceCase.SampleContent.ResponsibleParty, _id, 3);
  125. stream.AppendEvent(_openedEvent);
  126. stream.AppendEvent(e2);
  127. stream.AppendEvent(e3);
  128. stream.CommitEvents();
  129. stream.Events.Count().Should().Be(3);
  130. stream.UncommittedEvents.Count().Should().Be(0);
  131. stream.CommittedEvents.Count().Should().Be(3);
  132. stream.CommittedVersion.Should().Be(3);
  133. stream.Version.Should().Be(3);
  134. stream.CommittedEvents.ElementAt(0).Should().Be(_openedEvent);
  135. stream.CommittedEvents.ElementAt(1).Should().Be(e2);
  136. stream.CommittedEvents.ElementAt(2).Should().Be(e3);
  137. }
  138. [Test]
  139. public void CommittingEvents_UpToVersion_EventStreamsAndVersionCorrect()
  140. {
  141. var stream = new AggregateEventStream<ServiceCaseId>();
  142. var e2 = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 2);
  143. var e3 = new ServiceCase.CommunicationRecorded(1, CommunicationDirection.Incoming, ServiceCase.SampleContent.CommunicationContent, SystemClock.UtcNow, ServiceCase.SampleContent.CommunicationDuration, ServiceCase.SampleContent.ResponsibleParty, _id, 3);
  144. stream.AppendEvent(_openedEvent);
  145. stream.AppendEvent(e2);
  146. stream.AppendEvent(e3);
  147. stream.CommitEvents(2);
  148. stream.Events.Count().Should().Be(3);
  149. stream.UncommittedEvents.Count().Should().Be(1);
  150. stream.CommittedEvents.Count().Should().Be(2);
  151. stream.CommittedVersion.Should().Be(2);
  152. stream.Version.Should().Be(3);
  153. stream.CommittedEvents.ElementAt(0).Should().Be(_openedEvent);
  154. stream.CommittedEvents.ElementAt(1).Should().Be(e2);
  155. stream.UncommittedEvents.ElementAt(0).Should().Be(e3);
  156. }
  157. [Test]
  158. public void CommittingEvents_UpToVersion_WhenStreamIsTruncated_YieldsSomeCommittedEvents()
  159. {
  160. var stream = new AggregateEventStream<ServiceCaseId>();
  161. var e1 = new ServiceCase.CommunicationThreadStarted(1, ServiceCase.SampleContent.Topic, ServiceCase.SampleContent.TopicDescription, ServiceCase.SampleContent.ResponsibleParty, _id, 7);
  162. var e2 = new ServiceCase.CommunicationRecorded(1, CommunicationDirection.Incoming, ServiceCase.SampleContent.CommunicationContent, SystemClock.UtcNow, ServiceCase.SampleContent.CommunicationDuration, ServiceCase.SampleContent.ResponsibleParty, _id, 8);
  163. var e3 = new ServiceCase.CommunicationRecorded(1, CommunicationDirection.Incoming, ServiceCase.SampleContent.CommunicationContent, SystemClock.UtcNow, ServiceCase.SampleContent.CommunicationDuration, ServiceCase.SampleContent.ResponsibleParty, _id, 9);
  164. stream.AppendEvent(e1);
  165. stream.AppendEvent(e2);
  166. stream.AppendEvent(e3);
  167. stream.CommitEvents(8);
  168. stream.Events.Count().Should().Be(3);
  169. stream.UncommittedEvents.Count().Should().Be(1);
  170. stream.CommittedEvents.Count().Should().Be(2);
  171. stream.CommittedVersion.Should().Be(8);
  172. stream.Version.Should().Be(9);
  173. stream.CommittedEvents.ElementAt(0).Should().Be(e1);
  174. stream.CommittedEvents.ElementAt(1).Should().Be(e2);
  175. stream.UncommittedEvents.ElementAt(0).Should().Be(e3);
  176. }
  177. }
  178. }