From a3276f18e93adb6adfbe682b03965a29acb0ea85 Mon Sep 17 00:00:00 2001 From: Michael Pendon Date: Thu, 23 Sep 2021 13:22:31 +0200 Subject: [PATCH] #899 Added the FluentMapper.PropertyHandlerAttributes() Unit Tests. --- .../Mappers/FluentMapperTest.cs | 346 +++++++++++++++--- 1 file changed, 285 insertions(+), 61 deletions(-) diff --git a/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Mappers/FluentMapperTest.cs b/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Mappers/FluentMapperTest.cs index 44f062bd..3c122d85 100644 --- a/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Mappers/FluentMapperTest.cs +++ b/RepoDb.Core/RepoDb.Tests/RepoDb.UnitTests/Mappers/FluentMapperTest.cs @@ -2,20 +2,17 @@ using RepoDb.Attributes; using RepoDb.Attributes.Parameter; using RepoDb.Exceptions; +using System.Collections.Generic; using System.Data; +using System.Linq; namespace RepoDb.UnitTests.Mappers { [TestClass] public partial class FluentMapperTest { - [ClassInitialize] - public static void ClassInitialize(TestContext context) - { - Setup(); - } - - private static void Setup() + [TestInitialize()] + public void Initialize() { FluentMapper .Entity() @@ -23,7 +20,8 @@ private static void Setup() .Primary(e => e.PrimaryId) .Identity(e => e.PrimaryId) .Column(e => e.PropertyString, "ColumnString") - .DbType(e => e.PropertyString, DbType.String); + .DbType(e => e.PropertyString, DbType.String) + .PropertyValueAttributes(e => e.PropertyString, GetPropertyValueAttributes()); FluentMapper .Entity() @@ -31,7 +29,28 @@ private static void Setup() .Primary(e => e.PrimaryId) .Identity(e => e.PrimaryId) .Column(e => e.PropertyString, "ColumnNVarChar") - .DbType(e => e.PropertyString, DbType.StringFixedLength); + .DbType(e => e.PropertyString, DbType.StringFixedLength) + .PropertyValueAttributes(e => e.PropertyString, GetPropertyValueAttributes()); + } + + [TestCleanup] + public void Cleanup() + { + ClassMapper.Clear(); + PropertyMapper.Clear(); + PrimaryMapper.Clear(); + IdentityMapper.Clear(); + TypeMapper.Clear(); + //PropertyHandlerMapper.Clear(); + PropertyValueAttributeMapper.Clear(); + + ClassMappedNameCache.Flush(); + PropertyCache.Flush(); + PrimaryCache.Flush(); + IdentityCache.Flush(); + TypeMapCache.Flush(); + //PropertyHandlerMapper.Clear(); + PropertyValueAttributeCache.Flush(); } #region SubClasses @@ -52,11 +71,52 @@ private class FluentMapperTestWithAttributesClass public int PrimaryId { get; set; } [Map("ColumnString"), TypeMap(DbType.String)] + [ + Name("ColumnDecimal"), + DbType(DbType.Decimal), + Direction(ParameterDirection.InputOutput), + IsNullable(true), + Precision(100), + Scale(2), + Size(256) + ] public string PropertyString { get; set; } } #endregion + #region Helpers + + private IEnumerable GetPropertyValueAttributes() => + new PropertyValueAttribute[] + { + // Different Values + new NameAttribute("ColumnString"), + new DbTypeAttribute(DbType.StringFixedLength), + new DirectionAttribute(ParameterDirection.ReturnValue), + new SizeAttribute(512), + // Same Values + new IsNullableAttribute(true), + new PrecisionAttribute(100), + new ScaleAttribute(2) + }; + + private IEnumerable GetPropertyValueAttributesForOverriding() => + new PropertyValueAttribute[] + { + // Different Values + new NameAttribute("OverridingColumnString"), + new DbTypeAttribute(DbType.AnsiString), + new DirectionAttribute(ParameterDirection.InputOutput), + new SizeAttribute(1024), + // Same Values + new IsNullableAttribute(false), + new PrecisionAttribute(200), + new ScaleAttribute(4) + }; + + #endregion + #region Methods #region Table @@ -66,7 +126,7 @@ private class FluentMapperTestWithAttributesClass */ [TestMethod] - public void TestFluentMapMapping() + public void TestFluentMapTableMapping() { // Act var actual = ClassMappedNameCache.Get(); @@ -81,7 +141,7 @@ public void TestFluentMapMapping() */ [TestMethod] - public void TestFluentMapMappingWithMapAttribute() + public void TestFluentMapTableMappingWithMapAttribute() { // Act var actual = ClassMappedNameCache.Get(); @@ -96,7 +156,7 @@ public void TestFluentMapMappingWithMapAttribute() */ [TestMethod] - public void TestFluentMapMappingOverride() + public void TestFluentMapTableMappingOverride() { // Setup FluentMapper @@ -116,7 +176,7 @@ public void TestFluentMapMappingOverride() */ [TestMethod, ExpectedException(typeof(MappingExistsException))] - public void ThrowExceptionOnFluentMappingThatIsAlreadyExisting() + public void ThrowExceptionOnFluentMapTableMappingThatIsAlreadyExisting() { // Setup FluentMapper @@ -158,25 +218,25 @@ public void TestFluentMapPrimaryMappingWithMapAttribute() Assert.AreEqual(expected, actual.GetMappedName()); } - ///* - // * Override True - // */ + /* + * Override True + */ - //[TestMethod] - //public void TestFluentMapPrimaryMappingOverride() - //{ - // // Setup - // FluentMapper - // .Entity() - // .Primary(e => e.RowId, true); + [TestMethod] + public void TestFluentMapPrimaryMappingOverride() + { + // Setup + FluentMapper + .Entity() + .Primary(e => e.RowId, true); - // // Act - // var actual = PrimaryCache.Get(); - // var expected = "RowId"; + // Act + var actual = PrimaryCache.Get(); + var expected = "RowId"; - // // Assert - // Assert.AreEqual(expected, actual.GetMappedName()); - //} + // Assert + Assert.AreEqual(expected, actual.GetMappedName()); + } /* * Override False @@ -225,25 +285,25 @@ public void TestFluentMapIdentityMappingWithMapAttribute() Assert.AreEqual(expected, actual.GetMappedName()); } - ///* - // * Override True - // */ + /* + * Override True + */ - //[TestMethod] - //public void TestFluentMapIdentityMappingOverride() - //{ - // // Setup - // FluentMapper - // .Entity() - // .Identity(e => e.RowId, true); + [TestMethod] + public void TestFluentMapIdentityMappingOverride() + { + // Setup + FluentMapper + .Entity() + .Identity(e => e.RowId, true); - // // Act - // var actual = IdentityCache.Get(); - // var expected = "RowId"; + // Act + var actual = IdentityCache.Get(); + var expected = "RowId"; - // // Assert - // Assert.AreEqual(expected, actual.GetMappedName()); - //} + // Assert + Assert.AreEqual(expected, actual.GetMappedName()); + } /* * Override False @@ -260,6 +320,73 @@ public void ThrowExceptionOnFluentMapIdentityMappingThatIsAlreadyExisting() #endregion + #region Column + + /* + * No MapAttribute + */ + + [TestMethod] + public void TestFluentMapColumnMapping() + { + // Act + var actual = PropertyMappedNameCache.Get(e => e.PropertyString); + var expected = "ColumnString"; + + // Assert + Assert.AreEqual(expected, actual); + } + + /* + * With MapAttribute + */ + + [TestMethod] + public void TestFluentMapColumnMappingWithMapAttribute() + { + // Act + var actual = PropertyMappedNameCache.Get(e => e.PropertyString); + var expected = "ColumnString"; + + // Assert + Assert.AreEqual(expected, actual); + } + + /* + * Override True + */ + + [TestMethod] + public void TestFluentMapColumnMappingOverride() + { + // Setup + FluentMapper + .Entity() + .Column(e => e.PropertyString, "ColumnStringOverriden", true); + + // Act + var actual = PropertyMapper.Get(e => e.PropertyString); + var expected = "ColumnStringOverriden"; + + // Assert + Assert.AreEqual(expected, actual); + } + + /* + * Override False + */ + + [TestMethod, ExpectedException(typeof(MappingExistsException))] + public void ThrowExceptionOnFluentMapColumnMappingThatIsAlreadyExisting() + { + // Setup + FluentMapper + .Entity() + .Column(e => e.PropertyString, "ColumnStringOverriden"); + } + + #endregion + #region DbType /* @@ -292,25 +419,25 @@ public void TestFluentMapDbTypeMappingWithMapAttribute() Assert.AreEqual(expected, actual); } - ///* - // * Override True - // */ + /* + * Override True + */ - //[TestMethod] - //public void TestFluentMapDbTypeMappingOverride() - //{ - // // Setup - // FluentMapper - // .Entity() - // .DbType(e => e.PropertyString, DbType.AnsiString, true); + [TestMethod] + public void TestFluentMapDbTypeMappingOverride() + { + // Setup + FluentMapper + .Entity() + .DbType(e => e.PropertyString, DbType.AnsiString, true); - // // Act - // var actual = TypeMapCache.Get(); - // var expected = DbType.AnsiString; + // Act + var actual = TypeMapCache.Get(e => e.PropertyString); + var expected = DbType.AnsiString; - // // Assert - // Assert.AreEqual(expected, actual); - //} + // Assert + Assert.AreEqual(expected, actual); + } /* * Override False @@ -327,6 +454,103 @@ public void ThrowExceptionOnFluentMapDbTypeMappingThatIsAlreadyExisting() #endregion + // PropertyHandler + + #region PropertyValueAttribute + + /* + * No MapAttribute + */ + + [TestMethod] + public void TestFluentMapPropertyValueAttributesMapping() + { + // Act + var actual = PropertyValueAttributeCache + .Get(e => e.PropertyString, true); + + // Assert + Assert.AreEqual(7, actual.Count()); + } + + [TestMethod] + public void TestFluentMapPropertyValueAttributesMappingWithIncludeMappingFalse() + { + // Act + var actual = PropertyValueAttributeCache + .Get(e => e.PropertyString, false); + + // Assert + Assert.AreEqual(0, actual.Count()); + } + + /* + * With MapAttribute + */ + + [TestMethod] + public void TestFluentMapPropertyValueAttributesMappingWithMapAttribute() + { + // Act + var actual = PropertyValueAttributeCache + .Get(e => e.PropertyString, true); + + // Assert + Assert.AreEqual(12, actual.Count()); + } + + [TestMethod] + public void TestFluentMapPropertyValueAttributesMappingWithMapAttributeWithIncludeMappingFalse() + { + // Act + var actual = PropertyValueAttributeCache + .Get(e => e.PropertyString, false); + + // Assert + Assert.AreEqual(8, actual.Count()); + } + + /* + * Override True + */ + + [TestMethod] + public void TestFluentMapPropertyValueAttributesMappingOverride() + { + // Prepare + var attributes = GetPropertyValueAttributesForOverriding().ToList(); + + // Setup + FluentMapper + .Entity() + .PropertyValueAttributes(e => e.PropertyString, attributes, true); + + // Act + var actual = PropertyValueAttributeCache + .Get(e => e.PropertyString, true).ToList(); + + // Assert + for (var i = 0; i < attributes.Count; i++) + { + Assert.AreEqual(attributes[i], actual[i]); + } + } + + /* + * Override False + */ + + [TestMethod, ExpectedException(typeof(MappingExistsException))] + public void ThrowExceptionOnFluentMapPropertyValueAttributesMappingThatIsAlreadyExisting() + { + // Setup + FluentMapper + .Entity() + .PropertyValueAttributes(e => e.PropertyString, GetPropertyValueAttributes()); + } + + #endregion + #endregion } }