-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04d1e1b
commit 1b2e54b
Showing
7 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters