From 1065a98400842d3646591f74136f29112ac2f232 Mon Sep 17 00:00:00 2001 From: Ben Edwards Date: Fri, 6 Oct 2023 09:43:33 +1100 Subject: [PATCH] Add equivalent stream key tests --- docs/events/projections/ioc.md | 4 +- docs/events/querying.md | 4 +- .../fetching_stream_state.cs | 82 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/docs/events/projections/ioc.md b/docs/events/projections/ioc.md index b9fbe423f1..97bb97412d 100644 --- a/docs/events/projections/ioc.md +++ b/docs/events/projections/ioc.md @@ -52,7 +52,7 @@ public class ProductProjection : CustomProjection } } ``` -snippet source | anchor +snippet source | anchor Now, we *want* to use this projection at runtime within Marten, and need to register the projection @@ -78,7 +78,7 @@ using var host = await Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder() }) .StartAsync(); ``` -snippet source | anchor +snippet source | anchor Note that we're having to explicitly specify the projection lifecycle for the projection used within diff --git a/docs/events/querying.md b/docs/events/querying.md index b8cd0d21c6..c50b866e7f 100644 --- a/docs/events/querying.md +++ b/docs/events/querying.md @@ -177,6 +177,7 @@ public class fetching_stream_state: IntegrationContext { var state = theSession.Events.FetchStreamState(theStreamId); + state.ShouldNotBeNull(); state.Id.ShouldBe(theStreamId); state.Version.ShouldBe(2); state.AggregateType.ShouldBe(typeof(Quest)); @@ -189,6 +190,7 @@ public class fetching_stream_state: IntegrationContext { var state = await theSession.Events.FetchStreamStateAsync(theStreamId); + state.ShouldNotBeNull(); state.Id.ShouldBe(theStreamId); state.Version.ShouldBe(2); state.AggregateType.ShouldBe(typeof(Quest)); @@ -228,7 +230,7 @@ public class fetching_stream_state: IntegrationContext } } ``` -snippet source | anchor +snippet source | anchor Furthermore, `StreamState` contains metadata for when the stream was created, `StreamState.Created`, and when the stream was last updated, `StreamState.LastTimestamp`. diff --git a/src/EventSourcingTests/fetching_stream_state.cs b/src/EventSourcingTests/fetching_stream_state.cs index 0765d55828..099aee1a84 100644 --- a/src/EventSourcingTests/fetching_stream_state.cs +++ b/src/EventSourcingTests/fetching_stream_state.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using EventSourcingTests.Projections; using Marten; +using Marten.Events; using Marten.Schema.Identity; using Marten.Testing.Harness; using Shouldly; @@ -106,6 +107,7 @@ public void can_fetch_the_stream_version_and_aggregate_type() { var state = theSession.Events.FetchStreamState(theStreamId); + state.ShouldNotBeNull(); state.Id.ShouldBe(theStreamId); state.Version.ShouldBe(2); state.AggregateType.ShouldBe(typeof(Quest)); @@ -118,6 +120,7 @@ public async Task can_fetch_the_stream_version_and_aggregate_type_async() { var state = await theSession.Events.FetchStreamStateAsync(theStreamId); + state.ShouldNotBeNull(); state.Id.ShouldBe(theStreamId); state.Version.ShouldBe(2); state.AggregateType.ShouldBe(typeof(Quest)); @@ -158,3 +161,82 @@ public async Task can_fetch_the_stream_events_through_batch_query() } #endregion + +public class fetching_stream_state_string_id: IntegrationContext +{ + private string theStreamKey; + + public fetching_stream_state_string_id(DefaultStoreFixture fixture) : base(fixture) + { + } + + protected override Task fixtureSetup() + { + UseStreamIdentity(StreamIdentity.AsString); + + var joined = new MembersJoined { Members = new[] { "Rand", "Matt", "Perrin", "Thom" } }; + var departed = new MembersDeparted { Members = new[] { "Thom" } }; + + theStreamKey = Guid.NewGuid().ToString(); + theSession.Events.StartStream(theStreamKey, joined, departed); + return theSession.SaveChangesAsync(); + } + + [Fact] + public void can_fetch_the_stream_version_and_aggregate_type() + { + var state = theSession.Events.FetchStreamState(theStreamKey); + + state.ShouldNotBeNull(); + state.Key.ShouldBe(theStreamKey); + state.Version.ShouldBe(2); + state.AggregateType.ShouldBe(typeof(Quest)); + state.LastTimestamp.ShouldNotBe(DateTimeOffset.MinValue); + state.Created.ShouldNotBe(DateTimeOffset.MinValue); + } + + [Fact] + public async Task can_fetch_the_stream_version_and_aggregate_type_async() + { + var state = await theSession.Events.FetchStreamStateAsync(theStreamKey); + + state.ShouldNotBeNull(); + state.Key.ShouldBe(theStreamKey); + state.Version.ShouldBe(2); + state.AggregateType.ShouldBe(typeof(Quest)); + state.LastTimestamp.ShouldNotBe(DateTimeOffset.MinValue); + state.Created.ShouldNotBe(DateTimeOffset.MinValue); + } + + [Fact] + public async Task can_fetch_the_stream_version_through_batch_query() + { + var batch = theSession.CreateBatchQuery(); + + var stateTask = batch.Events.FetchStreamState(theStreamKey); + + await batch.Execute(); + + var state = await stateTask; + + state.Key.ShouldBe(theStreamKey); + state.Version.ShouldBe(2); + state.AggregateType.ShouldBe(typeof(Quest)); + state.LastTimestamp.ShouldNotBe(DateTimeOffset.MinValue); + } + + [Fact] + public async Task can_fetch_the_stream_events_through_batch_query() + { + var batch = theSession.CreateBatchQuery(); + + var eventsTask = batch.Events.FetchStream(theStreamKey); + + await batch.Execute(); + + var events = await eventsTask; + + events.Count.ShouldBe(2); + } +} +