using System.ComponentModel; using Quadarax.Foundation.Core.Data.Entity; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using qdr.fnd.core.test.Utils; namespace qdr.fnd.core.test { public abstract class DaoTest : BaseTest where TDao : Entity where TKey : IEquatable { protected void CheckHasPK(string customMessage) { if (string.IsNullOrEmpty(customMessage)) customMessage = "Has no PK flag"; Assert.IsTrue(ReflectionUtils.HasPropertyAttribute(typeof(TDao),"Id"), customMessage); } public void CheckColumnMappings(string propertyName, string columnName, bool? isRequired = null, int? maxSize = null, object? defaultValue = null) { var pinfo = ReflectionUtils.GetProperty(typeof(TDao), propertyName, false); Assert.IsNotNull(pinfo, $"type '{typeof(TDao).Name}' has no public property '{propertyName}'"); Assert.IsTrue(pinfo.CanRead, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'"); Assert.IsTrue(pinfo.CanWrite, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'"); if (!string.IsNullOrEmpty(columnName)) { //check column mapping var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Column attribute therefore expected"); Assert.That(attrCol.Name, Is.EqualTo(columnName)); } if (isRequired!=null) { //check requiref attribute var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); if (isRequired.GetValueOrDefault()) Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Require attribute therefore expected"); else Assert.IsNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has Require attribute therefore not allowed"); } if (maxSize!=null) { //check max size attribute var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no MaxLength attribute therefore expected"); Assert.That(attrCol.Length, Is.EqualTo(maxSize)); } if (defaultValue!=null) { //check max size attribute var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no DefaultValue attribute therefore expected"); Assert.That(attrCol.Value, Is.EqualTo(defaultValue)); } } public void CheckColumnFKMappings(string propertyName, string columnName, string fkName, bool? isRequired = null) { var pinfo = ReflectionUtils.GetProperty(typeof(TDao), propertyName, false); Assert.IsNotNull(pinfo, $"type '{typeof(TDao).Name}' has no public property '{propertyName}'"); Assert.IsTrue(pinfo.CanRead, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'"); Assert.IsTrue(pinfo.CanWrite, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'"); if (!string.IsNullOrEmpty(columnName)) { //check column mapping var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Column attribute therefore expected"); Assert.That(attrCol.Name, Is.EqualTo(columnName)); } var attrFk = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); Assert.IsNotNull(attrFk, $"type '{typeof(TDao).Name}' property '{propertyName}' has no ForeignKey attribute therefore expected"); Assert.That(attrFk.Name, Is.EqualTo(fkName)); if (isRequired!=null) { //check requiref attribute var attrCol = ReflectionUtils.GetPropertyAttribute(typeof(TDao), propertyName, true); if (isRequired.GetValueOrDefault()) Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Require attribute therefore expected"); else Assert.IsNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has Require attribute therefore not allowed"); } } } }