Skip to content

Commit

Permalink
adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ascott18 committed Oct 17, 2023
1 parent a0bc058 commit 0379643
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 134 deletions.
155 changes: 21 additions & 134 deletions src/IntelliTect.Coalesce.AuditLogging.Tests/AuditTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
using Z.EntityFramework.Plus;

namespace IntelliTect.Coalesce.AuditLogging.Tests;

public class AuditTests
{
private const string SqlServerConnString = "Server=(localdb)\\MSSQLLocalDB;Database=CoalesceAuditLoggingTests;Trusted_Connection=True;";

public SqliteConnection SqliteConn { get; }

public AuditTests()
Expand All @@ -24,135 +23,16 @@ public AuditTests()
SqliteConn.Open();
}

[Fact]
public async Task WithSqlServer_UpdatesExistingRecordForLikeChanges()
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlServer(SqlServerConnString)
.UseCoalesceAuditLogging<TestObjectChange>()
.Options);

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Act/Assert
user.Name = "bob3";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Assert
var entry = Assert.Single(db.ObjectChanges.Include(c => c.Properties).Where(c => c.State == AuditEntryState.EntityModified));

var propChange = Assert.Single(entry.Properties);
Assert.Equal(nameof(AppUser.Name), propChange.PropertyName);
Assert.Equal("bob", propChange.OldValue);
Assert.Equal("bob3", propChange.NewValue);
}

[Fact]
public async Task WithSqlServer_CreatesNewRecordForLikeChangesOutsideMergeWindow()
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlServer(SqlServerConnString)
.UseCoalesceAuditLogging<TestObjectChange>()
.Options);

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Make the original Updated entry old such that it is outside the merge window.
db.ObjectChanges.First(c => c.State == AuditEntryState.EntityModified).Date -= TimeSpan.FromMinutes(1);
db.SaveChanges();

// Act/Assert
user.Name = "bob3";
await db.SaveChangesAsync();
Assert.Equal(3, db.ObjectChanges.Count()); // Now two records for EntityUpdated
}

[Fact]
public async Task WithSqlServer_CreatesNewRecordForUnlikeChanges()
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlServer(SqlServerConnString)
.UseCoalesceAuditLogging<TestObjectChange>()
.Options);

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Act/Assert
user.Name = "bob3";
user.Title = "Associate";
await db.SaveChangesAsync();

Assert.Equal(3, db.ObjectChanges.Count()); // Now two records for EntityUpdated
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task WithSqlServer_PopulatesContextAndSavesEntries(bool async)
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlServer(SqlServerConnString)
.UseCoalesceAuditLogging<TestObjectChange>(x => x
.WithAugmentation<TestOperationContext>()
)
.Options);

await RunBasicTest(db, async,
expectedCustom1: "from TestOperationContext",
expectedCustom2: null);
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task WithSqlite_PopulatesContextAndSavesEntries(bool async)
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(SqliteConn)
using var db = BuildDbContext(b => b
.UseCoalesceAuditLogging<TestObjectChange>(x => x
.WithAugmentation<TestOperationContext>()
)
.Options);
));

await RunBasicTest(db, async,
expectedCustom1: "from TestOperationContext",
Expand All @@ -165,12 +45,10 @@ await RunBasicTest(db, async,
public async Task SuppressAudit_SuppressesAudit(bool async)
{
// Arrange
using var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(SqliteConn)
.UseCoalesceAuditLogging<TestObjectChange>(x => x
.WithAugmentation<TestOperationContext>()
)
.Options);
using var db = BuildDbContext(b => b
.UseCoalesceAuditLogging((Action<CoalesceAuditLoggingBuilder<TestObjectChange>>?)(x => x
.WithAugmentation<TestOperationContext>())
));

// Act
db.SuppressAudit = true;
Expand All @@ -190,8 +68,7 @@ public void ConfigureAudit_IsCached()
StrongBox<int> calls2 = new(0);
TestDbContext MakeDb(bool excludeUser = true)
{
var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(SqliteConn)
var db = BuildDbContext(b => b
.UseCoalesceAuditLogging<TestObjectChange>(x =>
{
x.WithAugmentation<TestOperationContext>();
Expand All @@ -213,8 +90,7 @@ TestDbContext MakeDb(bool excludeUser = true)
arg.Value++;
x.Exclude<TestObjectChange>();
}, calls2);
})
.Options);
}));
db.Database.EnsureCreated();
return db;
}
Expand Down Expand Up @@ -336,14 +212,25 @@ private WebApplicationBuilder CreateAppBuilder()
return builder;
}

