Skip to content

Instantly share code, notes, and snippets.

@trailmax
Last active August 29, 2015 14:10
Show Gist options
  • Save trailmax/7c97ac7bdfa68c8613e0 to your computer and use it in GitHub Desktop.
Save trailmax/7c97ac7bdfa68c8613e0 to your computer and use it in GitHub Desktop.
Bugreport for NEventStore.EventStoreRepository
using System;
using CommonDomain.Core;
using CommonDomain.Persistence.EventStore;
using FluentAssertions;
using NEventStore;
using Xunit;
public class NEventStoreRepositoryTests
{
[Fact]
public void Repository_DoesNot_Return_CorrectVersions()
{
// Arrange
var sut = new EventStoreRepository(WireUpStore(), new AggregateFactory(), new ConflictDetector());
var id = CreateAggregate(sut);
// Act
var firstVersion = sut.GetById<Account>(id, 1);
// Assert
firstVersion.Name.Should().Be("First"); // <<-- Fails here. Actual value of Name is "Third"
}
[Fact]
public void Repository_AfterDisposing_DoesNot_Return_CorrectVersions()
{
// Arrange
var eventStore = WireUpStore();
var sut = new EventStoreRepository(eventStore, new AggregateFactory(), new ConflictDetector());
var id = CreateAggregate(sut);
sut.Dispose(); // disposing just to check if it works
sut = new EventStoreRepository(eventStore, new AggregateFactory(), new ConflictDetector());
// Act
var firstVersion = sut.GetById<Account>(id, 1);
var secondVersion = sut.GetById<Account>(id, 2);
// Assert
firstVersion.Name.Should().Be("First");
secondVersion.Name.Should().Be("Second"); // <<-- Fails here. Actual value of Name is "First"
}
[Fact]
public void When_ApplyingEvents_On_NewInstance_Of_Aggregate_Versions_DontMatch()
{
var id = Guid.NewGuid();
var eventStore = WireUpStore();
// create aggregate
var sut = new MyEventStoreRepository(eventStore, new AggregateFactory(), new ConflictDetector());
var aggregate = new Account(id, "First");
sut.Save(aggregate, Guid.NewGuid());
// another instance of aggregate
var newAggregate = sut.GetById<Account>(id);
newAggregate.ChangeName("Second");
newAggregate.ChangeName("Third");
sut.Save(newAggregate, Guid.NewGuid());
var latestVersion = sut.GetById<Account>(id);
latestVersion.Name.Should().Be("Third"); // <<-- Fails here. Name value is "First"
}
private static Guid CreateAggregate(EventStoreRepository sut)
{
var id = Guid.NewGuid();
var aggregate = new Account(id, "First");
aggregate.ChangeName("Second");
aggregate.ChangeName("Third");
sut.Save(aggregate, Guid.NewGuid(), h => { });
return id;
}
private IStoreEvents WireUpStore()
{
return Wireup.Init()
.UsingInMemoryPersistence()
.InitializeStorageEngine()
.UsingJsonSerialization()
.LogToOutputWindow()
.Build();
}
public class Account : AggregateBase
{
public String Name { get; private set; }
private Account(Guid id)
{
Id = id;
}
public Account(Guid id, string name)
: this(id)
{
RaiseEvent(new ChangeNameEvent(name));
}
public void ChangeName(String newName)
{
RaiseEvent(new ChangeNameEvent(newName));
}
private void Apply(ChangeNameEvent @event)
{
this.Name = @event.Name;
}
}
[Serializable]
public class ChangeNameEvent
{
public ChangeNameEvent(string name)
{
Name = name;
}
public String Name { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment