Skip to content

Commit

Permalink
bug #28 et #30 resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
lonesomegeek committed Sep 18, 2017
1 parent e0b3b23 commit a46a3aa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
38 changes: 37 additions & 1 deletion LSG.GenericCrud.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,49 @@ public class UnitTest1
public void UpdateEntityWithNullValue_Success()
{
var entity = new TestEntity() {Id = Guid.NewGuid()};
var options = new DbContextOptionsBuilder<TestContext>().UseInMemoryDatabase(databaseName: "tests").Options;
var options = new DbContextOptionsBuilder<TestContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
var context = new TestContext(options, null);
var dal = new HistoricalCrud<TestEntity>(context);
dal.Create(entity);

dal.Update(entity.Id, entity);

// assert for create and update event
Assert.Equal(2, context.HistoricalEvents.Count());
}
[Fact]
public void CreateEntityWithId_InitializeIdInDal_Success()
{
var entity = new TestEntity() { Id = Guid.NewGuid() };
var options = new DbContextOptionsBuilder<TestContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
var context = new TestContext(options, null);
var dal = new HistoricalCrud<TestEntity>(context);

dal.Create(entity);

// assert for create and update event
Assert.Equal(1, context.HistoricalEvents.Count());
Assert.Equal(1, context.TestEntities.Count());
Assert.Equal(entity.Id ,context.HistoricalEvents.First().EntityId);
Assert.Equal(context.TestEntities.First().Id, context.HistoricalEvents.First().EntityId);
}

[Fact]
public void CreateEntityWithoutIdInitializer_InitializeIdInDal_Success()
{
var entity = new TestEntity();
var options = new DbContextOptionsBuilder<TestContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
var context = new TestContext(options, null);
var dal = new HistoricalCrud<TestEntity>(context);

var createdEntity = dal.Create(entity);

// assert for create and update event
Assert.Equal(1, context.HistoricalEvents.Count());
Assert.Equal(1, context.TestEntities.Count());
Assert.Equal(createdEntity.Id, context.HistoricalEvents.First().EntityId);
Assert.Equal(context.TestEntities.First().Id, context.HistoricalEvents.First().EntityId);
}

[Fact]
public void CreateChangesetWithNullValue_Success()
Expand All @@ -37,5 +71,7 @@ public void CreateChangesetWithNullValue_Success()

Assert.Contains("New Value", changeset);
}


}
}
3 changes: 3 additions & 0 deletions LSG.GenericCrud/Repositories/HistoricalCrud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public HistoricalCrud(IDbContext context) : base(context)
/// <returns></returns>
public override T Create(T entity)
{
// check for uninitialized id
if (entity.Id == Guid.Empty) entity.Id = Guid.NewGuid();

var historicalEvent = new HistoricalEvent
{
Action = HistoricalActions.Create.ToString(),
Expand Down

0 comments on commit a46a3aa

Please sign in to comment.