private TestDbContext BuildDbContext(Func<DbContextOptionsBuilder<TestDbContext>, DbContextOptionsBuilder> setup)
{
var builder = new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(SqliteConn);

var db = new TestDbContext(setup(builder).Options);

db.Database.EnsureCreated();

return db;
}

private static async Task RunBasicTest(
TestDbContext db,
bool async,
string? expectedCustom1,
string? expectedCustom2
)
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var user = new AppUser { Name = "bob" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
</ItemGroup>

<ItemGroup>
Expand Down
120 changes: 120 additions & 0 deletions src/IntelliTect.Coalesce.AuditLogging.Tests/SqlServerAuditTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Z.EntityFramework.Plus;

namespace IntelliTect.Coalesce.AuditLogging.Tests;

public class SqlServerAuditTests
{
private const string SqlServerConnString = "Server=(localdb)\\MSSQLLocalDB;Database=CoalesceAuditLoggingTests;Trusted_Connection=True;Timeout=5";

[SkippableFact]
public async Task WithSqlServer_UpdatesExistingRecordForLikeChanges()
{
// Arrange
using TestDbContext db = BuildDbContext();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Act/Assert
user.Name = "bob3";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Assert
var entry = Assert.Single(db.ObjectChanges.Include(c => c.Properties).Where(c => c.State == AuditEntryState.EntityModified));

var propChange = Assert.Single(entry.Properties);
Assert.Equal(nameof(AppUser.Name), propChange.PropertyName);
Assert.Equal("bob", propChange.OldValue);
Assert.Equal("bob3", propChange.NewValue);
}

[SkippableFact]
public async Task WithSqlServer_CreatesNewRecordForLikeChangesOutsideMergeWindow()
{
// Arrange
using TestDbContext db = BuildDbContext();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Make the original Updated entry old such that it is outside the merge window.
db.ObjectChanges.First(c => c.State == AuditEntryState.EntityModified).Date -= TimeSpan.FromMinutes(1);
db.SaveChanges();

// Act/Assert
user.Name = "bob3";
await db.SaveChangesAsync();
Assert.Equal(3, db.ObjectChanges.Count()); // Now two records for EntityUpdated
}

[SkippableFact]
public async Task WithSqlServer_CreatesNewRecordForUnlikeChanges()
{
// Arrange
using TestDbContext db = BuildDbContext();

var user = new AppUser { Name = "bob" };
db.Add(user);
await db.SaveChangesAsync();

// Act/Assert
user.Name = "bob2";
await db.SaveChangesAsync();

Assert.Equal(2, db.ObjectChanges.Count()); // Two records: EntityAdded, and EntityUpdated

// Act/Assert
user.Name = "bob3";
user.Title = "Associate";
await db.SaveChangesAsync();

Assert.Equal(3, db.ObjectChanges.Count()); // Now two records for EntityUpdated
}

private static TestDbContext BuildDbContext()
{
var db = new TestDbContext(new DbContextOptionsBuilder<TestDbContext>()
.UseSqlServer(SqlServerConnString)
.UseCoalesceAuditLogging<TestObjectChange>()
.Options);

try
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
}
catch (SqlException ex) when (
ex.Number == 53
|| ex.Message.Contains("Could not open a connection to SQL Server")
|| ex.Message.Contains("The server was not found or was not accessible")
)
{
Skip.If(true, ex.Message);
}
catch (PlatformNotSupportedException ex) when (
ex.Message.Contains("LocalDB is not supported on this platform")
)
{
Skip.If(true, ex.Message);
}
return db;
}
}

0 comments on commit 0379643

Please sign in to comment.