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 A { get; set; } = null!; public DbSet B { get; set; } = null!; } private HopFrameConfig CreateConfig() => new HopFrameConfig { Tables = new List() }; // ------------------------------------------------------------- // AddDbContext - all tables // ------------------------------------------------------------- [Fact] public void AddDbContext_AddsAllDbSets_WhenNoFilterProvided() { var services = new ServiceCollection(); var config = CreateConfig(); var configurator = new HopFrameConfigurator(config, services); configurator.AddDbContext(); 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(); Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository)); Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository)); } // ------------------------------------------------------------- // AddDbContext - filtered tables // ------------------------------------------------------------- [Fact] public void AddDbContext_UsesOnlyIncludedTables_WhenFilterProvided() { var services = new ServiceCollection(); var config = CreateConfig(); var configurator = new HopFrameConfigurator(config, services); configurator.AddDbContext(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(ctx => ctx.A ); Assert.Contains(services, d => d.ServiceType == typeof(EfCoreRepository)); Assert.DoesNotContain(services, d => d.ServiceType == typeof(EfCoreRepository)); } // ------------------------------------------------------------- // Relation detection // ------------------------------------------------------------- private class RelationModel { public TestModelA Single { get; set; } = null!; public List Many { get; set; } = new(); } private class RelationDbContext : DbContext { public DbSet A { get; set; } = null!; public DbSet B { get; set; } = null!; public DbSet R { get; set; } = null!; } [Fact] public void AddDbContext_ChecksRelationsForCreatedTables() { var services = new ServiceCollection(); var config = CreateConfig(); var configurator = new HopFrameConfigurator(config, services); configurator.AddDbContext(); 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(); Assert.Same(configurator, result); } }