Added more tests

This commit is contained in:
2025-01-19 12:12:01 +01:00
parent afe8a41f6c
commit 8d63910aae
14 changed files with 529 additions and 60 deletions

View File

@@ -18,6 +18,24 @@ public class HopFrameConfiguratorTests {
Assert.IsType<DbContextConfig>(config.Contexts[0]);
Assert.IsType<DbContextConfigurator<MockDbContext>>(dbContextConfigurator);
}
[Fact]
public void AddDbContext_WithConfigurator_AddsDbContextToInnerConfig() {
// Arrange
var config = new HopFrameConfig();
var configurator = new HopFrameConfigurator(config);
// Act
object dbContextConfigurator = null!;
configurator.AddDbContext<MockDbContext>(context => {
dbContextConfigurator = context;
});
// Assert
Assert.Single(config.Contexts);
Assert.IsType<DbContextConfig>(config.Contexts[0]);
Assert.IsType<DbContextConfigurator<MockDbContext>>(dbContextConfigurator);
}
[Fact]
public void DisplayUserInfo_SetsDisplayUserInfoProperty() {

View File

@@ -209,4 +209,17 @@ public class PropertyConfiguratorTests {
// Assert
Assert.Equal(orderIndex, propertyConfig.Order);
}
[Fact]
public void Constructor_SetsTableProperty() {
// Arrange
var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0);
// Act
var propertyConfig = new PropertyConfig(typeof(MockModel).GetProperty("Id")!, tableConfig, 0);
// Assert
Assert.NotNull(propertyConfig.Table);
Assert.Equal(tableConfig, propertyConfig.Table);
}
}

View File

@@ -32,6 +32,22 @@ public class TableConfiguratorTests {
// Assert
Assert.IsType<PropertyConfigurator<int>>(propertyConfigurator);
}
public void Property_WithConfigurator_ReturnsCorrectPropertyConfigurator() {
// Arrange
var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0);
var configurator = new TableConfigurator<MockModel>(tableConfig);
Expression<Func<MockModel, int>> propertyExpression = model => model.Id;
// Act
object propertyConfigurator = null!;
configurator.Property(propertyExpression, c => {
propertyConfigurator = c;
});
// Assert
Assert.IsType<PropertyConfigurator<int>>(propertyConfigurator);
}
[Fact]
public void AddVirtualProperty_AddsVirtualPropertyToConfig() {
@@ -50,6 +66,27 @@ public class TableConfiguratorTests {
Assert.True(virtualProperty.IsListingProperty);
Assert.Equal("VirtualName", virtualProperty.Name);
}
[Fact]
public void AddVirtualProperty_WithConfigurator_AddsVirtualPropertyToConfig() {
// Arrange
var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "MockModels", 0);
var configurator = new TableConfigurator<MockModel>(tableConfig);
Func<MockModel, IServiceProvider, string> template = (model, _) => model.Name!;
// Act
object propertyConfigurator = null!;
configurator.AddVirtualProperty("VirtualName", template, c => {
propertyConfigurator = c;
});
// Assert
var virtualProperty = tableConfig.Properties.SingleOrDefault(p => p.Name == "VirtualName");
Assert.NotNull(virtualProperty);
Assert.NotNull(propertyConfigurator);
Assert.True(virtualProperty.IsListingProperty);
Assert.Equal("VirtualName", virtualProperty.Name);
}
[Fact]
public void SetDisplayName_SetsDisplayNameProperty() {
@@ -148,4 +185,27 @@ public class TableConfiguratorTests {
// Assert
Assert.Equal(policy, tableConfig.DeletePolicy);
}
[Fact]
public void Constructor_WithKeyProperty_DisablesEdit() {
// Act
var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel2), "Models2", 0);
var prop = tableConfig.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel2.Id));
// Assert
Assert.NotNull(prop);
Assert.False(prop.Editable);
}
[Fact]
public void Constructor_WithGeneratedProperty_DisablesEditAndCreate() {
// Act
var tableConfig = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel2), "Models2", 0);
var prop = tableConfig.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel2.Number));
// Assert
Assert.NotNull(prop);
Assert.False(prop.Editable);
Assert.False(prop.Creatable);
}
}

View File

@@ -1,7 +1,12 @@
namespace HopFrame.Core.Tests.Models;
using System.ComponentModel.DataAnnotations.Schema;
namespace HopFrame.Core.Tests.Models;
// A mock model for testing purposes
public class MockModel {
public int Id { get; set; }
public string? Name { get; set; }
[ForeignKey("other")]
public List<MockModel2> Model2 { get; set; }
}

View File

@@ -1,5 +1,12 @@
namespace HopFrame.Core.Tests.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HopFrame.Core.Tests.Models;
public class MockModel2 {
public string Id { get; set; }
[Key]
public required string Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Number { get; set; }
}

View File

