Skip to content

Commit

Permalink
Move around some test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Dec 13, 2023
1 parent 3687b0c commit ddf0a12
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 20 deletions.
5 changes: 5 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/IEventIngester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@ namespace NexusMods.EventSourcing.Abstractions;
/// </summary>
public interface IEventIngester
{
/// <summary>
/// Ingests the given event into the context.
/// </summary>
/// <param name="event"></param>
/// <returns></returns>
public ValueTask Ingest(IEvent @event);
}
18 changes: 18 additions & 0 deletions src/NexusMods.EventSourcing.Abstractions/IEventSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace NexusMods.EventSourcing.Abstractions;

public interface IEventSerializer
{
/// <summary>
/// Serializes the given event into a byte array.
/// </summary>
/// <param name="event"></param>
/// <returns></returns>
public byte[] Serialize(IEvent @event);

/// <summary>
/// Deserializes the given byte array into an event.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public IEvent Deserialize(byte[] data);
}
2 changes: 1 addition & 1 deletion src/NexusMods.EventSourcing/EventSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NexusMods.EventSourcing;

public class EventSerializer
public class EventSerializer : IEventSerializer
{
public EventSerializer(IEnumerable<EventDefinition> events)
{
Expand Down
27 changes: 15 additions & 12 deletions tests/NexusMods.EventSourcing.TestModel/Events/AddMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ namespace NexusMods.EventSourcing.TestModel.Events;

[EventId("7DC8F80B-50B6-43B7-B805-43450E9F0C2B")]
[MemoryPackable]
public partial class AddMod : IEvent
public partial record AddMod(string Name, bool Enabled, EntityId<Mod> ModId, EntityId<Loadout> LoadoutId) : IEvent
{
public required string Name { get; init; } = string.Empty;
public required bool Enabled { get; init; } = true;
public required EntityId<Mod> Id { get; init; }
public required EntityId<Loadout> LoadoutId { get; init; }

public async ValueTask Apply<T>(T context) where T : IEventContext
{
context.New(Id);
context.Emit(Id, Mod._name, Name);
context.Emit(Id, Mod._enabled, Enabled);
context.Emit(Id, Mod._loadout, LoadoutId);
context.Emit(LoadoutId, Loadout._mods, Id);
context.New(ModId);
context.Emit(ModId, Mod._name, Name);
context.Emit(ModId, Mod._enabled, Enabled);
context.Emit(ModId, Mod._loadout, LoadoutId);
context.Emit(LoadoutId, Loadout._mods, ModId);
}

public static AddMod Create(string name, EntityId<Loadout> loadoutId) => new() { Name = name, Enabled = true, Id = EntityId<Mod>.NewId(), LoadoutId = loadoutId };
/// <summary>
/// Creates a event that adds a new mod to the given loadout giving it the given name.
/// </summary>
/// <param name="name"></param>
/// <param name="loadoutId"></param>
/// <param name="enabled"></param>
/// <returns></returns>
public static AddMod Create(string name, EntityId<Loadout> loadoutId, bool enabled = true)
=> new(name, enabled, EntityId<Mod>.NewId(), loadoutId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using MemoryPack;
using MemoryPack.Formatters;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.TestModel.Events;

namespace NexusMods.EventSourcing.Tests.Contexts;
namespace NexusMods.EventSourcing.TestModel;

public class InMemoryEventStore(EventSerializer serializer) : IEventStore
public class InMemoryEventStore<TSerializer>(TSerializer serializer) : IEventStore
where TSerializer : IEventSerializer
{
private readonly Dictionary<EntityId,IList<byte[]>> _events = new();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async void CanDeleteEntities()

loadout.Mods.Count().Should().Be(2);

await _ctx.Add(new DeleteMod(modEvent1.Id, loadoutEvent.Id));
await _ctx.Add(new DeleteMod(modEvent1.ModId, loadoutEvent.Id));

loadout.Mods.Count().Should().Be(1);

Expand Down
3 changes: 2 additions & 1 deletion tests/NexusMods.EventSourcing.Tests/Contexts/TestContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.TestModel;

namespace NexusMods.EventSourcing.Tests.Contexts;

public class TestContext(ILogger<TestContext> logger, EventSerializer serializer) : IEntityContext
{
private readonly InMemoryEventStore _store = new(serializer);
private readonly InMemoryEventStore<EventSerializer> _store = new(serializer);
private readonly Dictionary<EntityId, IEntity> _entities = new();
private readonly Dictionary<EntityId, Dictionary<IAttribute, IAccumulator>> _values = new();

Expand Down

0 comments on commit ddf0a12

Please sign in to comment.