Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Mar 1, 2024
1 parent 62e5de8 commit 057b789
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 20 deletions.
2 changes: 1 addition & 1 deletion SqliteTimestamp/SqliteTimestampModelCustomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Customize(ModelBuilder modelBuilder, DbContext context)
// No [Timestamp] column found.
continue;
}
property.SetColumnType("INTEGER");
property.SetProviderClrType(typeof(long));
property.SetDefaultValueSql("0");
property.SetValueConverter(TimestampToLongConverter.Singleton);
property.SetValueComparer(new ArrayStructuralComparer<byte>());
Expand Down
File renamed without changes.
19 changes: 0 additions & 19 deletions UnitTests/Api_Tests.cs

This file was deleted.

34 changes: 34 additions & 0 deletions UnitTests/MemoryDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

namespace UnitTests;

sealed class MemoryDbContext : DbContext
{
public const string TestTableName = "TestTable";
public const string IdName = "Id";
public const string RowVersionName = "RowVersion";

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlite("DataSource=:memory:")
.UseSqliteTimestamp()
.LogTo(Console.WriteLine);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity(TestTableName, b =>
{
b.Property<long>(IdName)
.ValueGeneratedOnAdd();
b.Property<byte[]>(RowVersionName)
.IsRowVersion();
b.HasKey(IdName);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

namespace UnitTests;

[TestClass]
sealed class SqliteTimestampDbContextOptionsBuilderExtensions_Tests
{
sealed class TestDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqliteTimestamp();
}
}

[TestMethod]
public void UseSqliteTimestamp()
{
using var db = new TestDbContext();
}
}
20 changes: 20 additions & 0 deletions UnitTests/SqliteTimestampModelCustomizer_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

namespace UnitTests;

[TestClass]
sealed class SqliteTimestampModelCustomizer_Tests
{
[TestMethod]
public void SqliteTimestampModelCustomizer_Customize()
{
using var db = new MemoryDbContext();

var TestTable = db.Model.FindEntityType(MemoryDbContext.TestTableName)!;
var RowVersion = TestTable.FindProperty(MemoryDbContext.RowVersionName)!;

Assert.AreEqual(typeof(long), RowVersion.GetProviderClrType());
}
}
38 changes: 38 additions & 0 deletions UnitTests/TimestampToLongConverter_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
//
// SPDX-License-Identifier: MIT

using Dorssel.EntityFrameworkCore.Storage.ValueConversion;

namespace UnitTests;

[TestClass]
sealed class TimestampToLongConverter_Tests
{
static IEnumerable<object[]> KnownGoodConversions
{
get => [
[0, new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 }],
[1, new byte[] { 0, 0, 0, 0, 0, 0, 0, 1 }],
[long.MaxValue, new byte[] { 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }],
[-1, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }],
[long.MinValue, new byte[] { 0x80, 0, 0, 0, 0, 0, 0, 0 }],
];
}

[TestMethod]
[DynamicData(nameof(KnownGoodConversions))]
public void TimestampToLongConverter_ConvertToProviderTyped(long expectedLong, byte[] expectedTimestamp)
{
var result = TimestampToLongConverter.Singleton.ConvertToProviderTyped(expectedTimestamp);
Assert.AreEqual(expectedLong, result);
}

[TestMethod]
[DynamicData(nameof(KnownGoodConversions))]
public void TimestampToLongConverter_ConvertFromProviderTyped(long expectedLong, byte[] expectedTimestamp)
{
var result = TimestampToLongConverter.Singleton.ConvertFromProviderTyped(expectedLong);
CollectionAssert.AreEqual(expectedTimestamp, result);
}
}

0 comments on commit 057b789

Please sign in to comment.