Fix category shadow foreign keys and .NET 10 Docker build context
Build MoneyMap image / build-and-push (push) Failing after 7s
Build MoneyMap image / build-and-push (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoneyMap.Data;
|
||||
using MoneyMap.Models;
|
||||
|
||||
namespace MoneyMap.Tests.Data;
|
||||
|
||||
public class CategoryModelTests
|
||||
{
|
||||
// Model/query compilation only: never opens a connection or reads application configuration.
|
||||
private static MoneyMapContext CreateContext() => new(
|
||||
new DbContextOptionsBuilder<MoneyMapContext>()
|
||||
.UseSqlServer("Server=localhost;Database=ModelOnly;Integrated Security=true;TrustServerCertificate=true")
|
||||
.Options);
|
||||
|
||||
[Theory]
|
||||
[InlineData(typeof(Transaction))]
|
||||
[InlineData(typeof(CategoryMapping))]
|
||||
public void StringCategoryEntities_DoNotHaveCategoryForeignKeys(Type entityType)
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var entity = context.Model.FindEntityType(entityType)!;
|
||||
|
||||
Assert.Null(entity.FindProperty("CategoryId"));
|
||||
Assert.DoesNotContain(entity.GetProperties(), p => p.IsShadowProperty());
|
||||
Assert.Equal(typeof(string), entity.FindProperty("Category")!.ClrType);
|
||||
Assert.DoesNotContain(entity.GetForeignKeys(), fk => fk.PrincipalEntityType.ClrType == typeof(Category));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public void ReceiptDuplicateQueries_DoNotSelectCategoryId(bool matchHash)
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var query = context.Receipts.Include(r => r.Transaction);
|
||||
var sql = (matchHash
|
||||
? query.Where(r => r.FileHashSha256 == "test-hash")
|
||||
: query.Where(r => r.FileName == "receipt.png" && r.FileSizeBytes == 8))
|
||||
.ToQueryString();
|
||||
|
||||
Assert.Contains("LEFT JOIN [Transactions]", sql);
|
||||
Assert.Contains("[Category]", sql);
|
||||
Assert.DoesNotContain("[CategoryId]", sql);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CategoryMappingQuery_DoesNotSelectCategoryId()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var sql = context.CategoryMappings.ToQueryString();
|
||||
Assert.Contains("[Category]", sql);
|
||||
Assert.DoesNotContain("[CategoryId]", sql);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IntendedCategoryRelationships_ArePreserved()
|
||||
{
|
||||
using var context = CreateContext();
|
||||
var recurringPlace = context.Model.FindEntityType(typeof(RecurringPlace))!;
|
||||
Assert.Contains(recurringPlace.GetForeignKeys(), fk =>
|
||||
fk.PrincipalEntityType.ClrType == typeof(Category) &&
|
||||
fk.Properties.Single().Name == "CategoryId" && !fk.Properties.Single().IsShadowProperty());
|
||||
var category = context.Model.FindEntityType(typeof(Category))!;
|
||||
Assert.Contains(category.GetForeignKeys(), fk =>
|
||||
fk.PrincipalEntityType.ClrType == typeof(Category) &&
|
||||
fk.Properties.Single().Name == "ParentCategoryId");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user