Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow in memory store to delete streams and clear #20

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Odyssey/InMemoryEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,22 @@ public Task<IReadOnlyCollection<EventData>> ReadStream(string streamId, ReadDire

return Task.FromResult<OneOf<EventData, NotFound>>(stream[(int)eventNumber]);
}

/// <summary>
/// Deletes the stream with the specified stream identifier
/// </summary>
/// <param name="streamId"></param>
/// <returns></returns>
public bool DeleteStream(string streamId)
{
return _streams.TryRemove(streamId, out _);
}

/// <summary>
/// Resets the entire in-memory store, clearing all streams
/// </summary>
public void Reset()
{
_streams.Clear();
}
}
14 changes: 14 additions & 0 deletions test/Odyssey.Tests/InMemoryEventStoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(TEvent @event)
=> new(Guid.NewGuid(), @event!.GetType().Name, @event);

Expand Down