@@ -49,6 +49,26 @@ public class ContextExplorerTests {
Assert.NotNull(result);
Assert.Equal(tableConfig, result);
}
[Fact]
public void GetTable_ByDisplayName_ReturnsNullIfTableNotFound() {
// Arrange
var config = new HopFrameConfig();
var contextConfig = new DbContextConfig(typeof(MockDbContext));
var tableConfig = contextConfig.Tables[0];
config.Contexts.Add(contextConfig);
tableConfig.DisplayName = "TestTable";
var provider = new Mock<IServiceProvider>();
provider.Setup(p => p.GetService(typeof(MockDbContext))).Returns(new MockDbContext());
var contextExplorer = new ContextExplorer(config, provider.Object, new Logger<ContextExplorer>(new LoggerFactory()));
// Act
var result = contextExplorer.GetTable("InvalidTable");
// Assert
Assert.Null(result);
}
[Fact]
public void GetTable_ByType_ReturnsCorrectTable() {
@@ -69,6 +89,25 @@ public class ContextExplorerTests {
Assert.NotNull(result);
Assert.Equal(tableConfig, result);
}
[Fact]
public void GetTable_ByType_ReturnsNullIfTableNotFound() {
// Arrange
var config = new HopFrameConfig();
var contextConfig = new DbContextConfig(typeof(MockDbContext));
var tableConfig = contextConfig.Tables[0];
config.Contexts.Add(contextConfig);
var provider = new Mock<IServiceProvider>();
provider.Setup(p => p.GetService(typeof(MockDbContext))).Returns(new MockDbContext());
var contextExplorer = new ContextExplorer(config, provider.Object, new Logger<ContextExplorer>(new LoggerFactory()));
// Act
var result = contextExplorer.GetTable(typeof(ContextExplorerTests));
// Assert
Assert.Null(result);
}
[Fact]
public void GetTableManager_ReturnsCorrectTableManager() {
@@ -110,6 +149,27 @@ public class ContextExplorerTests {
// Assert
Assert.Null(tableManager);
}
[Fact]
public void GetTableManager_ReturnsNullIfTableNotFound() {
// Arrange
var config = new HopFrameConfig();
var contextConfig = new DbContextConfig(typeof(MockDbContext));
var tableConfig = new TableConfig(contextConfig, typeof(MockModel), "Models", 0);
contextConfig.Tables.Add(tableConfig);
config.Contexts.Add(contextConfig);
var dbContext = new MockDbContext();
var provider = new Mock<IServiceProvider>();
provider.Setup(p => p.GetService(typeof(MockDbContext))).Returns(dbContext);
var contextExplorer = new ContextExplorer(config, provider.Object, new Logger<ContextExplorer>(new LoggerFactory()));
// Act
var tableManager = contextExplorer.GetTableManager("InvalidTable");
// Assert
Assert.Null(tableManager);
}
[Fact]
public void SeedTableData_SetsTableSeededFlag() {
@@ -129,4 +189,33 @@ public class ContextExplorerTests {
// Assert
Assert.True(tableConfig.Seeded);
}
[Fact]
public void SeedTableData_SetsTablePropertiesCorrectly() {
// Arrange
var config = new HopFrameConfig();
var contextConfig = new DbContextConfig(typeof(MockDbContext));
var tableConfig = contextConfig.Tables[0];
var tableConfig2 = contextConfig.Tables[1];
config.Contexts.Add(contextConfig);
var provider = new Mock<IServiceProvider>();
provider.Setup(p => p.GetService(typeof(MockDbContext))).Returns(new MockDbContext());
var contextExplorer = new ContextExplorer(config, provider.Object, new Logger<ContextExplorer>(new LoggerFactory()));
// Act
contextExplorer.GetTable("Models");
contextExplorer.GetTable("Models2");
// Assert
var relationProp = tableConfig.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel.Model2));
var keyProp = tableConfig2.Properties.SingleOrDefault(prop => prop.Info.Name == nameof(MockModel2.Id));
Assert.NotNull(relationProp);
Assert.NotNull(keyProp);
Assert.True(relationProp.IsRelation);
Assert.True(relationProp.IsEnumerable);
Assert.True(keyProp.IsRequired);
Assert.False(keyProp.IsRelation);
Assert.False(keyProp.IsEnumerable);
}
}

View File

