Skip to content

Commit

Permalink
Bug/28 (#32)
Browse files Browse the repository at this point in the history
* bug resolved with unit tests

* bug #28 et #30 resolved
  • Loading branch information
lonesomegeek committed Sep 18, 2017
1 parent 04d1e1b commit 1b2e54b
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 8 deletions.
25 changes: 25 additions & 0 deletions LSG.GenericCrud.Tests/LSG.GenericCrud.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170628-02" />
<PackageReference Include="Moq" Version="4.7.99" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\LSG.GenericCrud\LSG.GenericCrud.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions LSG.GenericCrud.Tests/TestContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using LSG.GenericCrud.Models;
using LSG.GenericCrud.Repositories;
using Microsoft.EntityFrameworkCore;

namespace LSG.GenericCrud.Tests
{
public class TestContext : BaseDbContext, IDbContext
{
public TestContext
(DbContextOptions options, IServiceProvider serviceProvider) : base(options, serviceProvider)
{
}

public DbSet<TestEntity> TestEntities { get; set; }
public DbSet<HistoricalEvent> HistoricalEvents { get; set; }
}
}
11 changes: 11 additions & 0 deletions LSG.GenericCrud.Tests/TestEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using LSG.GenericCrud.Models;

namespace LSG.GenericCrud.Tests
{
public class TestEntity : IEntity
{
public Guid Id { get; set; }
public string Value { get; set; }
}
}
77 changes: 77 additions & 0 deletions LSG.GenericCrud.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LSG.GenericCrud.Models;
using LSG.GenericCrud.Repositories;
using LSG.GenericCrud.Repositories.DataFillers;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace LSG.GenericCrud.Tests
{
public class UnitTest1
{
[Fact]
public void UpdateEntityWithNullValue_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);

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()
{
var originalEntity = new TestEntity() {Id = Guid.NewGuid()}; // create a test entity with a null value
var modifiedEntity = new TestEntity() {Id = originalEntity.Id, Value = "New Value"};

var changeset = originalEntity.DetailedCompare(modifiedEntity);

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


}
}
8 changes: 7 additions & 1 deletion LSG.GenericCrud.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LSG.GenericCrud.TestApi", "
EndProject
Project("{00D1A9C2-B5F0-4AF3-8072-F6C62B433612}") = "LSG.GenericCrud.TestApi.Database", "LSG.GenericCrud.TestApi.Database\LSG.GenericCrud.TestApi.Database.sqlproj", "{2A420A47-95C4-44A0-993F-15100667A2C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSG.GenericCrud", "LSG.GenericCrud\LSG.GenericCrud.csproj", "{722007F7-B2C5-40E3-B6A8-57AD5A9745F8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LSG.GenericCrud", "LSG.GenericCrud\LSG.GenericCrud.csproj", "{722007F7-B2C5-40E3-B6A8-57AD5A9745F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LSG.GenericCrud.Tests", "LSG.GenericCrud.Tests\LSG.GenericCrud.Tests.csproj", "{0359BCEA-7881-488F-83CC-27C3CE94DC30}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -29,6 +31,10 @@ Global
{722007F7-B2C5-40E3-B6A8-57AD5A9745F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{722007F7-B2C5-40E3-B6A8-57AD5A9745F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{722007F7-B2C5-40E3-B6A8-57AD5A9745F8}.Release|Any CPU.Build.0 = Release|Any CPU
{0359BCEA-7881-488F-83CC-27C3CE94DC30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0359BCEA-7881-488F-83CC-27C3CE94DC30}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0359BCEA-7881-488F-83CC-27C3CE94DC30}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0359BCEA-7881-488F-83CC-27C3CE94DC30}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
14 changes: 8 additions & 6 deletions LSG.GenericCrud/Repositories/BaseDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class BaseDbContext : DbContext
public BaseDbContext(DbContextOptions options, IServiceProvider serviceProvider) : base(options)
{
_serviceProvider = serviceProvider;
_dataFillers = _serviceProvider.GetServices<IEntityDataFiller<BaseEntity>>();
_dataFillers = _serviceProvider?.GetServices<IEntityDataFiller<BaseEntity>>();

}

Expand All @@ -47,15 +47,17 @@ public BaseDbContext(DbContextOptions options, IServiceProvider serviceProvider)
/// </remarks>
public override int SaveChanges()
{
var entries = ChangeTracker.Entries();
foreach (var entry in entries)
if (_dataFillers != null)
{
foreach (var dataFiller in _dataFillers)
var entries = ChangeTracker.Entries();
foreach (var entry in entries)
{
if (dataFiller.IsEntitySupported(entry)) dataFiller.Fill(entry);
foreach (var dataFiller in _dataFillers)
{
if (dataFiller.IsEntitySupported(entry)) dataFiller.Fill(entry);
}
}
}

return base.SaveChanges();
}
}
Expand Down
5 changes: 4 additions & 1 deletion 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 Expand Up @@ -86,7 +89,7 @@ public override void Update(Guid id, T entity)
{
var oldValue = prop.GetValue(originalEntity, null);
var newValue = prop.GetValue(entity, null);
if (!oldValue.Equals(newValue))
if (oldValue == null || !oldValue.Equals(newValue))
{
var originalProperty = originalEntity.GetType().GetProperty(prop.Name);
var value = prop.GetValue(entity, null);
Expand Down

0 comments on commit 1b2e54b

Please sign in to comment.