ConventionMapperTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using Shouldly;
  3. using Xunit;
  4. namespace PetaPoco.Tests.Unit.Core
  5. {
  6. public class ConventionMapperTests
  7. {
  8. private ConventionMapper _mapper;
  9. public ConventionMapperTests()
  10. {
  11. _mapper = new ConventionMapper();
  12. }
  13. [Fact]
  14. public void GetFromDbConverter_GivenPropertyAndType_ShouldBeNull()
  15. {
  16. var func = _mapper.GetFromDbConverter(typeof(Order).GetProperty(nameof(Order.OrderId)), typeof(int));
  17. func.ShouldBeNull();
  18. }
  19. [Fact]
  20. public void GetToDbConverter_GivenProperty_ShouldBeNull()
  21. {
  22. var func = _mapper.GetToDbConverter(typeof(Order).GetProperty(nameof(Order.OrderId)));
  23. func.ShouldBeNull();
  24. }
  25. [Fact]
  26. public void GetFromDbConverter_GivenPropertyWithValueConverterAttribute_ShouldNotBeNull()
  27. {
  28. var func = _mapper.GetFromDbConverter(typeof(Order).GetProperty(nameof(Order.PO)), typeof(string));
  29. func.ShouldNotBeNull();
  30. }
  31. [Fact]
  32. public void GetToDbConverter_GivenPropertyWithValueConverterAttribute_ShouldNotBeNull()
  33. {
  34. var func = _mapper.GetToDbConverter(typeof(Order).GetProperty(nameof(Order.PO)));
  35. func.ShouldNotBeNull();
  36. }
  37. [Fact]
  38. public void GetFromDbConverter_GivenPropertyTypeAndInterceptSet_ShouldCallback()
  39. {
  40. var wasCalled = false;
  41. _mapper.FromDbConverter = (info, type) =>
  42. {
  43. wasCalled = true;
  44. return null;
  45. };
  46. _mapper.GetFromDbConverter(typeof(Order).GetProperty(nameof(Order.OrderId)), typeof(int));
  47. wasCalled.ShouldBeTrue();
  48. }
  49. [Fact]
  50. public void GetToDbConverter_GivenPropertyAndInterceptSet_ShouldCallback()
  51. {
  52. var wasCalled = false;
  53. _mapper.ToDbConverter = (info) =>
  54. {
  55. wasCalled = true;
  56. return null;
  57. };
  58. _mapper.GetToDbConverter(typeof(Order).GetProperty(nameof(Order.OrderId)));
  59. wasCalled.ShouldBeTrue();
  60. }
  61. [Fact]
  62. public void GetTableInfo_UsingDefaults_ShouldBeValid()
  63. {
  64. var ti1 = _mapper.GetTableInfo(typeof(Order));
  65. var ti2 = _mapper.GetTableInfo(typeof(OrderLine));
  66. var ti3 = _mapper.GetTableInfo(typeof(Product));
  67. var ti4 = _mapper.GetTableInfo(typeof(Box));
  68. ti1.TableName.ShouldBe("Order");
  69. ti1.PrimaryKey.ShouldBe("OrderId");
  70. ti1.AutoIncrement.ShouldBeTrue();
  71. ti1.SequenceName.ShouldBeNull();
  72. ti2.TableName.ShouldBe("OrderLine");
  73. ti2.PrimaryKey.ShouldBe("OrderLine_Id");
  74. ti2.AutoIncrement.ShouldBeTrue();
  75. ti2.SequenceName.ShouldBeNull();
  76. ti3.TableName.ShouldBe("Product");
  77. ti3.PrimaryKey.ShouldBe("Id");
  78. ti3.AutoIncrement.ShouldBeTrue();
  79. ti3.SequenceName.ShouldBeNull();
  80. ti4.TableName.ShouldBe("Box");
  81. ti4.PrimaryKey.ShouldBe("Id");
  82. ti4.AutoIncrement.ShouldBeFalse();
  83. ti4.SequenceName.ShouldBeNull();
  84. }
  85. [Fact]
  86. public void GetColumnInfo_UsingDefaults_ShouldBeValid()
  87. {
  88. var ci1 = _mapper.GetColumnInfo(typeof(Order).GetProperty(nameof(Order.OrderId)));
  89. var ci2 = _mapper.GetColumnInfo(typeof(Order).GetProperty(nameof(Order.CreatedOn)));
  90. var ci3 = _mapper.GetColumnInfo(typeof(Order).GetProperty(nameof(Order.PO)));
  91. var ci4 = _mapper.GetColumnInfo(typeof(Order).GetProperty(nameof(Order.Discount)));
  92. ci1.ColumnName.ShouldBe("OrderId");
  93. ci1.ForceToUtc.ShouldBeFalse();
  94. ci1.ResultColumn.ShouldBeFalse();
  95. ci2.ColumnName.ShouldBe("CreatedOn");
  96. ci2.ForceToUtc.ShouldBeFalse();
  97. ci2.ResultColumn.ShouldBeFalse();
  98. ci3.ColumnName.ShouldBe("PO");
  99. ci3.ForceToUtc.ShouldBeFalse();
  100. ci3.ResultColumn.ShouldBeFalse();
  101. ci4.ColumnName.ShouldBe("Discount");
  102. ci4.ForceToUtc.ShouldBeFalse();
  103. ci4.ResultColumn.ShouldBeFalse();
  104. }
  105. [Fact]
  106. public void GetTableInfo_GivenEntityWithTableName_ShouldBeValid()
  107. {
  108. var ti = _mapper.GetTableInfo(typeof(EntityWithAttributes));
  109. ti.TableName.ShouldBe("Test1");
  110. ti.PrimaryKey.ShouldBe("ThatId");
  111. ti.AutoIncrement.ShouldBeTrue();
  112. ti.SequenceName.ShouldBeNull();
  113. }
  114. [Fact]
  115. public void GetColumnInfo_GivenColumnWithResultAttribute_ShouldBeValid()
  116. {
  117. var ci = _mapper.GetColumnInfo(typeof(EntityWithAttributes).GetProperty(nameof(EntityWithAttributes.Result)));
  118. ci.ColumnName.ShouldBe("Result");
  119. ci.ResultColumn.ShouldBeTrue();
  120. ci.ForceToUtc.ShouldBeFalse();
  121. }
  122. [Fact]
  123. public void GetColumnInfo_GivenColumnWithColumnAttribute_ShouldBeValid()
  124. {
  125. var ci = _mapper.GetColumnInfo(typeof(EntityWithAttributes).GetProperty(nameof(EntityWithAttributes.ColumnOne)));
  126. ci.ColumnName.ShouldBe("Col1");
  127. ci.ResultColumn.ShouldBeFalse();
  128. ci.ForceToUtc.ShouldBeFalse();
  129. ci = _mapper.GetColumnInfo(typeof(EntityWithAttributes).GetProperty(nameof(EntityWithAttributes.ColumnTwo)));
  130. ci.InsertTemplate.ShouldBe("test");
  131. ci.UpdateTemplate.ShouldBe("test1");
  132. }
  133. [Fact]
  134. public void GetColumnInfo_GivenColumnWithIgnoreAttribute_ShouldBeNull()
  135. {
  136. var ci = _mapper.GetColumnInfo(typeof(EntityWithAttributes).GetProperty(nameof(EntityWithAttributes.NonPersistedColumn)));
  137. ci.ShouldBeNull();
  138. }
  139. [Fact]
  140. public void GetColumnInfo_GivenColumnWithNoMapping_ShouldBeNull()
  141. {
  142. var ci = _mapper.GetColumnInfo(typeof(EntityWithAttributes).GetProperty(nameof(EntityWithAttributes.UnmappedColumn)));
  143. ci.ShouldBeNull();
  144. }
  145. [Fact]
  146. public void GetColumnInfo_GivenColumnAndInflectionInterceptSet_ShouldBeValid()
  147. {
  148. _mapper.InflectColumnName = (inflector, cn) => inflector.Underscore(cn).ToLowerInvariant();
  149. var ci = _mapper.GetColumnInfo(typeof(Order).GetProperty(nameof(Order.CreatedOn)));
  150. ci.ColumnName.ShouldBe("created_on");
  151. ci.ForceToUtc.ShouldBeFalse();
  152. ci.ResultColumn.ShouldBeFalse();
  153. }
  154. [Fact]
  155. public void GetTableInfo_GivenEntityAndInflectionInterceptSet_ShouldBeValid()
  156. {
  157. _mapper.InflectTableName = (inflector, tn) => inflector.Underscore(tn).ToLowerInvariant();
  158. var ti = _mapper.GetTableInfo(typeof(OrderLine));
  159. ti.TableName.ShouldBe("order_line");
  160. ti.PrimaryKey.ShouldBe("OrderLine_Id");
  161. ti.AutoIncrement.ShouldBeTrue();
  162. ti.SequenceName.ShouldBeNull();
  163. }
  164. [TableName("Test1")]
  165. [PrimaryKey("ThatId", AutoIncrement = true)]
  166. [ExplicitColumns]
  167. public class EntityWithAttributes
  168. {
  169. [Column]
  170. public int ThatId { get; set; }
  171. [ResultColumn]
  172. public string Result { get; set; }
  173. [Column("Col1")]
  174. public string ColumnOne { get; set; }
  175. [Ignore]
  176. public int NonPersistedColumn { get; set; }
  177. public DateTime UnmappedColumn { get; set; }
  178. [Column(InsertTemplate = "test", UpdateTemplate = "test1")]
  179. public string ColumnTwo { get; set; }
  180. }
  181. public class EmptyValueConverterAttribute : ValueConverterAttribute
  182. {
  183. public override object ConvertFromDb(object value)
  184. {
  185. return value;
  186. }
  187. public override object ConvertToDb(object value)
  188. {
  189. return value;
  190. }
  191. }
  192. public class Order
  193. {
  194. public long OrderId { get; set; }
  195. public DateTime CreatedOn { get; set; }
  196. [EmptyValueConverter]
  197. public string PO { get; set; }
  198. public decimal? Discount { get; set; }
  199. }
  200. public class OrderLine
  201. {
  202. public int OrderLine_Id { get; set; }
  203. public long OrderId { get; set; }
  204. public short Quantity { get; set; }
  205. public decimal Cost { get; set; }
  206. public string Description { get; set; }
  207. }
  208. public class Product
  209. {
  210. public ushort Id { get; set; }
  211. public int BoxId { get; set; }
  212. public decimal ListPrice { get; set; }
  213. }
  214. public class Box
  215. {
  216. public Guid Id { get; set; }
  217. public int Size { get; set; }
  218. }
  219. }
  220. }