BaseDeleteTests.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using System;
  2. using System.Threading.Tasks;
  3. using PetaPoco.Tests.Integration.Models;
  4. using Shouldly;
  5. using Xunit;
  6. namespace PetaPoco.Tests.Integration.Databases
  7. {
  8. public abstract class BaseDeleteTests : BaseDatabase
  9. {
  10. private Note _note = new Note
  11. {
  12. Text = "A test note",
  13. CreatedOn = new DateTime(1955, 1, 11, 4, 2, 4, DateTimeKind.Utc)
  14. };
  15. private Note _note2 = new Note
  16. {
  17. Text = "A test note 2",
  18. CreatedOn = new DateTime(1985, 1, 11, 4, 2, 4, DateTimeKind.Utc)
  19. };
  20. private Order _order = new Order
  21. {
  22. PoNumber = "Peta's Order",
  23. Status = OrderStatus.Accepted,
  24. CreatedOn = new DateTime(1948, 1, 11, 4, 2, 4, DateTimeKind.Utc),
  25. CreatedBy = "Harry"
  26. };
  27. private OrderLine _orderLine = new OrderLine
  28. {
  29. Quantity = 5,
  30. SellPrice = 4.99m,
  31. Status = OrderLineStatus.Pending
  32. };
  33. private Person _person = new Person
  34. {
  35. Id = Guid.NewGuid(),
  36. Age = 18,
  37. Dob = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc),
  38. Height = 180,
  39. Name = "Peta"
  40. };
  41. protected BaseDeleteTests(DBTestProvider provider)
  42. : base(provider)
  43. {
  44. }
  45. [Fact]
  46. public void Delete_GivenPoco_ShouldDeletePoco()
  47. {
  48. // Arrange
  49. DB.Insert(_person);
  50. _order.PersonId = _person.Id;
  51. DB.Insert(_order);
  52. _orderLine.OrderId = _order.Id;
  53. DB.Insert(_orderLine);
  54. DB.Insert(_note);
  55. // Act
  56. DB.Delete(_orderLine);
  57. DB.Delete(_order);
  58. DB.Delete(_person);
  59. DB.Delete(_note);
  60. _person = DB.SingleOrDefault<Person>(_person.Id);
  61. _order = DB.SingleOrDefault<Order>(_order.Id);
  62. _orderLine = DB.SingleOrDefault<OrderLine>(_orderLine.Id);
  63. _note = DB.SingleOrDefault<Note>(_note.Id);
  64. // Assert
  65. _person.ShouldBeNull();
  66. _order.ShouldBeNull();
  67. _orderLine.ShouldBeNull();
  68. _note.ShouldBeNull();
  69. }
  70. [Fact]
  71. public void Delete_GivenPocoOrPrimaryKey_ShouldDeletePoco()
  72. {
  73. DB.Insert(_note);
  74. DB.Insert(_note2);
  75. DB.Insert(_person);
  76. DB.Delete<Person>(_person.Id).ShouldBe(1);
  77. DB.Delete<Note>(_note).ShouldBe(1);
  78. DB.Delete<Note>(new { _note2.Id }).ShouldBe(1);
  79. _person = DB.SingleOrDefault<Person>(_person.Id);
  80. _note = DB.SingleOrDefault<Note>(_note.Id);
  81. _note2 = DB.SingleOrDefault<Note>(_note2.Id);
  82. _person.ShouldBeNull();
  83. _note.ShouldBeNull();
  84. _note2.ShouldBeNull();
  85. }
  86. [Fact]
  87. public void Delete_GivenTableNamePrimaryKeyNameAndPoco_ShouldDeletePoco()
  88. {
  89. DB.Insert(_person);
  90. DB.Insert(_note);
  91. DB.Delete("People", "Id", _person).ShouldBe(1);
  92. DB.Delete("Note", "Id", _note).ShouldBe(1);
  93. _person = DB.SingleOrDefault<Person>(_person.Id);
  94. _note = DB.SingleOrDefault<Note>(_note.Id);
  95. _person.ShouldBeNull();
  96. _note.ShouldBeNull();
  97. }
  98. [Fact]
  99. public void Delete_GivenTableNamePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldDeletePoco()
  100. {
  101. DB.Insert(_person);
  102. DB.Insert(_note);
  103. DB.Delete("People", "Id", _person, _person.Id).ShouldBe(1);
  104. DB.Delete("Note", "Id", _note, _note.Id).ShouldBe(1);
  105. _person = DB.SingleOrDefault<Person>(_person.Id);
  106. _note = DB.SingleOrDefault<Note>(_note.Id);
  107. _person.ShouldBeNull();
  108. _note.ShouldBeNull();
  109. }
  110. [Fact]
  111. public void Delete_GivenSqlAndArgs_ShouldDeletePoco()
  112. {
  113. DB.Insert(_note);
  114. DB.Insert(_person);
  115. DB.Delete<Note>($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id).ShouldBe(1);
  116. DB.Delete<Person>($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id).ShouldBe(1);
  117. _person = DB.SingleOrDefault<Person>(_person.Id);
  118. _note = DB.SingleOrDefault<Note>(_note.Id);
  119. _person.ShouldBeNull();
  120. _note.ShouldBeNull();
  121. }
  122. [Fact]
  123. public void Delete_GivenSql_ShouldDeletePoco()
  124. {
  125. DB.Insert(_note);
  126. DB.Insert(_person);
  127. DB.Delete<Note>(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id)).ShouldBe(1);
  128. DB.Delete<Person>(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id)).ShouldBe(1);
  129. _person = DB.SingleOrDefault<Person>(_person.Id);
  130. _note = DB.SingleOrDefault<Note>(_note.Id);
  131. _person.ShouldBeNull();
  132. _note.ShouldBeNull();
  133. }
  134. [Fact]
  135. public async Task DeleteAsync_GivenPoco_ShouldDeletePoco()
  136. {
  137. // Arrange
  138. await DB.InsertAsync(_person);
  139. _order.PersonId = _person.Id;
  140. await DB.InsertAsync(_order);
  141. _orderLine.OrderId = _order.Id;
  142. await DB.InsertAsync(_orderLine);
  143. await DB.InsertAsync(_note);
  144. // Act
  145. await DB.DeleteAsync(_orderLine);
  146. await DB.DeleteAsync(_order);
  147. await DB.DeleteAsync(_person);
  148. await DB.DeleteAsync(_note);
  149. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  150. _order = await DB.SingleOrDefaultAsync<Order>(_order.Id);
  151. _orderLine = await DB.SingleOrDefaultAsync<OrderLine>(_orderLine.Id);
  152. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  153. // Assert
  154. _person.ShouldBeNull();
  155. _order.ShouldBeNull();
  156. _orderLine.ShouldBeNull();
  157. _note.ShouldBeNull();
  158. }
  159. [Fact]
  160. public async Task DeleteAsync_GivenPocoOrPrimaryKey_ShouldDeletePoco()
  161. {
  162. await DB.InsertAsync(_note);
  163. await DB.InsertAsync(_note2);
  164. await DB.InsertAsync(_person);
  165. (await DB.DeleteAsync<Person>(_person.Id)).ShouldBe(1);
  166. (await DB.DeleteAsync<Note>(_note)).ShouldBe(1);
  167. (await DB.DeleteAsync<Note>(new { _note2.Id })).ShouldBe(1);
  168. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  169. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  170. _note2 = await DB.SingleOrDefaultAsync<Note>(_note2.Id);
  171. _person.ShouldBeNull();
  172. _note.ShouldBeNull();
  173. _note2.ShouldBeNull();
  174. }
  175. [Fact]
  176. public async Task DeleteAsync_GivenTableNamePrimaryKeyNameAndPoco_ShouldDeletePoco()
  177. {
  178. await DB.InsertAsync(_person);
  179. await DB.InsertAsync(_note);
  180. (await DB.DeleteAsync("People", "Id", _person)).ShouldBe(1);
  181. (await DB.DeleteAsync("Note", "Id", _note)).ShouldBe(1);
  182. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  183. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  184. _person.ShouldBeNull();
  185. _note.ShouldBeNull();
  186. }
  187. [Fact]
  188. public async Task DeleteAsync_GivenTableNamePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldDeletePoco()
  189. {
  190. await DB.InsertAsync(_person);
  191. await DB.InsertAsync(_note);
  192. (await DB.DeleteAsync("People", "Id", _person, _person.Id)).ShouldBe(1);
  193. (await DB.DeleteAsync("Note", "Id", _note, _note.Id)).ShouldBe(1);
  194. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  195. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  196. _person.ShouldBeNull();
  197. _note.ShouldBeNull();
  198. }
  199. [Fact]
  200. public async Task DeleteAsync_GivenSqlAndArgs_ShouldDeletePoco()
  201. {
  202. await DB.InsertAsync(_note);
  203. await DB.InsertAsync(_person);
  204. (await DB.DeleteAsync<Note>($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id)).ShouldBe(1);
  205. (await DB.DeleteAsync<Person>($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id)).ShouldBe(1);
  206. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  207. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  208. _person.ShouldBeNull();
  209. _note.ShouldBeNull();
  210. }
  211. [Fact]
  212. public async Task DeleteAsync_GivenSql_ShouldDeletePoco()
  213. {
  214. await DB.InsertAsync(_note);
  215. await DB.InsertAsync(_person);
  216. (await DB.DeleteAsync<Note>(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id))).ShouldBe(1);
  217. (await DB.DeleteAsync<Person>(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id))).ShouldBe(1);
  218. _person = await DB.SingleOrDefaultAsync<Person>(_person.Id);
  219. _note = await DB.SingleOrDefaultAsync<Note>(_note.Id);
  220. _person.ShouldBeNull();
  221. _note.ShouldBeNull();
  222. }
  223. }
  224. }