From 932722d365abc4c24de7b2f936bd051eeb6ae034 Mon Sep 17 00:00:00 2001 From: Ben Foster Date: Wed, 29 Nov 2023 15:51:06 +0100 Subject: [PATCH] Allow in memory store to delete streams and clear --- src/Odyssey/InMemoryEventStore.cs | 18 ++++++++++++++++++ test/Odyssey.Tests/InMemoryEventStoreTests.cs | 14 ++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Odyssey/InMemoryEventStore.cs b/src/Odyssey/InMemoryEventStore.cs index eeaae9b..710dd16 100644 --- a/src/Odyssey/InMemoryEventStore.cs +++ b/src/Odyssey/InMemoryEventStore.cs @@ -105,4 +105,22 @@ public Task> ReadStreamEvent(string streamId, long ev return Task.FromResult>(stream[(int)eventNumber]); } + + /// + /// Deletes the stream with the specified stream identifier + /// + /// + /// + public bool DeleteStream(string streamId) + { + return _streams.TryRemove(streamId, out _); + } + + /// + /// Resets the entire in-memory store, clearing all streams + /// + public void Reset() + { + _streams.Clear(); + } } \ No newline at end of file diff --git a/test/Odyssey.Tests/InMemoryEventStoreTests.cs b/test/Odyssey.Tests/InMemoryEventStoreTests.cs index f7ee3ba..123eab9 100644 --- a/test/Odyssey.Tests/InMemoryEventStoreTests.cs +++ b/test/Odyssey.Tests/InMemoryEventStoreTests.cs @@ -69,6 +69,20 @@ public async Task Can_read_stream_backwards() events.Last().EventNumber.ShouldBe(0); } + [Fact] + public async Task Can_remove_stream() + { + var streamId = Guid.NewGuid().ToString(); + await _eventStore.AppendToStream(streamId, new[] { Map(new TestEvent()) }, StreamState.NoStream); + + var events = await _eventStore.ReadStream(streamId, ReadDirection.Backwards, StreamPosition.Start); + events.Count().ShouldBe(1); + + _eventStore.DeleteStream(streamId); + events = await _eventStore.ReadStream(streamId, ReadDirection.Backwards, StreamPosition.Start); + events.Count.ShouldBe(0); + } + static EventData Map(TEvent @event) => new(Guid.NewGuid(), @event!.GetType().Name, @event);