DaoTest.cs 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.ComponentModel;
  2. using Quadarax.Foundation.Core.Data.Entity;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.ComponentModel.DataAnnotations.Schema;
  5. using qdr.fnd.core.test.Utils;
  6. namespace qdr.fnd.core.test
  7. {
  8. public abstract class DaoTest<TDao,TKey> : BaseTest where TDao : Entity<TKey>
  9. where TKey : IEquatable<TKey>
  10. {
  11. protected void CheckHasPK(string customMessage)
  12. {
  13. if (string.IsNullOrEmpty(customMessage))
  14. customMessage = "Has no PK flag";
  15. Assert.IsTrue(ReflectionUtils.HasPropertyAttribute<KeyAttribute>(typeof(TDao),"Id"), customMessage);
  16. }
  17. public void CheckColumnMappings(string propertyName, string columnName, bool? isRequired = null, int? maxSize = null, object? defaultValue = null)
  18. {
  19. var pinfo = ReflectionUtils.GetProperty(typeof(TDao), propertyName, false);
  20. Assert.IsNotNull(pinfo, $"type '{typeof(TDao).Name}' has no public property '{propertyName}'");
  21. Assert.IsTrue(pinfo.CanRead, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'");
  22. Assert.IsTrue(pinfo.CanWrite, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'");
  23. if (!string.IsNullOrEmpty(columnName))
  24. {
  25. //check column mapping
  26. var attrCol = ReflectionUtils.GetPropertyAttribute<ColumnAttribute>(typeof(TDao), propertyName, true);
  27. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Column attribute therefore expected");
  28. Assert.That(attrCol.Name, Is.EqualTo(columnName));
  29. }
  30. if (isRequired!=null)
  31. {
  32. //check requiref attribute
  33. var attrCol = ReflectionUtils.GetPropertyAttribute<RequiredAttribute>(typeof(TDao), propertyName, true);
  34. if (isRequired.GetValueOrDefault())
  35. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Require attribute therefore expected");
  36. else
  37. Assert.IsNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has Require attribute therefore not allowed");
  38. }
  39. if (maxSize!=null)
  40. {
  41. //check max size attribute
  42. var attrCol = ReflectionUtils.GetPropertyAttribute<MaxLengthAttribute>(typeof(TDao), propertyName, true);
  43. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no MaxLength attribute therefore expected");
  44. Assert.That(attrCol.Length, Is.EqualTo(maxSize));
  45. }
  46. if (defaultValue!=null)
  47. {
  48. //check max size attribute
  49. var attrCol = ReflectionUtils.GetPropertyAttribute<DefaultValueAttribute>(typeof(TDao), propertyName, true);
  50. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no DefaultValue attribute therefore expected");
  51. Assert.That(attrCol.Value, Is.EqualTo(defaultValue));
  52. }
  53. }
  54. public void CheckColumnFKMappings(string propertyName, string columnName, string fkName, bool? isRequired = null)
  55. {
  56. var pinfo = ReflectionUtils.GetProperty(typeof(TDao), propertyName, false);
  57. Assert.IsNotNull(pinfo, $"type '{typeof(TDao).Name}' has no public property '{propertyName}'");
  58. Assert.IsTrue(pinfo.CanRead, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'");
  59. Assert.IsTrue(pinfo.CanWrite, $"type '{typeof(TDao).Name}' has no public getter on property '{propertyName}'");
  60. if (!string.IsNullOrEmpty(columnName))
  61. {
  62. //check column mapping
  63. var attrCol = ReflectionUtils.GetPropertyAttribute<ColumnAttribute>(typeof(TDao), propertyName, true);
  64. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Column attribute therefore expected");
  65. Assert.That(attrCol.Name, Is.EqualTo(columnName));
  66. }
  67. var attrFk = ReflectionUtils.GetPropertyAttribute<ForeignKeyAttribute>(typeof(TDao), propertyName, true);
  68. Assert.IsNotNull(attrFk, $"type '{typeof(TDao).Name}' property '{propertyName}' has no ForeignKey attribute therefore expected");
  69. Assert.That(attrFk.Name, Is.EqualTo(fkName));
  70. if (isRequired!=null)
  71. {
  72. //check requiref attribute
  73. var attrCol = ReflectionUtils.GetPropertyAttribute<RequiredAttribute>(typeof(TDao), propertyName, true);
  74. if (isRequired.GetValueOrDefault())
  75. Assert.IsNotNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has no Require attribute therefore expected");
  76. else
  77. Assert.IsNull(attrCol, $"type '{typeof(TDao).Name}' property '{propertyName}' has Require attribute therefore not allowed");
  78. }
  79. }
  80. }
  81. }