Skip to content

Commit

Permalink
Update dependencies (#11)
Browse files Browse the repository at this point in the history
* Remove broken error check from build.sh

* Update third-party dependencies

* Test against latest LTS

* Update core package, implement async members

* Fix event store version setup in tests
  • Loading branch information
rcknight authored Jun 2, 2023
1 parent bbcaa07 commit 5331926
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 134 deletions.
2 changes: 0 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ fi
tag=${tag/tags\//}

dotnet test .\\src\\AggregateRepository.EventStore.Tests\\AggregateRepository.EventStore.Tests.csproj
if %errorlevel% neq 0 exit /b %errorlevel%

dotnet pack .\\src\\AggregateRepository.EventStore\\AggregateRepository.EventStore.csproj -o .\\dist -p:Version="$version" -p:PackageVersion="$version" -p:Tag="$tag" -c Release
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CorshamScience.AggregateRepository.Core" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="CorshamScience.AggregateRepository.Core" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.36" />
<PackageReference Include="Testcontainers" Version="2.1.0" />
<PackageReference Include="Testcontainers" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,38 @@

namespace CorshamScience.AggregateRepository.EventStore.Tests
{
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CorshamScience.AggregateRepository.Core;
using CorshamScience.AggregateRepository.Core.Exceptions;
using NUnit.Framework;

internal abstract class AggregateRepositoryTestFixture
[TestFixture]
public abstract class AggregateRepositoryTestFixture
{
private List<Guid> _storedEvents = new();
private TestAggregate _retrievedAggregate = null!;
private string _aggregateIdUnderTest = null!;
private List<Guid> _storedEvents = new List<Guid>();
private TestAggregate _retrievedAggregate;
private string _aggregateIdUnderTest;

protected IAggregateRepository RepoUnderTest { get; set; } = null!;

[OneTimeSetUp]
public async Task OneTimeSetUp() => await InitRepositoryAsync();
protected IAggregateRepository RepoUnderTest { get; set; }

[SetUp]
public void SetUp()
public async Task SetUp()
{
await InitRepository();
_aggregateIdUnderTest = Guid.NewGuid().ToString();
_storedEvents = new List<Guid>();
}

[OneTimeTearDown]
public async Task OneTimeTearDown() => await CleanUpRepositoryAsync();
[TearDown]
public async Task TearDown() => await CleanUpRepository();

[Test]
public void Retreiving_an_aggregate_from_an_empty_eventstore_should_throw_an_exception() => Assert.Throws<AggregateNotFoundException>(() => RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest));
public void Retreiving_an_aggregate_from_an_empty_eventstore_should_throw_an_exception() => Assert.ThrowsAsync<AggregateNotFoundException>(async () => await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest));

[Test]
public void Retreiving_a_nonexistant_aggregate_id_should_throw_an_exception()
public async Task Retreiving_a_nonexistant_aggregate_id_should_throw_an_exception()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 2; i++)
Expand All @@ -42,27 +45,23 @@ public void Retreiving_a_nonexistant_aggregate_id_should_throw_an_exception()
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
Assert.Throws<AggregateNotFoundException>(() => RepoUnderTest.GetAggregateFromRepository<TestAggregate>(Guid.NewGuid().ToString()));
await RepoUnderTest.SaveAsync(aggregate);
Assert.ThrowsAsync<AggregateNotFoundException>(async () => await RepoUnderTest.GetAggregateAsync<TestAggregate>(Guid.NewGuid().ToString()));
}

[Test]
public void Retrieving_a_newly_created_aggregate_reconstructs_the_entity_correctly()
public async Task Retrieving_a_newly_created_aggregate_reconstructs_the_entity_correctly()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
RepoUnderTest.Save(aggregate);

_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest);
await RepoUnderTest.SaveAsync(aggregate);

Assert.Multiple(() =>
{
Assert.That(_retrievedAggregate.Id, Is.EqualTo(_aggregateIdUnderTest));
Assert.That(_retrievedAggregate.EventsApplied.Count, Is.EqualTo(0));
});
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest);
Assert.AreEqual(_aggregateIdUnderTest, _retrievedAggregate.Id);
Assert.AreEqual(0, _retrievedAggregate.EventsApplied.Count);
}

[Test]
public void Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctly()
public async Task Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctly()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
Expand All @@ -72,23 +71,19 @@ public void Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctl
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest);

