Added ef core integration
This commit is contained in:
147
tests/HopFrame.Tests.Core/EFCore/DbConfigPopulatorTests.cs
Normal file
147
tests/HopFrame.Tests.Core/EFCore/DbConfigPopulatorTests.cs
Normal file
@@ -0,0 +1,147 @@
|
||||
using HopFrame.Core.Configuration;
|
||||
using HopFrame.Core.EFCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Tests.Core.EFCore;
|
||||
|
||||
public class DbConfigPopulatorTests {
|
||||
private class TestContext { }
|
||||
|
||||
private class TestModel {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
private class OtherModel {
|
||||
public int X { get; set; }
|
||||
}
|
||||
|
||||
private HopFrameConfig CreateConfig(params TableConfig[] tables)
|
||||
=> new HopFrameConfig { Tables = tables.ToList() };
|
||||
|
||||
private TableConfig CreateTable(Type modelType)
|
||||
=> new TableConfig {
|
||||
Identifier = modelType.Name,
|
||||
TableType = modelType,
|
||||
RepositoryType = typeof(object),
|
||||
Route = modelType.Name.ToLower(),
|
||||
DisplayName = modelType.Name,
|
||||
OrderIndex = 0,
|
||||
Properties = new List<PropertyConfig> {
|
||||
new PropertyConfig {
|
||||
Identifier = "Id",
|
||||
DisplayName = "Id",
|
||||
Type = typeof(int),
|
||||
PropertyType = PropertyType.Numeric
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// ConfigureRepository
|
||||
// -------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void ConfigureRepository_RegistersRepositoryType() {
|
||||
var services = new ServiceCollection();
|
||||
var global = CreateConfig();
|
||||
|
||||
var prop = typeof(TestDbContext).GetProperty(nameof(TestDbContext.TestModels))!;
|
||||
|
||||
var identifier = DbConfigPopulator.ConfigureRepository(global, services, typeof(TestDbContext), prop);
|
||||
|
||||
var repoType = typeof(EfCoreRepository<TestModel, TestDbContext>);
|
||||
|
||||
Assert.Contains(services, d => d.ServiceType == repoType);
|
||||
Assert.Single(global.Tables);
|
||||
Assert.Equal(identifier, global.Tables[0].Identifier);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ConfigureRepository_AddsTableToGlobalConfig() {
|
||||
var services = new ServiceCollection();
|
||||
var global = CreateConfig();
|
||||
|
||||
var prop = typeof(TestDbContext).GetProperty(nameof(TestDbContext.TestModels))!;
|
||||
|
||||
DbConfigPopulator.ConfigureRepository(global, services, typeof(TestDbContext), prop);
|
||||
|
||||
Assert.Single(global.Tables);
|
||||
Assert.Equal(typeof(TestModel), global.Tables[0].TableType);
|
||||
}
|
||||
|
||||
private class TestDbContext : DbContext {
|
||||
public List<TestModel> TestModels { get; set; } = new();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// CheckForRelations
|
||||
// -------------------------------------------------------------
|
||||
|
||||
private class RelationModel {
|
||||
public OtherModel Single { get; set; } = new();
|
||||
public List<OtherModel> Many { get; set; } = new();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckForRelations_SetsRelationFlag_ForSingleReference() {
|
||||
var otherTable = CreateTable(typeof(OtherModel));
|
||||
var relationTable = CreateTable(typeof(RelationModel));
|
||||
|
||||
relationTable.Properties = new List<PropertyConfig> {
|
||||
new PropertyConfig {
|
||||
Identifier = "Single",
|
||||
DisplayName = "Single",
|
||||
Type = typeof(OtherModel),
|
||||
PropertyType = PropertyType.Text
|
||||
}
|
||||
};
|
||||
|
||||
var global = CreateConfig(otherTable, relationTable);
|
||||
|
||||
DbConfigPopulator.CheckForRelations(global, relationTable);
|
||||
|
||||
Assert.True((relationTable.Properties[0].PropertyType & PropertyType.Relation) != 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckForRelations_SetsRelationFlag_ForListReference() {
|
||||
var otherTable = CreateTable(typeof(OtherModel));
|
||||
var relationTable = CreateTable(typeof(RelationModel));
|
||||
|
||||
relationTable.Properties = new List<PropertyConfig> {
|
||||
new PropertyConfig {
|
||||
Identifier = "Many",
|
||||
DisplayName = "Many",
|
||||
Type = typeof(List<OtherModel>),
|
||||
PropertyType = PropertyType.List
|
||||
}
|
||||
};
|
||||
|
||||
var global = CreateConfig(otherTable, relationTable);
|
||||
|
||||
DbConfigPopulator.CheckForRelations(global, relationTable);
|
||||
|
||||
Assert.True((relationTable.Properties[0].PropertyType & PropertyType.Relation) != 0);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CheckForRelations_DoesNotSetRelationFlag_WhenNoMatchingTableExists() {
|
||||
var relationTable = CreateTable(typeof(RelationModel));
|
||||
|
||||
relationTable.Properties = new List<PropertyConfig> {
|
||||
new PropertyConfig {
|
||||
Identifier = "Single",
|
||||
DisplayName = "Single",
|
||||
Type = typeof(OtherModel),
|
||||
PropertyType = PropertyType.Text
|
||||
}
|
||||
};
|
||||
|
||||
var global = CreateConfig(relationTable);
|
||||
|
||||
DbConfigPopulator.CheckForRelations(global, relationTable);
|
||||
|
||||
Assert.False((relationTable.Properties[0].PropertyType & PropertyType.Relation) != 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
using HopFrame.Core.Configuration;
|
||||
using HopFrame.Core.Configurators;
|
||||
using HopFrame.Core.EFCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace HopFrame.Tests.Core.EFCore;
|
||||
|
||||
public class HopFrameConfiguratorExtensionsTests {
|
||||
private class TestModelA {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
private class TestModelB {
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
private class TestDbContext : DbContext {
|
||||
public DbSet<TestModelA> A { get; set; } = null!;
|
||||
public DbSet<TestModelB> B { get; set; } = null!;
|
||||
}
|
||||
|
||||
private HopFrameConfig CreateConfig()
|
||||
=> new HopFrameConfig { Tables = new List<TableConfig>() };
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// AddDbContext - all tables
|
||||
// -------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_AddsAllDbSets_WhenNoFilterProvided() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
configurator.AddDbContext<TestDbContext>();
|
||||
|
||||
Assert.Equal(2, config.Tables.Count);
|
||||
Assert.Contains(config.Tables, t => t.TableType == typeof(TestModelA));
|
||||
Assert.Contains(config.Tables, t => t.TableType == typeof(TestModelB));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_RegistersRepositoriesForAllDbSets() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
configurator.AddDbContext<TestDbContext>();
|
||||
|
||||
Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository<TestModelA, TestDbContext>));
|
||||
Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository<TestModelB, TestDbContext>));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// AddDbContext - filtered tables
|
||||
// -------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_UsesOnlyIncludedTables_WhenFilterProvided() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
configurator.AddDbContext<TestDbContext>(ctx => ctx.A
|
||||
);
|
||||
|
||||
Assert.Single(config.Tables);
|
||||
Assert.Equal(typeof(TestModelA), config.Tables[0].TableType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_RegistersOnlyFilteredRepositories() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
configurator.AddDbContext<TestDbContext>(ctx => ctx.A
|
||||
);
|
||||
|
||||
Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository<TestModelA, TestDbContext>));
|
||||
Assert.DoesNotContain(services, d => d.ServiceType == typeof(EfCoreRepository<TestModelB, TestDbContext>));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Relation detection
|
||||
// -------------------------------------------------------------
|
||||
|
||||
private class RelationModel {
|
||||
public TestModelA Single { get; set; } = null!;
|
||||
public List<TestModelB> Many { get; set; } = new();
|
||||
}
|
||||
|
||||
private class RelationDbContext : DbContext {
|
||||
public DbSet<TestModelA> A { get; set; } = null!;
|
||||
public DbSet<TestModelB> B { get; set; } = null!;
|
||||
public DbSet<RelationModel> R { get; set; } = null!;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_ChecksRelationsForCreatedTables() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
configurator.AddDbContext<RelationDbContext>();
|
||||
|
||||
var relationTable = config.Tables.Single(t => t.TableType == typeof(RelationModel));
|
||||
|
||||
// At least one property should have the Relation flag
|
||||
Assert.Contains(relationTable.Properties, p => (p.PropertyType & PropertyType.Relation) != 0);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Fluent API
|
||||
// -------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public void AddDbContext_ReturnsConfigurator() {
|
||||
var services = new ServiceCollection();
|
||||
var config = CreateConfig();
|
||||
var configurator = new HopFrameConfigurator(config, services);
|
||||
|
||||
var result = configurator.AddDbContext<TestDbContext>();
|
||||
|
||||
Assert.Same(configurator, result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user