-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from e13tech/db
Various Db enhancements
- Loading branch information
Showing
21 changed files
with
569 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace E13.Common.Domain | ||
{ | ||
public interface ICreatable : IEntity | ||
{ | ||
string CreatedBy { get; set; } | ||
string CreatedSource { get; set; } | ||
DateTime Created { 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace E13.Common.Domain | ||
{ | ||
public interface IModifiable : IEntity | ||
{ | ||
string ModifiedBy { get; set; } | ||
string ModifiedSource { get; set; } | ||
DateTime? Modified { 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,51 @@ | ||
using E13.Common.Data.Db.Tests.Sample; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace E13.Common.Data.Db.Tests | ||
{ | ||
public class BaseDbContextTests | ||
{ | ||
private TestDbContext Context; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase($"{Guid.NewGuid()}")); | ||
|
||
Context = services.BuildServiceProvider().GetService<TestDbContext>(); | ||
Context.AddTestData(); | ||
} | ||
|
||
/// <summary> | ||
/// InMemory Data should initialize to a single entry in every table | ||
/// </summary> | ||
[Test] | ||
public void InMemory_Baseline_OnePerTable() | ||
{ | ||
Context.Creatables.Count().Should().Be(1); | ||
Context.Modifiables.Count().Should().Be(1); | ||
Context.Deletable.Count().Should().Be(1); | ||
Context.Effectable.Count().Should().Be(1); | ||
Context.Ownable.Count().Should().Be(1); | ||
} | ||
|
||
/// <summary> | ||
/// InMemory Data should initialize with a non-empty guid for the Id | ||
/// </summary> | ||
[Test] | ||
public void InMemory_Baseline_EmptyGuids() | ||
{ | ||
Context.Creatables.All(e => e.Id == Guid.Empty).Should().BeFalse(); | ||
Context.Modifiables.All(e => e.Id == Guid.Empty).Should().BeFalse(); | ||
Context.Deletable.All(e => e.Id == Guid.Empty).Should().BeFalse(); | ||
Context.Effectable.All(e => e.Id == Guid.Empty).Should().BeFalse(); | ||
Context.Ownable.All(e => e.Id == Guid.Empty).Should().BeFalse(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
test/E13.Common.Data.Db.Tests/BaseDbContext_ICreatableTests.cs
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,64 @@ | ||
using E13.Common.Data.Db.Tests.Sample; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace E13.Common.Data.Db.Tests | ||
{ | ||
public class BaseDbContext_ICreatableTests | ||
{ | ||
private TestDbContext Context; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase($"{Guid.NewGuid()}")); | ||
|
||
Context = services.BuildServiceProvider().GetService<TestDbContext>(); | ||
Context.AddTestData(); | ||
} | ||
[Test] | ||
public void InitialData_CreatedSource_AddTestData() | ||
{ | ||
var arranged = Context.Creatables.First(); | ||
|
||
arranged.CreatedSource.Should().Be("E13.Common.Data.Db.Tests.Sample.TestDbContext.AddTestData"); | ||
} | ||
|
||
[Test] | ||
public void InitialData_CreatedBy_Unknown() | ||
{ | ||
var arranged = Context.Creatables.First(); | ||
|
||
arranged.CreatedBy.Should().Be(BaseDbContext.UnknownUser); | ||
} | ||
|
||
[Test] | ||
public void SaveChanges_UnknownUser_CreatedByUnknown() | ||
{ | ||
var id = Guid.NewGuid(); | ||
Context.Creatables.Add(new TestCreatable { Id = id }); | ||
Context.SaveChanges(); | ||
|
||
var arranged = Context.Creatables.First(e => e.Id == id); | ||
|
||
arranged.CreatedBy.Should().Be(BaseDbContext.UnknownUser); | ||
} | ||
|
||
[Test] | ||
public void SaveChanges_NamedUser_CreatedByNamedUser() | ||
{ | ||
var id = Guid.NewGuid(); | ||
Context.Creatables.Add(new TestCreatable { Id = id }); | ||
Context.SaveChanges(nameof(SaveChanges_NamedUser_CreatedByNamedUser)); | ||
|
||
var arranged = Context.Creatables.First(e => e.Id == id); | ||
|
||
arranged.CreatedBy.Should().Be(nameof(SaveChanges_NamedUser_CreatedByNamedUser)); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
test/E13.Common.Data.Db.Tests/BaseDbContext_IDeletableTests.cs
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,57 @@ | ||
using E13.Common.Data.Db.Tests.Sample; | ||
using FluentAssertions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace E13.Common.Data.Db.Tests | ||
{ | ||
public class BaseDbContext_IDeletableTests | ||
{ | ||
private TestDbContext Context; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
var services = new ServiceCollection(); | ||
services.AddDbContext<TestDbContext>(o => o.UseInMemoryDatabase($"{Guid.NewGuid()}")); | ||
|
||
Context = services.BuildServiceProvider().GetService<TestDbContext>(); | ||
Context.AddTestData(); | ||
} | ||
|
||
[Test] | ||
public void SaveChanges_Deleting_SetsDeleted() | ||
{ | ||
Context.Deletable.Count().Should().Be(1); | ||
Context.Deletable.Count(e => e.Deleted == null).Should().Be(1); | ||
var arranged = Context.Deletable.First(); | ||
|
||
Context.Deletable.Remove(arranged); | ||
Context.SaveChanges(); | ||
var a = Context.Deletable.Count(); | ||
|
||
Context.Deletable.Count().Should().Be(1); | ||
Context.Deletable.Count(e => e.Deleted == null).Should().Be(0); | ||
Context.Deletable.Count(e => e.Deleted != null).Should().Be(1); | ||
} | ||
|
||
[Test] | ||
public void SaveChangesForUser_Deleting_SetsDeletedBy() | ||
{ | ||
Context.Deletable.Count().Should().Be(1); | ||
Context.Deletable.Count(e => e.DeletedBy == null).Should().Be(1); | ||
var arranged = Context.Deletable.First(); | ||
|
||
Context.Deletable.Remove(arranged); | ||
Context.SaveChanges(nameof(SaveChangesForUser_Deleting_SetsDeletedBy)); | ||
|
||
Context.Deletable.Count().Should().Be(1); | ||
Context.Deletable.Count(e => e.DeletedBy == null).Should().Be(0); | ||
Context.Deletable.Count(e => e.DeletedBy == nameof(SaveChangesForUser_Deleting_SetsDeletedBy)).Should().Be(1); | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.