Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@slovely
Created August 19, 2022 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slovely/ee50e8ffb318d604473ed66c585d17b5 to your computer and use it in GitHub Desktop.
Save slovely/ee50e8ffb318d604473ed66c585d17b5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using Marten;
using Marten.Events.Projections;
using Xunit;
using Xunit.Abstractions;
namespace MartenEventSourceTests;
public class MartenEventStoreTests
{
private readonly ITestOutputHelper _output;
private readonly DocumentStore _theStore;
private Guid _tenancyId = new Guid("00000000-0000-0000-0000-000000000001");
private Guid _landlordId = new Guid("00000000-0000-0000-0000-000000000002");
public MartenEventStoreTests(ITestOutputHelper output)
{
_output = output;
_theStore = DocumentStore.For(opts =>
{
opts.Connection("Host=localhost;Port=5433;Database=martentest;Username=postgres;Password=*****");
opts.Projections.SelfAggregate<Tenancy>(ProjectionLifecycle.Inline);
});
}
[Fact]
public async Task ClearDatabase()
{
await _theStore.Advanced.ResetAllData();
}
[Fact]
public async Task CreateNewTenancy()
{
var session = _theStore.OpenSession();
session.Events.StartStream<Tenancy>(_tenancyId, new NewTenancy(DateTime.Today.AddDays(-3), "1 High Street"));
await session.SaveChangesAsync();
}
[Fact]
public async Task InviteLandlord()
{
var session = _theStore.OpenSession();
var stream = await session.Events.FetchForWriting<Tenancy>(_tenancyId);
stream.AppendOne(new LandlordAdded(_landlordId));
await session.SaveChangesAsync();
}
[Fact]
public async Task GetTenancy()
{
var session = _theStore.LightweightSession();
var tenancy = session.Load<Tenancy>(_tenancyId);
_output.WriteLine("Tenancy Start Date: " + tenancy.StartDate);
_output.WriteLine("Tenancy Address: " + tenancy.PropertyAddress);
_output.WriteLine("Number of LLs: " + tenancy.LandlordIds.Count());
}
}
public record NewTenancy(DateTime StartDate, string PropertyAddress);
public record LandlordAdded(Guid LandlordId);
public class Tenancy
{
public Guid Id { get; private set; }
public DateTime StartDate { get; private set; }
public string PropertyAddress { get; private set; }
public IEnumerable<Guid> LandlordIds { get; private set; } = new List<Guid>();
public static async Task<Tenancy> Create(NewTenancy @event, IQuerySession session)
{
var t = new Tenancy();
t.StartDate = @event.StartDate;
t.PropertyAddress = @event.PropertyAddress;
return t;
}
public void Apply(LandlordAdded @event)
{
((List<Guid>)LandlordIds).Add(@event.LandlordId);
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Marten" Version="5.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
@slovely
Copy link
Author

slovely commented Aug 22, 2022

Completely throwaway code to check understanding.

Repro details:
1 - Run ClearDatabase() and then CreateNewTenancy() unit tests. This will create a new event stream, and update the SelfAggregate projection.

2 - select * from mt_doc_tenancy; will then return:
image

3 - Now run InviteLandlord unit test, which creates a new event on the same stream.

4 - select * from mt_doc_tenancy; will then return:
image

So the projection has been updated with a Landlord ID, but the start date/property address (set by the first event) have been reset to default. It's like the projection is only applying the latest event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment