Added ef core integration
This commit is contained in:
@@ -8,18 +8,23 @@ namespace HopFrame.Tests.Core.Configurators;
|
||||
|
||||
public class HopFrameConfiguratorTests {
|
||||
private class TestRepository : IHopFrameRepository {
|
||||
public Task<IEnumerable> LoadPageGenericAsync(int page, int perPage, CancellationToken ct = default) {
|
||||
public Task<IEnumerable> LoadPageGenericAsync(int page, int perPage, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task<int> CountAsync(CancellationToken ct = default) {
|
||||
public Task<int> CountAsync(CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task<IEnumerable> SearchGenericAsync(string searchTerm, int page, int perPage, CancellationToken ct = default) {
|
||||
public Task<IEnumerable> SearchGenericAsync(string searchTerm, int page, int perPage, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task CreateGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task UpdateGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task DeleteGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -41,7 +46,8 @@ public class HopFrameConfiguratorTests {
|
||||
Identifier = "Id",
|
||||
DisplayName = "Id",
|
||||
Type = typeof(int),
|
||||
OrderIndex = 0
|
||||
OrderIndex = 0,
|
||||
PropertyType = PropertyType.Numeric
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
using HopFrame.Core.Configuration;
|
||||
using HopFrame.Core.Configurators;
|
||||
|
||||
namespace HopFrame.Tests.Core.Configurators;
|
||||
|
||||
public class PropertyConfiguratorTests {
|
||||
private PropertyConfig CreateConfig(PropertyType type)
|
||||
=> new PropertyConfig {
|
||||
Identifier = "Test",
|
||||
DisplayName = "Test",
|
||||
Type = typeof(string),
|
||||
OrderIndex = 0,
|
||||
PropertyType = type
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void SetType_ReplacesBaseType_AndPreservesModifiers() {
|
||||
// Arrange: Nullable + List + Numeric
|
||||
var original = PropertyType.Numeric | PropertyType.Nullable | PropertyType.List;
|
||||
var config = CreateConfig(original);
|
||||
|
||||
var configurator = new PropertyConfigurator(config);
|
||||
|
||||
// Act: change base type to Text
|
||||
configurator.SetType(PropertyType.Text);
|
||||
|
||||
// Assert: modifiers remain, base type replaced
|
||||
Assert.Equal(
|
||||
PropertyType.Text | PropertyType.Nullable | PropertyType.List,
|
||||
config.PropertyType
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetType_DoesNotAffectModifiers_WhenSettingSameBaseType() {
|
||||
var original = PropertyType.Boolean | PropertyType.Nullable;
|
||||
var config = CreateConfig(original);
|
||||
|
||||
var configurator = new PropertyConfigurator(config);
|
||||
|
||||
configurator.SetType(PropertyType.Boolean);
|
||||
|
||||
Assert.Equal(original, config.PropertyType);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetType_CanChangeEnumToNumeric_WhileKeepingModifiers() {
|
||||
var original = PropertyType.Enum | PropertyType.List;
|
||||
var config = CreateConfig(original);
|
||||
|
||||
var configurator = new PropertyConfigurator(config);
|
||||
|
||||
configurator.SetType(PropertyType.Numeric);
|
||||
|
||||
Assert.Equal(
|
||||
PropertyType.Numeric | PropertyType.List,
|
||||
config.PropertyType
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetType_CanChangeToEmail_AndPreserveNullable() {
|
||||
var original = PropertyType.Text | PropertyType.Nullable;
|
||||
var config = CreateConfig(original);
|
||||
|
||||
var configurator = new PropertyConfigurator(config);
|
||||
|
||||
configurator.SetType(PropertyType.Email);
|
||||
|
||||
Assert.Equal(
|
||||
PropertyType.Email | PropertyType.Nullable,
|
||||
config.PropertyType
|
||||
);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SetType_ReturnsConfigurator_ForFluentApi() {
|
||||
var config = CreateConfig(PropertyType.Text);
|
||||
var configurator = new PropertyConfigurator(config);
|
||||
|
||||
var result = configurator.SetType(PropertyType.Numeric);
|
||||
|
||||
Assert.Same(configurator, result);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using System.Reflection;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using HopFrame.Core.Configuration;
|
||||
using HopFrame.Core.Helpers;
|
||||
|
||||
@@ -7,6 +7,28 @@ using HopFrame.Core.Helpers;
|
||||
namespace HopFrame.Tests.Core.Helpers;
|
||||
|
||||
public class ConfigurationHelperTests {
|
||||
private class PropertyTypeModel {
|
||||
public int Number { get; set; }
|
||||
public int? NullableNumber { get; set; }
|
||||
public string Text { get; set; } = "";
|
||||
public bool Flag { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
public DateOnly Date { get; set; }
|
||||
public TimeOnly Time { get; set; }
|
||||
public TestEnum EnumValue { get; set; }
|
||||
public List<int> Numbers { get; set; } = new();
|
||||
public IEnumerable<string> Strings { get; set; } = new List<string>();
|
||||
|
||||
[EmailAddress]
|
||||
public string Email { get; set; } = "";
|
||||
}
|
||||
|
||||
private enum TestEnum {
|
||||
A,
|
||||
B
|
||||
}
|
||||
|
||||
|
||||
private HopFrameConfig CreateGlobal(params TableConfig[] tables)
|
||||
=> new HopFrameConfig { Tables = tables.ToList() };
|
||||
|
||||
@@ -21,7 +43,8 @@ public class ConfigurationHelperTests {
|
||||
new PropertyConfig {
|
||||
Identifier = "Prop1",
|
||||
DisplayName = "Property 1",
|
||||
Type = typeof(int)
|
||||
Type = typeof(int),
|
||||
PropertyType = PropertyType.Numeric
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -92,12 +115,30 @@ public class ConfigurationHelperTests {
|
||||
Assert.Contains(config.Properties, p => p.Identifier == "Name");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeTable_SetsPropertyTypes_ForAllProperties() {
|
||||
var global = CreateGlobal();
|
||||
|
||||
var config = ConfigurationHelper.InitializeTable(global, typeof(string), typeof(PropertyTypeModel));
|
||||
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.Numeric);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == (PropertyType.Numeric | PropertyType.Nullable));
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.Boolean);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.DateTime);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.DateOnly);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.TimeOnly);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.Enum);
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == (PropertyType.Numeric | PropertyType.List));
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == (PropertyType.Text | PropertyType.List));
|
||||
Assert.Contains(config.Properties, p => p.PropertyType == PropertyType.Email);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InitializeProperty_UsesPropertyNameAsIdentifier_WhenUnique() {
|
||||
var table = CreateDummyTable("T");
|
||||
var property = typeof(TestModel).GetProperty(nameof(TestModel.Id))!;
|
||||
|
||||
var config = InvokeInitializeProperty(table, property);
|
||||
var config = ConfigurationHelper.InitializeProperty(table, property);
|
||||
|
||||
Assert.Equal("Id", config.Identifier);
|
||||
}
|
||||
@@ -105,12 +146,14 @@ public class ConfigurationHelperTests {
|
||||
[Fact]
|
||||
public void InitializeProperty_GeneratesGuid_WhenIdentifierAlreadyExists() {
|
||||
var table = CreateDummyTable("T");
|
||||
table.Properties.Add(new PropertyConfig
|
||||
{ Identifier = "Id", Type = typeof(int), DisplayName = "Id", OrderIndex = 0 });
|
||||
table.Properties.Add(new PropertyConfig {
|
||||
Identifier = "Id", Type = typeof(int), DisplayName = "Id", OrderIndex = 0,
|
||||
PropertyType = PropertyType.Numeric
|
||||
});
|
||||
|
||||
var property = typeof(TestModel).GetProperty(nameof(TestModel.Id))!;
|
||||
|
||||
var config = InvokeInitializeProperty(table, property);
|
||||
var config = ConfigurationHelper.InitializeProperty(table, property);
|
||||
|
||||
Assert.NotEqual("Id", config.Identifier);
|
||||
Assert.True(Guid.TryParse(config.Identifier, out _));
|
||||
@@ -121,7 +164,7 @@ public class ConfigurationHelperTests {
|
||||
var table = CreateDummyTable("T");
|
||||
var property = typeof(TestModel).GetProperty(nameof(TestModel.Name))!;
|
||||
|
||||
var config = InvokeInitializeProperty(table, property);
|
||||
var config = ConfigurationHelper.InitializeProperty(table, property);
|
||||
|
||||
Assert.Equal("Name", config.DisplayName);
|
||||
Assert.Equal(typeof(string), config.Type);
|
||||
@@ -131,23 +174,28 @@ public class ConfigurationHelperTests {
|
||||
[Fact]
|
||||
public void InitializeProperty_SetsOrderIndex_ToCurrentPropertyCount() {
|
||||
var table = CreateDummyTable("T");
|
||||
table.Properties.Add(new PropertyConfig
|
||||
{ Identifier = "X", Type = typeof(int), DisplayName = "X", OrderIndex = 0 });
|
||||
table.Properties.Add(new PropertyConfig {
|
||||
Identifier = "X", Type = typeof(int), DisplayName = "X", OrderIndex = 0, PropertyType = PropertyType.Numeric
|
||||
});
|
||||
|
||||
var property = typeof(TestModel).GetProperty(nameof(TestModel.Name))!;
|
||||
|
||||
var config = InvokeInitializeProperty(table, property);
|
||||
var config = ConfigurationHelper.InitializeProperty(table, property);
|
||||
|
||||
Assert.Equal(1, config.OrderIndex);
|
||||
}
|
||||
|
||||
private PropertyConfig InvokeInitializeProperty(TableConfig table, PropertyInfo property) {
|
||||
var method = typeof(ConfigurationHelper)
|
||||
.GetMethod("InitializeProperty", BindingFlags.NonPublic | BindingFlags.Static)!;
|
||||
[Fact]
|
||||
public void InitializeProperty_SetsPropertyType_FromInferPropertyType() {
|
||||
var table = CreateDummyTable("T");
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Number))!;
|
||||
|
||||
return (PropertyConfig)method.Invoke(null, [table, property])!;
|
||||
var config = ConfigurationHelper.InitializeProperty(table, prop);
|
||||
|
||||
Assert.Equal(PropertyType.Numeric, config.PropertyType);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void ValidateTable_ReturnsError_WhenIdentifierNotUnique() {
|
||||
var config = CreateValidTable();
|
||||
@@ -210,7 +258,8 @@ public class ConfigurationHelperTests {
|
||||
config.Properties.Add(new PropertyConfig {
|
||||
Identifier = "Prop1",
|
||||
DisplayName = "Duplicate",
|
||||
Type = typeof(int)
|
||||
Type = typeof(int),
|
||||
PropertyType = PropertyType.Numeric
|
||||
});
|
||||
|
||||
var result = ConfigurationHelper.ValidateTable(CreateGlobal(), config).ToList();
|
||||
@@ -246,4 +295,84 @@ public class ConfigurationHelperTests {
|
||||
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesNumeric() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Number))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(int), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Numeric, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesNullableNumeric() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.NullableNumber))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(int?), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Numeric | PropertyType.Nullable, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesBoolean() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Flag))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(bool), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Boolean, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesDateTime() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Timestamp))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(DateTime), prop);
|
||||
|
||||
Assert.Equal(PropertyType.DateTime, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesDateOnly() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Date))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(DateOnly), prop);
|
||||
|
||||
Assert.Equal(PropertyType.DateOnly, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesTimeOnly() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Time))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(TimeOnly), prop);
|
||||
|
||||
Assert.Equal(PropertyType.TimeOnly, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesEnum() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.EnumValue))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(TestEnum), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Enum, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesList() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Numbers))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(List<int>), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Numeric | PropertyType.List, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesEnumerable() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Strings))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(IEnumerable<string>), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Text | PropertyType.List, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InferPropertyType_RecognizesEmail() {
|
||||
var prop = typeof(PropertyTypeModel).GetProperty(nameof(PropertyTypeModel.Email))!;
|
||||
var result = ConfigurationHelper.InferPropertyType(typeof(string), prop);
|
||||
|
||||
Assert.Equal(PropertyType.Email, result);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ using Moq;
|
||||
namespace HopFrame.Tests.Core.Repositories;
|
||||
|
||||
public class HopFrameRepositoryTests {
|
||||
|
||||
private Mock<HopFrameRepository<TestModel>> CreateMock()
|
||||
=> new(MockBehavior.Strict);
|
||||
|
||||
@@ -21,7 +20,7 @@ public class HopFrameRepositoryTests {
|
||||
mock.Setup(r => r.LoadPageAsync(2, 10, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(expected);
|
||||
|
||||
var result = await mock.Object.LoadPageGenericAsync(2, 10);
|
||||
var result = await mock.Object.LoadPageGenericAsync(2, 10, CancellationToken.None);
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
@@ -39,7 +38,7 @@ public class HopFrameRepositoryTests {
|
||||
mock.Setup(r => r.SearchAsync("abc", 1, 20, It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(expected);
|
||||
|
||||
var result = await mock.Object.SearchGenericAsync("abc", 1, 20);
|
||||
var result = await mock.Object.SearchGenericAsync("abc", 1, 20, CancellationToken.None);
|
||||
|
||||
Assert.Equal(expected, result);
|
||||
}
|
||||
@@ -62,6 +61,24 @@ public class HopFrameRepositoryTests {
|
||||
mock.Verify(r => r.CreateAsync(model, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// UpdateGenericAsync
|
||||
// -------------------------------------------------------------
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateGenericAsync_CastsAndDelegates() {
|
||||
var mock = CreateMock();
|
||||
|
||||
var model = new TestModel { Id = 77 };
|
||||
|
||||
mock.Setup(r => r.UpdateAsync(model, It.IsAny<CancellationToken>()))
|
||||
.Returns(Task.CompletedTask);
|
||||
|
||||
await mock.Object.UpdateGenericAsync(model, CancellationToken.None);
|
||||
|
||||
mock.Verify(r => r.UpdateAsync(model, It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// DeleteGenericAsync
|
||||
// -------------------------------------------------------------
|
||||
|
||||
@@ -9,18 +9,23 @@ namespace HopFrame.Tests.Core.Services.Implementation;
|
||||
|
||||
public class ConfigAccessorTests {
|
||||
private class TestRepository : IHopFrameRepository {
|
||||
public Task<IEnumerable> LoadPageGenericAsync(int page, int perPage, CancellationToken ct = default) {
|
||||
public Task<IEnumerable> LoadPageGenericAsync(int page, int perPage, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task<int> CountAsync(CancellationToken ct = default) {
|
||||
public Task<int> CountAsync(CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task<IEnumerable> SearchGenericAsync(string searchTerm, int page, int perPage, CancellationToken ct = default) {
|
||||
public Task<IEnumerable> SearchGenericAsync(string searchTerm, int page, int perPage, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
public Task CreateGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task UpdateGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task DeleteGenericAsync(object entry, CancellationToken ct) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
@@ -39,7 +44,8 @@ public class ConfigAccessorTests {
|
||||
Identifier = "Id",
|
||||
DisplayName = "Id",
|
||||
Type = typeof(int),
|
||||
OrderIndex = 0
|
||||
OrderIndex = 0,
|
||||
PropertyType = PropertyType.Numeric
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user