| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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<TDao,TKey> : BaseTest where TDao : Entity<TKey>
- where TKey : IEquatable<TKey>
- {
- protected void CheckHasPK(string customMessage)
- {
- if (string.IsNullOrEmpty(customMessage))
- customMessage = "Has no PK flag";
- Assert.IsTrue(ReflectionUtils.HasPropertyAttribute<KeyAttribute>(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<ColumnAttribute>(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<RequiredAttribute>(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<MaxLengthAttribute>(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<DefaultValueAttribute>(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<ColumnAttribute>(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<ForeignKeyAttribute>(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<RequiredAttribute>(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");
- }
- }
- }
- }
|