Skip to content

Commit d3bf1ce

Browse files
author
Tomas Lycken
authored
Decouple id types (#19)
* Add type argument for stream id * Add tests for querying for events + adjust to genericity * Increment next version
1 parent b563da1 commit d3bf1ce

19 files changed

Lines changed: 126 additions & 75 deletions

File tree

GitVersion.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mode: ContinuousDelivery
2-
next-version: 0.2.0
2+
next-version: 0.3.0
33
branches:
44
master:
55
tag: beta
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using RdbmsEventStore.EntityFramework.Tests.Infrastructure;
4+
using RdbmsEventStore.EntityFramework.Tests.TestData;
5+
using Xunit;
6+
7+
namespace RdbmsEventStore.EntityFramework.Tests.EventStoreTests
8+
{
9+
public class QueryEventsTests : EventStoreTestBase<long, string, LongStringEvent>
10+
{
11+
public QueryEventsTests(EventStoreFixture<long, string, LongStringEvent> fixture, AssemblyInitializerFixture initializer) : base(fixture, initializer)
12+
{
13+
_dbContext.Events.AddRange(_fixture.EventFactory.Create("stream-1", 0,
14+
new FooEvent{Foo = "Foo"},
15+
new BarEvent{Bar = "Bar"},
16+
new FooEvent{Foo = "Baz"}));
17+
_dbContext.Events.AddRange(_fixture.EventFactory.Create("stream-2", 0,
18+
new FooEvent {Foo = "Boo"},
19+
new BarEvent {Bar = "Far"}));
20+
_dbContext.SaveChanges();
21+
}
22+
23+
[Theory]
24+
[InlineData("stream-1", 3)]
25+
[InlineData("stream-2", 2)]
26+
public async Task ReturnsEventsFromCorrectStreamOnly(string streamId, long expectedCount)
27+
{
28+
var store = _fixture.BuildEventStore(_dbContext);
29+
var events = await store.Events(streamId);
30+
Assert.Equal(expectedCount, events.Count());
31+
}
32+
33+
[Theory]
34+
[InlineData("stream-1", 2)]
35+
[InlineData("stream-2", 1)]
36+
public async Task ReturnsEventsAccordingToQuery(string streamId, long expectedCount)
37+
{
38+
var store = _fixture.BuildEventStore(_dbContext);
39+
var events = await store.Events(streamId, es => es.Where(e => e.Type == "FooEvent"));
40+
Assert.Equal(expectedCount, events.Count());
41+
}
42+
43+
}
44+
}

src/RdbmsEventStore.EntityFramework.Tests/EventStoreTests/WriteEventTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace RdbmsEventStore.EntityFramework.Tests.EventStoreTests
1111
{
12-
public class WriteEventTests : EventStoreTestBase
12+
public class WriteEventTests : EventStoreTestBase<Guid, Guid, GuidGuidEvent>
1313
{
14-
public WriteEventTests(EventStoreFixture fixture, AssemblyInitializerFixture initializer) : base(fixture, initializer)
14+
public WriteEventTests(EventStoreFixture<Guid, Guid, GuidGuidEvent> fixture, AssemblyInitializerFixture initializer) : base(fixture, initializer)
1515
{
1616
}
1717

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
using System;
2-
using RdbmsEventStore.EntityFramework.Tests.TestData;
32
using RdbmsEventStore.EventRegistry;
43

54
namespace RdbmsEventStore.EntityFramework.Tests.Infrastructure
65
{
7-
public class EventStoreFixture
6+
public class EventStoreFixture<TId, TStreamId, TEvent>
7+
where TId : IEquatable<TId>
8+
where TStreamId : IEquatable<TStreamId>
9+
where TEvent : Event<TId, TStreamId>, new()
810
{
911
public EventStoreFixture()
1012
{
11-
EventRegistry = new AssemblyEventRegistry(typeof(TestEvent), type => type.Name, type => !type.Name.StartsWith("<>"));
13+
EventRegistry = new AssemblyEventRegistry(typeof(TEvent), type => type.Name, type => !type.Name.StartsWith("<>"));
1214
EventSerializer = new DefaultEventSerializer();
13-
EventFactory = new DefaultEventFactory<Guid, TestEvent>(EventRegistry, EventSerializer);
15+
EventFactory = new DefaultEventFactory<TId, TStreamId, TEvent>(EventRegistry, EventSerializer);
1416
WriteLock = new WriteLock();
1517
}
1618

1719
public IEventRegistry EventRegistry { get; }
1820
public IEventSerializer EventSerializer { get; }
19-
public IEventFactory<Guid, TestEvent> EventFactory { get; }
21+
public DefaultEventFactory<TId, TStreamId, TEvent> EventFactory { get; }
2022
public IWriteLock WriteLock { get; }
2123

22-
public EntityFrameworkEventStore<Guid, EventStoreContext<TestEvent>, TestEvent> BuildEventStore(EventStoreContext<TestEvent> dbContext)
23-
=> new EntityFrameworkEventStore<Guid, EventStoreContext<TestEvent>, TestEvent>(dbContext, EventFactory, WriteLock);
24+
public EntityFrameworkEventStore<TId, TStreamId, EventStoreContext<TEvent>, TEvent> BuildEventStore(EventStoreContext<TEvent> dbContext)
25+
=> new EntityFrameworkEventStore<TId, TStreamId, EventStoreContext<TEvent>, TEvent>(dbContext, EventFactory, WriteLock);
2426
}
2527
}

src/RdbmsEventStore.EntityFramework.Tests/Infrastructure/EventStoreTestBase.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
using System;
2-
using RdbmsEventStore.EntityFramework.Tests.TestData;
32
using Xunit;
43

54
namespace RdbmsEventStore.EntityFramework.Tests.Infrastructure
65
{
76
[Collection(nameof(InMemoryDatabaseCollection))]
8-
public class EventStoreTestBase :
9-
IClassFixture<EventStoreFixture>,
10-
IDisposable
7+
public class EventStoreTestBase<TId, TStreamId, TEvent> : IClassFixture<EventStoreFixture<TId, TStreamId, TEvent>>, IDisposable
8+
where TId : IEquatable<TId>
9+
where TStreamId : IEquatable<TStreamId>
10+
where TEvent : Event<TId, TStreamId>, new()
1111
{
12-
protected readonly EventStoreFixture _fixture;
13-
protected readonly EventStoreContext<TestEvent> _dbContext;
12+
protected readonly EventStoreFixture<TId, TStreamId, TEvent> _fixture;
13+
protected readonly EventStoreContext<TEvent> _dbContext;
1414

15-
public EventStoreTestBase(EventStoreFixture fixture, AssemblyInitializerFixture initializer)
15+
public EventStoreTestBase(EventStoreFixture<TId, TStreamId, TEvent> fixture, AssemblyInitializerFixture initializer)
1616
{
1717
EffortProviderFactory.ResetDb();
1818
_fixture = fixture;
19-
_dbContext = new EventStoreContext<TestEvent>();
19+
_dbContext = new EventStoreContext<TEvent>();
2020
}
2121

2222
public void Dispose()

src/RdbmsEventStore.EntityFramework.Tests/Infrastructure/SmokeTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using Xunit;
1+
using RdbmsEventStore.EntityFramework.Tests.TestData;
2+
using Xunit;
23

34
namespace RdbmsEventStore.EntityFramework.Tests.Infrastructure
45
{
5-
public class SmokeTest : EventStoreTestBase
6+
public class SmokeTest : EventStoreTestBase<long, long, LongLongEvent>
67
{
7-
public SmokeTest(EventStoreFixture fixture, AssemblyInitializerFixture assemblyInitializer)
8+
public SmokeTest(EventStoreFixture<long, long, LongLongEvent> fixture, AssemblyInitializerFixture assemblyInitializer)
89
: base(fixture, assemblyInitializer)
910
{
1011
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace RdbmsEventStore.EntityFramework.Tests.TestData
4+
{
5+
public class GuidGuidEvent : Event<Guid, Guid> { }
6+
public class LongStringEvent : Event<long, string> { }
7+
public class LongLongEvent : Event<long, long> { }
8+
9+
public class FooEvent
10+
{
11+
public string Foo { get; set; }
12+
}
13+
14+
public class BarEvent
15+
{
16+
public string Bar { get; set; }
17+
}
18+
}

src/RdbmsEventStore.EntityFramework.Tests/TestData/FooEvent.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/RdbmsEventStore.EntityFramework.Tests/TestData/TestEvent.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/RdbmsEventStore.EntityFramework/EntityFrameworkEventStore.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,33 @@
66

77
namespace RdbmsEventStore.EntityFramework
88
{
9-
public class EntityFrameworkEventStore<TId, TContext, TEvent> : IEventStore<TId, TEvent>
9+
public class EntityFrameworkEventStore<TId, TStreamId, TContext, TEvent> : IEventStore<TId, TStreamId, TEvent>
1010
where TId : IEquatable<TId>
11+
where TStreamId : IEquatable<TStreamId>
1112
where TContext : DbContext, IEventDbContext<TEvent>
12-
where TEvent : Event<TId>, IEvent<TId>, new()
13+
where TEvent : Event<TId, TStreamId>, IEvent<TId, TStreamId>, new()
1314
{
1415
private readonly TContext context;
15-
private readonly IEventFactory<TId, TEvent> _eventFactory;
16+
private readonly IEventFactory<TId, TStreamId, TEvent> _eventFactory;
1617
private readonly IWriteLock _writeLock;
1718

18-
public EntityFrameworkEventStore(TContext context, IEventFactory<TId, TEvent> eventFactory, IWriteLock writeLock)
19+
public EntityFrameworkEventStore(TContext context, IEventFactory<TId, TStreamId, TEvent> eventFactory, IWriteLock writeLock)
1920
{
2021
this.context = context;
2122
_eventFactory = eventFactory;
2223
_writeLock = writeLock;
2324
}
2425

25-
public Task<IEnumerable<TEvent>> Events(TId streamId) => Events(streamId, query => query);
26+
public Task<IEnumerable<TEvent>> Events(TStreamId streamId) => Events(streamId, query => query);
2627

27-
public async Task<IEnumerable<TEvent>> Events(TId streamId, Func<IQueryable<TEvent>, IQueryable<TEvent>> query)
28+
public async Task<IEnumerable<TEvent>> Events(TStreamId streamId, Func<IQueryable<TEvent>, IQueryable<TEvent>> query)
2829
=> await context.Events
2930
.Where(e => e.StreamId.Equals(streamId))
3031
.Apply(query)
3132
.AsNoTracking()
3233
.ToListAsync();
3334

34-
public async Task Commit(TId streamId, long versionBefore, params object[] payloads)
35+
public async Task Commit(TStreamId streamId, long versionBefore, params object[] payloads)
3536
{
3637
using (await _writeLock.Aquire())
3738
{

0 commit comments

Comments
 (0)