Skip to content

Commit

Permalink
Merge pull request #923 from mikependon/repodb-enhancements
Browse files Browse the repository at this point in the history
Added the PropertyValuteAttributeResolverTest / More updates on the test coverages for the PropertyValueAttribute
  • Loading branch information
mikependon committed Sep 23, 2021
2 parents 0faeb53 + 9443bb0 commit a347708
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void TestPropertyValueAttributeCacheWithAttributesViaPropertyName()
var propertyName = "PropertyInt";

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(propertyName, false);
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(propertyName);

// Assert
Assert.AreEqual(2, actual.Count());
Expand All @@ -94,7 +94,7 @@ public void TestPropertyValueAttributeCacheWithAttributesViaField()
var field = new Field("PropertyInt");

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(field, false);
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(field);

// Assert
Assert.AreEqual(2, actual.Count());
Expand All @@ -104,7 +104,7 @@ public void TestPropertyValueAttributeCacheWithAttributesViaField()
public void TestPropertyValueAttributeCacheWithAttributesViaExpression()
{
// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(e => e.PropertyInt, false);
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(e => e.PropertyInt);

// Assert
Assert.AreEqual(2, actual.Count());
Expand All @@ -117,7 +117,7 @@ public void TestPropertyValueAttributeCacheWithAttributesViaPropertyInfo()
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyInt", true);

// Act
var actual = PropertyValueAttributeCache.Get(classProperty.PropertyInfo, false);
var actual = PropertyValueAttributeCache.Get(classProperty.PropertyInfo);

// Assert
Assert.AreEqual(2, actual.Count());
Expand Down Expand Up @@ -189,6 +189,68 @@ public void TestPropertyValueAttributeCacheWithMappedAttributesViaPropertyInfo()
Assert.AreEqual(7, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheWithMappedAttributesWithIncludeMappingsFalseViaMappedPropertyName()
{
// Prepare
var propertyName = "PropertyString";

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(propertyName, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheWithMappedAttributesWithIncludeMappingsFalseViaPropertyName()
{
// Prepare
var propertyName = "PropertyString";

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(propertyName, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheWithMappedAttributesWithIncludeMappingsFalseViaField()
{
// Prepare
var field = new Field("PropertyString");

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(field, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheWithMappedAttributesWithIncludeMappingsFalseViaExpression()
{
// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(e => e.PropertyString, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheWithMappedAttributesWithIncludeMappingsFalseViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyString", true);

// Act
var actual = PropertyValueAttributeCache.Get(classProperty.PropertyInfo, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

/*
* Attribute Collisions
*/
Expand Down Expand Up @@ -255,6 +317,55 @@ public void TestPropertyValueAttributeCacheCollisionsViaPropertyInfo()
Assert.AreEqual(11, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheCollisionsWithIncludeMappingsFalseViaPropertyName()
{
// Prepare
var propertyName = "PropertyDecimal";

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(propertyName, false);

// Assert
Assert.AreEqual(7, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheCollisionsWithIncludeMappingsFalseViaField()
{
// Prepare
var field = new Field("PropertyDecimal");

// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(field, false);

// Assert
Assert.AreEqual(7, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheCollisionsWithIncludeMappingsFalseViaExpression()
{
// Act
var actual = PropertyValueAttributeCache.Get<PropertyValueAttributeClass>(e => e.PropertyDecimal, false);

// Assert
Assert.AreEqual(7, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeCacheCollisionsWithIncludeMappingsFalseViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyDecimal", false);

// Act
var actual = PropertyValueAttributeCache.Get(classProperty.PropertyInfo, false);

// Assert
Assert.AreEqual(7, actual.Count());
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RepoDb.Attributes.Parameter;
using RepoDb.Resolvers;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace RepoDb.UnitTests.Cachers
{
[TestClass]
public partial class PropertyValueAttributeResolverTest
{
[TestInitialize]
public void Initialize()
{
var attributes = GetPropertyValueAttributes();
PropertyValueAttributeMapper.Add<PropertyValueAttributeClass>(e => e.PropertyString, attributes, true);
PropertyValueAttributeMapper.Add<PropertyValueAttributeClass>(e => e.PropertyDecimal, attributes, true);
}

[TestCleanup]
public void Cleanup()
{
PropertyValueAttributeCache.Flush();
PropertyValueAttributeMapper.Clear();
}

#region SubClasses

private class PropertyValueAttributeClass
{
public string PropertyString { get; set; }

[
Name("ColumnInt"),
Size(256)
]
public string PropertyInt { get; set; }

[
Name("ColumnDecimal"),
DbType(DbType.Decimal),
Direction(ParameterDirection.InputOutput),
IsNullable(true),
Precision(100),
Scale(2),
Size(256)
]
public string PropertyDecimal { get; set; }
}

#endregion

#region Helpers

private IEnumerable<PropertyValueAttribute> 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)
};

#endregion

#region Methods

/*
* With Attributes
*/

[TestMethod]
public void TestPropertyValueAttributeResolverWithAttributesViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyInt", true);

// Act
var actual = new PropertyValueAttributeResolver().Resolve(classProperty.PropertyInfo);

// Assert
Assert.AreEqual(2, actual.Count());
}

/*
* Without Attributes
*/

[TestMethod]
public void TestPropertyValueAttributeResolverWithMappedAttributesViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyString", true);

// Act
var actual = new PropertyValueAttributeResolver().Resolve(classProperty.PropertyInfo, true);

// Assert
Assert.AreEqual(7, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeResolverWithMappedAttributesAndWithIncludeMappingsFalseViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyString", true);

// Act
var actual = new PropertyValueAttributeResolver().Resolve(classProperty.PropertyInfo, false);

// Assert
Assert.AreEqual(0, actual.Count());
}

/*
* Attribute Collisions
*/

[TestMethod]
public void TestPropertyValueAttributeResolverCollisionsViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyDecimal", true);

// Act
var actual = new PropertyValueAttributeResolver().Resolve(classProperty.PropertyInfo, true);

// Assert
Assert.AreEqual(11, actual.Count());
}

[TestMethod]
public void TestPropertyValueAttributeResolverCollisionsWithIncludeMappingsFalseViaPropertyInfo()
{
// Prepare
var classProperty = PropertyCache.Get<PropertyValueAttributeClass>("PropertyDecimal", true);

// Act
var actual = new PropertyValueAttributeResolver().Resolve(classProperty.PropertyInfo, false);

// Assert
Assert.AreEqual(7, actual.Count());
}

#endregion
}
}
30 changes: 0 additions & 30 deletions RepoDb.Core/RepoDb/Cachers/PropertyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ public static ClassProperty Get<TEntity>(Expression<Func<TEntity, object>> expre
where TEntity : class =>
Get(typeof(TEntity), ExpressionExtension.GetProperty<TEntity>(expression), false);

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via property name).
/// </summary>
/// <typeparam name="TEntity">The type of the data entity.</typeparam>
/// <param name="propertyName">The name of the property.</param>
/// <returns>The instance of cached <see cref="ClassProperty"/> object.</returns>
public static ClassProperty Get<TEntity>(string propertyName)
where TEntity : class =>
Get(typeof(TEntity), propertyName, false);

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via property name).
/// </summary>
Expand All @@ -49,16 +39,6 @@ public static ClassProperty Get<TEntity>(string propertyName,
where TEntity : class =>
Get(typeof(TEntity), propertyName, includeMappings);

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via property name).
/// </summary>
/// <param name="entityType">The type of the data entity.</param>
/// <param name="propertyName">The name of the property.</param>
/// <returns>The instance of cached <see cref="ClassProperty"/> object.</returns>
public static ClassProperty Get(Type entityType,
string propertyName) =>
Get(entityType, propertyName, false);

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via property name).
/// </summary>
Expand Down Expand Up @@ -110,16 +90,6 @@ public static ClassProperty Get(Type entityType,
return Get(entityType, field.Name, includeMappings);
}

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via <see cref="PropertyInfo"/> object).
/// </summary>
/// <param name="entityType">The type of the data entity.</param>
/// <param name="propertyInfo">The instance of the <see cref="PropertyInfo"/> object.</param>
/// <returns>The instance of cached <see cref="ClassProperty"/> object.</returns>
internal static ClassProperty Get(Type entityType,
PropertyInfo propertyInfo) =>
Get(entityType, propertyInfo, false);

/// <summary>
/// Gets the cached <see cref="ClassProperty"/> object of the data entity (via <see cref="PropertyInfo"/> object).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public static Func<TEntity, TResult> GetFunc(ClassProperty classProperty)
/// <returns></returns>
public static Action<TEntity, object> GetPropertySetterFunc<TEntity>(string propertyName)
where TEntity : class =>
PropertySetterFuncCache<TEntity>.GetFunc(PropertyCache.Get<TEntity>(propertyName));
PropertySetterFuncCache<TEntity>.GetFunc(PropertyCache.Get<TEntity>(propertyName, true));

/// <summary>
///
Expand Down

0 comments on commit a347708

Please sign in to comment.