@@ -0,0 +1,199 @@
using HopFrame.Core.Config;
using HopFrame.Core.Services;
using HopFrame.Core.Services.Implementations;
using HopFrame.Core.Tests.Models;
using Microsoft.EntityFrameworkCore;
using Moq;
namespace HopFrame.Core.Tests.Services;
public class DisplayPropertyTests {
private readonly Mock<IServiceProvider> _providerMock;
private readonly Mock<IContextExplorer> _explorerMock;
private readonly TableConfig _config;
private readonly TableManager<object> _tableManager;
public DisplayPropertyTests() {
var contextMock = new Mock<DbContext>();
_providerMock = new Mock<IServiceProvider>();
_explorerMock = new Mock<IContextExplorer>();
_config = new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0);
_tableManager =
new TableManager<object>(contextMock.Object, _config, _explorerMock.Object, _providerMock.Object);
}
[Fact]
public void DisplayProperty_ReturnsEmptyString_WhenItemIsNull() {
// Arrange
var prop = new PropertyConfig(typeof(string).GetProperty("Length")!, _config, 0);
// Act
var result = _tableManager.DisplayProperty(null, prop);
// Assert
Assert.Equal(string.Empty, result);
}
[Fact]
public void DisplayProperty_UsesFormatter_WhenListingProperty() {
// Arrange
var item = "test";
var prop = new PropertyConfig(typeof(string).GetProperty("Length")!, _config, 0) {
IsListingProperty = true,
Formatter = (obj, provider) => ((string)obj).ToUpper()
};
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("TEST", result);
}
[Fact]
public void DisplayProperty_UsesValueFormatter_WhenNotListingProperty() {
// Arrange
var item = "test";
var prop = new PropertyConfig(typeof(string).GetProperty("Length")!, _config, 0) {
Formatter = (obj, provider) => ((int)obj).ToString("D4")
};
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("0004", result);
}
[Fact]
public void DisplayProperty_ReturnsValueAsString_WhenNoFormatter() {
// Arrange
var item = "test";
var prop = new PropertyConfig(typeof(string).GetProperty("Length")!, _config, 0);
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("4", result);
}
[Fact]
public void DisplayProperty_ReturnsEnumerableCount_WhenEnumerableProperty() {
// Arrange
var item = new { List = new List<int> { 1, 2, 3 } };
var prop = new PropertyConfig(item.GetType().GetProperty("List")!, _config, 0) {
IsEnumerable = true
};
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("3", result);
}
[Fact]
public void DisplayProperty_UsesDisplayedProperty_WhenNoDirectFormatter() {
// Arrange
var item = new { Inner = new { Key = 42 } };
var innerPropInfo = item.Inner.GetType().GetProperty("Key");
var innerPropConfig = new PropertyConfig(innerPropInfo!, _config, 0);
var propInfo = item.GetType().GetProperty("Inner");
var prop = new PropertyConfig(propInfo!, _config, 0) {
DisplayedProperty = innerPropInfo
};
_explorerMock
.Setup(e => e.GetTable(item.Inner.GetType()))
.Returns(new TableConfig(new DbContextConfig(typeof(MockDbContext)), typeof(MockModel), "Models", 0) {
Properties = { innerPropConfig }
});
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("42", result);
}
[Fact]
public void DisplayProperty_ReturnsEmptyString_WhenPropValueIsNull() {
// Arrange
var item = new { Name = (string?)null };
var prop = new PropertyConfig(item.GetType().GetProperty("Name")!, _config, 0);
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal(string.Empty, result);
}
[Fact]
public void DisplayProperty_UsesEnumerableFormatter_WhenEnumerableAndValueProvided() {
// Arrange
var item = new { List = new List<int> { 1, 2, 3 } };
var prop = new PropertyConfig(item.GetType().GetProperty("List")!, _config, 0) {
IsEnumerable = true,
EnumerableFormatter = (obj, provider) => string.Join(",", ((IEnumerable<int>)obj))
};
// Act
var result = _tableManager.DisplayProperty(item, prop, item.List);
// Assert
Assert.Equal("1,2,3", result);
}
[Fact]
public void DisplayProperty_ReturnsEmptyString_WhenDisplayedPropertyAndInnerConfigIsNull() {
// Arrange
var item = new { Inner = new { Key = 42 } };
var innerPropInfo = item.Inner.GetType().GetProperty("Key");
var propInfo = item.GetType().GetProperty("Inner");
var prop = new PropertyConfig(propInfo!, _config, 0) {
DisplayedProperty = innerPropInfo
};
_explorerMock
.Setup(e => e.GetTable(item.Inner.GetType()))
.Returns((TableConfig?)null);
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("{ Key = 42 }", result); // Returns the value as string if inner config is null
}
[Fact]
public void DisplayProperty_ReturnsKeyValue_WhenDisplayedPropertyIsNull() {
// Arrange
var item = new { Inner = new { Key = 42 } };
var propInfo = item.GetType().GetProperty("Inner");
var prop = new PropertyConfig(propInfo!, _config, 0);
var keyProperty = item.Inner.GetType().GetProperty("Key");
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("{ Key = 42 }", result); // Returns key value as string if DisplayedProperty is null
}
[Fact]
public void DisplayProperty_ReturnsToStringValue_WhenNoKeyOrDisplayedProperty() {
// Arrange
var item = new { Inner = new { Name = "Test" } };
var propInfo = item.GetType().GetProperty("Inner");
var prop = new PropertyConfig(propInfo!, _config, 0);
// Act
var result = _tableManager.DisplayProperty(item, prop);
// Assert
Assert.Equal("{ Name = Test }", result); // Returns ToString value of inner property
}
}