Assert.Multiple(() =>
{
Assert.That(_retrievedAggregate.Id, Is.EqualTo(_aggregateIdUnderTest));
Assert.That(_retrievedAggregate.EventsApplied.Count, Is.EqualTo(_storedEvents.Count));
});
await RepoUnderTest.SaveAsync(aggregate);
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest);

Assert.AreEqual(_aggregateIdUnderTest, _retrievedAggregate.Id);
Assert.AreEqual(_storedEvents.Count, _retrievedAggregate.EventsApplied.Count);
foreach (var id in _storedEvents)
{
Assert.That(_retrievedAggregate.EventsApplied, Does.Contain(id));
Assert.Contains(id, _retrievedAggregate.EventsApplied);
}
}

[Test]
public void Retrieving_an_aggregate_with_events_when_specifying_a_version_reconstructs_the_entity_correctly()
public async Task Retrieving_an_aggregate_with_events_when_specifying_a_version_reconstructs_the_entity_correctly()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
Expand All @@ -98,39 +93,19 @@ public void Retrieving_an_aggregate_with_events_when_specifying_a_version_recons
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest, 6);

Assert.Multiple(() =>
{
Assert.That(_retrievedAggregate.Id, Is.EqualTo(_aggregateIdUnderTest));
Assert.That(_retrievedAggregate.EventsApplied.Count, Is.EqualTo(_storedEvents.Count));
});
await RepoUnderTest.SaveAsync(aggregate);
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest, 6);

Assert.AreEqual(_aggregateIdUnderTest, _retrievedAggregate.Id);
Assert.AreEqual(_storedEvents.Count, _retrievedAggregate.EventsApplied.Count);
foreach (var id in _storedEvents)
{
Assert.That(_retrievedAggregate.EventsApplied, Does.Contain(id));
}
}

[Test]
public void Retrieving_an_aggregate_with_events_when_specifying_an_incorrect_version_throws_aggregate_not_found_exception()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
{
var eventId = Guid.NewGuid();
_storedEvents.Add(eventId);
aggregate.GenerateEvent(eventId);
Assert.Contains(id, _retrievedAggregate.EventsApplied);
}

RepoUnderTest.Save(aggregate);

Assert.Throws<AggregateVersionException>(() => RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest, 10));
}

[Test]
public void Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctly_when_the_event_store_contains_multiple_aggregates()
public async Task Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctly_when_the_event_store_contains_multiple_aggregates()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
Expand All @@ -140,55 +115,46 @@ public void Retrieving_an_aggregate_with_events_reconstructs_the_entity_correctl
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
await RepoUnderTest.SaveAsync(aggregate);
var secondAggregate = new TestAggregate(Guid.NewGuid().ToString());
for (var i = 0; i < 6; i++)
{
var eventId = Guid.NewGuid();
secondAggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(secondAggregate);
_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest);

Assert.Multiple(() =>
{
Assert.That(_retrievedAggregate.Id, Is.EqualTo(_aggregateIdUnderTest));
Assert.That(_retrievedAggregate.EventsApplied.Count, Is.EqualTo(_storedEvents.Count));
});
await RepoUnderTest.SaveAsync(secondAggregate);
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest);

Assert.AreEqual(_aggregateIdUnderTest, _retrievedAggregate.Id);
Assert.AreEqual(_storedEvents.Count, _retrievedAggregate.EventsApplied.Count);
foreach (var id in _storedEvents)
{
Assert.That(_retrievedAggregate.EventsApplied, Does.Contain(id));
Assert.Contains(id, _retrievedAggregate.EventsApplied);
}
}

[Test]
public void Saving_new_events_to_an_existing_aggregate_should_correctly_persist_events()
public async Task Saving_new_events_to_an_existing_aggregate_should_correctly_persist_events()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
RepoUnderTest.Save(aggregate);
await RepoUnderTest.SaveAsync(aggregate);

_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest);
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest);

var eventId = Guid.NewGuid();
_retrievedAggregate.GenerateEvent(eventId);

RepoUnderTest.Save(_retrievedAggregate);

var actualAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest);
await RepoUnderTest.SaveAsync(_retrievedAggregate);
var actualAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest);

Assert.Multiple(() =>
{
Assert.That(actualAggregate.EventsApplied.Count, Is.EqualTo(1));
Assert.That(actualAggregate.Id, Is.EqualTo(_aggregateIdUnderTest));
Assert.That(actualAggregate.EventsApplied[0], Is.EqualTo(eventId));
Assert.That(actualAggregate.Version, Is.EqualTo(2));
});
Assert.AreEqual(1, actualAggregate.EventsApplied.Count);
Assert.AreEqual(_aggregateIdUnderTest, actualAggregate.Id);
Assert.AreEqual(eventId, actualAggregate.EventsApplied[0]);
}

