Skip to content

Commit

Permalink
Improve extensibility (#22)
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
Tomas Lycken committed Oct 5, 2017
1 parent d3bf1ce commit 139ab88
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RdbmsEventStore.EntityFramework.Tests.Infrastructure;
using RdbmsEventStore.EntityFramework.Tests.TestData;
using Xunit;

namespace RdbmsEventStore.EntityFramework.Tests.ExtensibilityTests
{
public class NonDefaultEvent : IMutableEvent<long, long>
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public long EventId { get; set; }
[Required]
public long StreamId { get; set; }
[Required]
public DateTimeOffset Timestamp { get; set; }
[Required]
public long Version { get; set; }
[Required]
public string Type { get; set; }
[Required]
public byte[] Payload { get; set; }
}

public class NonDefaultContext : DbContext, IEventDbContext<NonDefaultEvent>
{
public DbSet<NonDefaultEvent> Events { get; set; }
}

[Collection(nameof(InMemoryDatabaseCollection))]
public class NonDefaultImplementationsTests : IClassFixture<EventStoreFixture<long, long, NonDefaultEvent>>, IDisposable
{
private readonly EventStoreFixture<long, long, NonDefaultEvent> _fixture;
private readonly NonDefaultContext _dbContext;

public NonDefaultImplementationsTests(EventStoreFixture<long, long, NonDefaultEvent> fixture)
{
EffortProviderFactory.ResetDb();
_fixture = fixture;
_dbContext = new NonDefaultContext();
}

[Fact]
public async Task CanCommitEventsToStoreWithDefaultImplementations()
{
var store = _fixture.BuildEventStore(_dbContext);

await store.Commit(1, 0, new FooEvent { Foo = "Bar" });
}

[Fact]
public async Task CanReadEventsFromStoreWithNonDefaultImplementations()
{
_dbContext.Events.AddRange(new[]
{
new NonDefaultEvent
{
StreamId = 1,
Timestamp = DateTimeOffset.UtcNow,
Version = 1,
Type = "FooEvent",
Payload =Encoding.UTF8.GetBytes(@"{""Foo"":""Bar""}")
}
});
await _dbContext.SaveChangesAsync();

var store = _fixture.BuildEventStore(_dbContext);

var events = await store.Events(1);

Assert.Equal(1, events.Count());
}
public void Dispose()
{
_dbContext?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Data.Entity;
using RdbmsEventStore.EventRegistry;

namespace RdbmsEventStore.EntityFramework.Tests.Infrastructure
{
public class EventStoreFixture<TId, TStreamId, TEvent>
where TId : IEquatable<TId>
where TStreamId : IEquatable<TStreamId>
where TEvent : Event<TId, TStreamId>, new()
where TEvent : class, IMutableEvent<TId, TStreamId>, new()
{
public EventStoreFixture()
{
Expand All @@ -21,7 +22,8 @@ public EventStoreFixture()
public DefaultEventFactory<TId, TStreamId, TEvent> EventFactory { get; }
public IWriteLock WriteLock { get; }

public EntityFrameworkEventStore<TId, TStreamId, EventStoreContext<TEvent>, TEvent> BuildEventStore(EventStoreContext<TEvent> dbContext)
=> new EntityFrameworkEventStore<TId, TStreamId, EventStoreContext<TEvent>, TEvent>(dbContext, EventFactory, WriteLock);
public EntityFrameworkEventStore<TId, TStreamId, TEventStoreContext, TEvent> BuildEventStore<TEventStoreContext>(TEventStoreContext dbContext)
where TEventStoreContext : DbContext, IEventDbContext<TEvent>
=> new EntityFrameworkEventStore<TId, TStreamId, TEventStoreContext, TEvent>(dbContext, EventFactory, WriteLock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class EntityFrameworkEventStore<TId, TStreamId, TContext, TEvent> : IEven
where TId : IEquatable<TId>
where TStreamId : IEquatable<TStreamId>
where TContext : DbContext, IEventDbContext<TEvent>
where TEvent : Event<TId, TStreamId>, IEvent<TId, TStreamId>, new()
where TEvent : class, IEvent<TId, TStreamId>, new()
{
private readonly TContext context;
private readonly IEventFactory<TId, TStreamId, TEvent> _eventFactory;
Expand Down

0 comments on commit 139ab88

Please sign in to comment.