Documentation Coverage Report
Current view: top level - Cqrs.Tests.Integrations - SnapshotTests.cs Hit Total Coverage
Version: 2.2 Artefacts: 3 10 30.0 %
Date: 2018-08-07 15:04:50

          Line data    Source code
       1             : using System;
       2             : using System.Collections.Generic;
       3             : using cdmdotnet.Logging;
       4             : using Cqrs.Authentication;
       5             : using Cqrs.Configuration;
       6             : using Cqrs.Domain;
       7             : using Cqrs.Domain.Factories;
       8             : using Cqrs.Events;
       9             : using Cqrs.Messages;
      10             : using Cqrs.Ninject.Configuration;
      11             : using Cqrs.Snapshots;
      12             : using NUnit.Framework;
      13             : 
      14             : namespace Cqrs.Tests.Integrations
      15             : {
      16             :         [TestFixture]
      17             :         public class SnapshotTests
      18           0 :         {
      19             :                 [Test]
      20           0 :                 public void Should_load_events()
      21             :                 {
      22             :                         // Arrange
      23             :                         NinjectDependencyResolver.ModulesToLoad.Add(new CqrsModule<int, DefaultAuthenticationTokenHelper>());
      24             :                         NinjectDependencyResolver.ModulesToLoad.Add(new SimplifiedSqlModule<int>());
      25             :                         NinjectDependencyResolver.ModulesToLoad.Add(new InProcessEventBusModule<int>());
      26             :                         NinjectDependencyResolver.Start();
      27             :                         var unitOfWork = new UnitOfWork<int>(DependencyResolver.Current.Resolve<ISnapshotAggregateRepository<int>>());
      28             :                         var aggregate = DependencyResolver.Current.Resolve<IAggregateFactory>().Create<TestAggregate>(Guid.NewGuid());
      29             :                         unitOfWork.Add(aggregate);
      30             :                         int count = 0;
      31             :                         do
      32             :                         {
      33             :                                 aggregate.GenerateRandomNumber();
      34             :                                 if (count%10 == 0)
      35             :                                 {
      36             :                                         unitOfWork.Commit();
      37             :                                         unitOfWork.Add(aggregate);
      38             :                                 }
      39             :                         } while (count++ <= 20);
      40             :                         unitOfWork.Commit();
      41             : 
      42             :                         // Act
      43             :                         var aggregate2 = unitOfWork.Get<TestAggregate>(aggregate.Rsn);
      44             : 
      45             :                         // Assert
      46             :                         Assert.AreEqual(22, aggregate2.Version);
      47             :                         Assert.AreEqual(aggregate.CurrentRandomNumber, aggregate2.CurrentRandomNumber);
      48             :                 }
      49             : 
      50             :                 public class TestAggregate
      51             :                         : SnapshotAggregateRoot<int, TestAggregateSnapshot>
      52           0 :                 {
      53             :                         /// <summary>
      54             :                         /// Gets or sets the <see cref="IDependencyResolver"/> used.
      55             :                         /// </summary>
      56             :                         protected IDependencyResolver DependencyResolver { get; private set; }
      57             : 
      58             :                         /// <summary>
      59             :                         /// Gets or sets the <see cref="ILogger"/> used.
      60             :                         /// </summary>
      61             :                         protected ILogger Logger { get; private set; }
      62             : 
      63             :                         /// <summary>
      64             :                         /// Instantiates a new instance of <see cref="AggregateFactory"/>.
      65             :                         /// </summary>
      66           1 :                         public TestAggregate(IDependencyResolver dependencyResolver, ILogger logger, Guid rsn)
      67             :                         {
      68             :                                 DependencyResolver = dependencyResolver;
      69             :                                 Logger = logger;
      70             :                                 Rsn = rsn;
      71             :                         }
      72             : 
      73             :                         public Guid Rsn
      74             :                         {
      75             :                                 get { return Id; }
      76             :                                 set { Id = value; }
      77             :                         }
      78             : 
      79             :                         public int CurrentRandomNumber { get; set; }
      80             : 
      81           0 :                         public void GenerateRandomNumber()
      82             :                         {
      83             :                                 ApplyChange(new RandomNumberEvent(Rsn));
      84             :                         }
      85             : 
      86             :                         private void Apply(RandomNumberEvent @event)
      87             :                         {
      88             :                                 CurrentRandomNumber = @event.RandomNumber;
      89             :                         }
      90             : 
      91             :                         #region Overrides of SnapshotAggregateRoot<int,TestAggregateSnapshot>
      92             : 
      93             :                         /// <summary>
      94             :                         /// Create a <see cref="TestAggregateSnapshot"/> of the current state of this instance.
      95             :                         /// </summary>
      96           1 :                         protected override TestAggregateSnapshot CreateSnapshot()
      97             :                         {
      98             :                                 return new TestAggregateSnapshot {CurrentRandomNumber = CurrentRandomNumber};
      99             :                         }
     100             : 
     101             :                         /// <summary>
     102             :                         /// Rehydrate this instance from the provided <paramref name="snapshot"/>.
     103             :                         /// </summary>
     104             :                         /// <param name="snapshot">The <see cref="TestAggregateSnapshot"/> to rehydrate this instance from.</param>
     105           1 :                         protected override void RestoreFromSnapshot(TestAggregateSnapshot snapshot)
     106             :                         {
     107             :                                 CurrentRandomNumber = snapshot.CurrentRandomNumber;
     108             :                         }
     109             : 
     110             :                         #endregion
     111             :                 }
     112             : 
     113             :                 public class TestAggregateSnapshot
     114             :                         : Snapshot
     115           0 :                 {
     116             :                         public int CurrentRandomNumber { get; set; }
     117             :                 }
     118             : 
     119             :                 public class RandomNumberEvent
     120             :                         : IEventWithIdentity<int>
     121           0 :                 {
     122             :                         #region Implementation of IMessage
     123             : 
     124             :                         /// <summary>
     125             :                         /// An identifier used to group together several <see cref="IMessage"/>. Any <see cref="IMessage"/> with the same <see cref="CorrelationId"/> were triggered by the same initiating request.
     126             :                         /// </summary>
     127             :                         public Guid CorrelationId { get; set; }
     128             : 
     129             :                         /// <summary>
     130             :                         /// The originating framework this message was sent from.
     131             :                         /// </summary>
     132             :                         public string OriginatingFramework { get; set; }
     133             : 
     134             :                         /// <summary>
     135             :                         /// The frameworks this <see cref="IMessage"/> has been delivered to/sent via already.
     136             :                         /// </summary>
     137             :                         public IEnumerable<string> Frameworks { get; set; }
     138             : 
     139             :                         #endregion
     140             : 
     141             :                         #region Implementation of IMessageWithAuthenticationToken<int>
     142             : 
     143             :                         /// <summary>
     144             :                         /// The <typeparamref name="TAuthenticationToken"/> of the entity that triggered the event to be raised.
     145             :                         /// </summary>
     146             :                         public int AuthenticationToken { get; set; }
     147             : 
     148             :                         #endregion
     149             : 
     150             :                         #region Implementation of IEvent<int>
     151             : 
     152             :                         /// <summary>
     153             :                         /// The ID of the <see cref="IEvent{TAuthenticationToken}"/>
     154             :                         /// </summary>
     155             :                         public Guid Id { get; set; }
     156             : 
     157             :                         /// <summary>
     158             :                         /// The version of the <see cref="IEvent{TAuthenticationToken}"/>
     159             :                         /// </summary>
     160             :                         public int Version { get; set; }
     161             : 
     162             :                         /// <summary>
     163             :                         /// The date and time the event was raised or published.
     164             :                         /// </summary>
     165             :                         public DateTimeOffset TimeStamp { get; set; }
     166             : 
     167             :                         #endregion
     168             : 
     169             :                         #region Implementation of IEventWithIdentity<int>
     170             : 
     171             :                         /// <summary>
     172             :                         /// The identity of the <see cref="IAggregateRoot{TAuthenticationToken}">aggregate</see> being targeted.
     173             :                         /// </summary>
     174             :                         public Guid Rsn { get; set; }
     175             : 
     176             :                         #endregion
     177             : 
     178             :                         public int RandomNumber { get; set; }
     179             : 
     180           0 :                         public RandomNumberEvent(Guid rsn)
     181             :                         {
     182             :                                 Id = Guid.NewGuid();
     183             :                                 Rsn = rsn;
     184             :                                 RandomNumber = new Random().Next(0, 99999);
     185             :                         }
     186             :                 }
     187             :         }
     188             : }

Generated by: LCOV version 1.12