[Test]
public void Saving_an_aggregate_with_expected_version_less_than_the_actual_version_should_throw_a_concurrency_exception()
public async Task Saving_an_aggregate_with_expected_version_less_than_the_actual_version_should_throw_a_concurrency_exception()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
Expand All @@ -198,9 +164,9 @@ public void Saving_an_aggregate_with_expected_version_less_than_the_actual_versi
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
await RepoUnderTest.SaveAsync(aggregate);

_retrievedAggregate = RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest, 3);
_retrievedAggregate = await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest, 3);

for (var i = 0; i < 5; i++)
{
Expand All @@ -209,11 +175,11 @@ public void Saving_an_aggregate_with_expected_version_less_than_the_actual_versi
_retrievedAggregate.GenerateEvent(eventId);
}

Assert.Throws<AggregateVersionException>(() => RepoUnderTest.Save(_retrievedAggregate));
Assert.ThrowsAsync<AggregateVersionException>(async () => await RepoUnderTest.SaveAsync(_retrievedAggregate));
}

[Test]
public void Retrieving_an_aggregate_with_expected_version_greater_than_the_actual_version_should_throw_a_concurrency_exception()
public async Task Retrieving_an_aggregate_with_expected_version_greater_than_the_actual_version_should_throw_a_concurrency_exception()
{
var aggregate = new TestAggregate(_aggregateIdUnderTest);
for (var i = 0; i < 5; i++)
Expand All @@ -223,12 +189,12 @@ public void Retrieving_an_aggregate_with_expected_version_greater_than_the_actua
aggregate.GenerateEvent(eventId);
}

RepoUnderTest.Save(aggregate);
Assert.Throws<AggregateVersionException>(() => RepoUnderTest.GetAggregateFromRepository<TestAggregate>(_aggregateIdUnderTest, 10));
await RepoUnderTest.SaveAsync(aggregate);
Assert.ThrowsAsync<AggregateVersionException>(async () => await RepoUnderTest.GetAggregateAsync<TestAggregate>(_aggregateIdUnderTest, 10));
}

protected abstract Task InitRepositoryAsync();
protected abstract Task InitRepository();

protected abstract Task CleanUpRepositoryAsync();
protected abstract Task CleanUpRepository();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@
// Copyright (c) Corsham Science. All rights reserved.
// </copyright>

using System.Runtime.InteropServices;
using Grpc.Core.Interceptors;

namespace CorshamScience.AggregateRepository.EventStore.Tests
{
using CorshamScience.AggregateRepository.EventStore;
using DotNet.Testcontainers.Builders;
using DotNet.Testcontainers.Containers;
using System.Runtime.InteropServices;
using DotNet.Testcontainers.Images;
using DotNet.Testcontainers.Containers;
using global::EventStore.Client;

internal class EventStoreAggregateRepositoryTests : AggregateRepositoryTestFixture
{
private ITestcontainersContainer? _container;
private IContainer? _container;
private EventStoreClient? _client;

protected override async Task InitRepositoryAsync()
protected override async Task InitRepository()
{
const string eventStoreVersion = "21.10.8";
const string eventStoreVersion = "22.10.1";
string imageName = RuntimeInformation.OSArchitecture == Architecture.Arm64
// if on arm (like an m1 mac) use the alpha arm image from github
? $"ghcr.io/eventstore/eventstore:{eventStoreVersion}-alpha-arm64v8"
: $"eventstore/eventstore:{eventStoreVersion}-buster-slim";

const int hostPort = 2113;
_container = new TestcontainersBuilder<TestcontainersContainer>()
const int hostPort = 2113;

_container = new ContainerBuilder()
.WithImage(new DockerImage(imageName))
.WithCleanUp(true)
.WithPortBinding(hostPort)
Expand All @@ -48,7 +47,7 @@ protected override async Task InitRepositoryAsync()
RepoUnderTest = new EventStoreAggregateRepository(_client);
}

protected override async Task CleanUpRepositoryAsync()
protected override async Task CleanUpRepository()
{
if (_container != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CorshamScience.AggregateRepository.Core" Version="2.0.0" />
<PackageReference Include="CorshamScience.AggregateRepository.Core" Version="3.0.0" />
<PackageReference Include="CorshamScience.CodeStyle.CSharp.Full" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="22.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="EventStore.Client.Grpc.Streams" Version="23.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 5331926

Please sign in to comment.