BaseInsertTests.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 BaseInsertTests : BaseDatabase
  9. {
  10. private readonly Note _note = new Note
  11. {
  12. Text = "PetaPoco's note",
  13. CreatedOn = new DateTime(1948, 1, 11, 4, 2, 4, DateTimeKind.Utc)
  14. };
  15. private readonly Order _order = new Order
  16. {
  17. PoNumber = "Peta's Order",
  18. Status = OrderStatus.Accepted,
  19. CreatedOn = new DateTime(1948, 1, 11, 4, 2, 4, DateTimeKind.Utc),
  20. CreatedBy = "Harry"
  21. };
  22. private readonly OrderLine _orderLine = new OrderLine
  23. {
  24. Quantity = 5,
  25. SellPrice = 4.99m,
  26. Status = OrderLineStatus.Pending
  27. };
  28. private readonly Person _person = new Person
  29. {
  30. Id = Guid.NewGuid(),
  31. Age = 18,
  32. Dob = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc),
  33. Height = 180,
  34. Name = "Peta"
  35. };
  36. protected BaseInsertTests(DBTestProvider provider)
  37. : base(provider)
  38. {
  39. }
  40. [Fact]
  41. public void Insert_GivenPoco_ShouldBeValid()
  42. {
  43. var id = DB.Insert(_person);
  44. var personOther = DB.Single<Person>(_person.Id);
  45. _person.Id.ShouldBe(id);
  46. personOther.ShouldNotBeNull();
  47. personOther.ShouldBe(_person);
  48. }
  49. [Fact]
  50. public void Insert_WhenInsertingRelatedPocosAndGivenPoco_ShouldInsertPocos()
  51. {
  52. DB.Insert(_person);
  53. _order.PersonId = _person.Id;
  54. DB.Insert(_order);
  55. _orderLine.OrderId = _order.Id;
  56. DB.Insert(_orderLine);
  57. var personOther = DB.Single<Person>(_person.Id);
  58. var orderOther = DB.Single<Order>(_order.Id);
  59. var orderLineOther = DB.Single<OrderLine>(_orderLine.Id);
  60. personOther.ShouldNotBeNull();
  61. personOther.ShouldBe(_person);
  62. orderOther.ShouldNotBeNull();
  63. orderOther.ShouldBe(_order);
  64. orderLineOther.ShouldNotBeNull();
  65. orderLineOther.ShouldBe(_orderLine);
  66. }
  67. [Fact]
  68. public void Insert_GivenPocoTableNameAndColumnName_ShouldInsertPoco()
  69. {
  70. DB.Insert("SpecificPeople", "Id", false, _person);
  71. var personOther =
  72. DB.Single<Person>($"SELECT * From {DB.Provider.EscapeTableName("SpecificPeople")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0",
  73. _person.Id);
  74. personOther.ShouldNotBeNull();
  75. personOther.ShouldBe(_person);
  76. }
  77. [Fact]
  78. public void Insert_WhenInsertingRelatedPocosGivenPocoTableNameAndColumnName_ShouldInsertPocos()
  79. {
  80. DB.Insert("SpecificPeople", "Id", false, _person);
  81. _order.PersonId = _person.Id;
  82. DB.Insert("SpecificOrders", "Id", _order);
  83. _orderLine.OrderId = _order.Id;
  84. DB.Insert("SpecificOrderLines", "Id", _orderLine);
  85. var personOther =
  86. DB.Single<Person>($"SELECT * FROM {DB.Provider.EscapeTableName("SpecificPeople")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0",
  87. _person.Id);
  88. var orderOther =
  89. DB.Single<Order>($"SELECT * FROM {DB.Provider.EscapeTableName("SpecificOrders")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0",
  90. _order.Id);
  91. var orderLineOther =
  92. DB.Single<OrderLine>($"SELECT * FROM {DB.Provider.EscapeTableName("SpecificOrderLines")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0",
  93. _orderLine.Id);
  94. personOther.ShouldNotBeNull();
  95. personOther.ShouldBe(_person);
  96. orderOther.ShouldNotBeNull();
  97. orderOther.ShouldBe(_order);
  98. orderLineOther.ShouldNotBeNull();
  99. orderLineOther.ShouldBe(_orderLine);
  100. }
  101. [Fact]
  102. public void Insert_GivenTableNameAndAnonymousType_ShouldInsertPoco()
  103. {
  104. var log = new { Description = "Test log", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  105. var logId = DB.Insert("TransactionLogs", log);
  106. var otherLog = DB.Single<TransactionLog>($"SELECT * FROM {DB.Provider.EscapeTableName("TransactionLogs")}");
  107. logId.ShouldBeNull();
  108. otherLog.Description.ShouldBe(log.Description);
  109. otherLog.CreatedOn.ShouldBe(log.CreatedOn);
  110. }
  111. [Fact]
  112. public void Insert_GivenNullByteArray_ShouldNotThrow()
  113. {
  114. DB.Insert("BugInvestigation_10R9LZYK", "Id", true, new { TestColumn1 = (byte[]) null });
  115. DB.ExecuteScalar<int>($"SELECT * FROM {DB.Provider.EscapeTableName("BugInvestigation_10R9LZYK")}").ShouldBe(1);
  116. }
  117. [Fact]
  118. public void Insert_GivenNonNullByteArray_ShouldNotThrow()
  119. {
  120. DB.Insert("BugInvestigation_10R9LZYK", "Id", true, new { TestColumn1 = new byte[] { 1, 2, 3 } });
  121. DB.ExecuteScalar<int>($"SELECT * FROM {DB.Provider.EscapeTableName("BugInvestigation_10R9LZYK")}").ShouldBe(1);
  122. }
  123. [Fact]
  124. public void Insert_GivenPocoWithNullDateTime_ShouldNotThrow()
  125. {
  126. _person.Dob = null;
  127. var id = DB.Insert(_person);
  128. var personOther = DB.Single<Person>(_person.Id);
  129. _person.Id.ShouldBe(id);
  130. personOther.ShouldNotBeNull();
  131. personOther.ShouldBe(_person);
  132. }
  133. [Fact]
  134. public void Insert_GivenConventionalPocoWithTable_ShouldInsertPoco()
  135. {
  136. var id = DB.Insert("People", _person);
  137. var otherPerson = DB.Single<Person>(id);
  138. _person.Id.ShouldBe(id);
  139. otherPerson.ShouldNotBeNull();
  140. otherPerson.ShouldBe(_person);
  141. }
  142. [Fact]
  143. public void Insert_GivenConventionalPocoWithTableAndPrimaryKey_ShouldInsertPoco()
  144. {
  145. var id = DB.Insert("People", "Id", _person);
  146. var otherPerson = DB.Single<Person>(id);
  147. _person.Id.ShouldBe(id);
  148. otherPerson.ShouldNotBeNull();
  149. otherPerson.ShouldBe(_person);
  150. }
  151. [Fact]
  152. public void Insert_GivenPocoWithPrimaryNullableValueType_ShouldBeValid()
  153. {
  154. var note = new NoteNullablePrimary() { Text = _note.Text, CreatedOn = _note.CreatedOn };
  155. var id = DB.Insert(note);
  156. var noteOther = DB.Single<NoteNullablePrimary>(note.Id);
  157. note.Id.ShouldBe(id);
  158. noteOther.ShouldNotBeNull();
  159. noteOther.ShouldBe(note);
  160. }
  161. [Fact]
  162. public void Insert_GivenTableNamePrimaryKeyNameAndAnonymousType_ShouldInsertPoco()
  163. {
  164. var note = new { Text = "Test note", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  165. var id = DB.Insert("Note", "Id", note);
  166. var otherNote = DB.Single<Note>(id);
  167. otherNote.Text.ShouldBe(note.Text);
  168. otherNote.CreatedOn.ShouldBe(note.CreatedOn);
  169. }
  170. [Fact]
  171. public void Insert_GivenTableNamePrimaryKeyNameAndAnonymousTypeWithNullablePrimaryKey_ShouldInsertPoco()
  172. {
  173. var note = new { Id = (int?) null, Text = "Test note", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  174. var id = DB.Insert("Note", "Id", note);
  175. var otherNote = DB.Single<Note>(id);
  176. id.ShouldNotBeNull();
  177. otherNote.Text.ShouldBe(note.Text);
  178. otherNote.CreatedOn.ShouldBe(note.CreatedOn);
  179. }
  180. [Fact]
  181. public void Insert_GivenTableNamePrimaryKeyNameAndAnonymousTypeWithStaticPrimaryKey_ShouldInsertPoco()
  182. {
  183. var person = new { Id = Guid.NewGuid(), Age = 18, Dob = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc), Height = 180, FullName = "Peta" };
  184. var id = DB.Insert("People", "Id", person);
  185. var otherPerson = DB.Single<Person>(id);
  186. id.ShouldNotBeNull();
  187. otherPerson.Id.ShouldBe(person.Id);
  188. otherPerson.Age.ShouldBe(person.Age);
  189. otherPerson.Dob.ShouldBe(person.Dob);
  190. otherPerson.Height.ShouldBe(person.Height);
  191. otherPerson.Name.ShouldBe(person.FullName);
  192. }
  193. [Fact]
  194. public async Task InsertAsync_GivenPoco_ShouldBeValid()
  195. {
  196. var id = await DB.InsertAsync(_person);
  197. var personOther = await DB.SingleAsync<Person>(_person.Id);
  198. _person.Id.ShouldBe(id);
  199. personOther.ShouldNotBeNull();
  200. personOther.ShouldBe(_person);
  201. }
  202. [Fact]
  203. public async Task InsertAsync_WhenInsertingRelatedPocosAndGivenPoco_ShouldInsertPocos()
  204. {
  205. await DB.InsertAsync(_person);
  206. _order.PersonId = _person.Id;
  207. await DB.InsertAsync(_order);
  208. _orderLine.OrderId = _order.Id;
  209. await DB.InsertAsync(_orderLine);
  210. var personOther = await DB.SingleAsync<Person>(_person.Id);
  211. var orderOther = await DB.SingleAsync<Order>(_order.Id);
  212. var orderLineOther = await DB.SingleAsync<OrderLine>(_orderLine.Id);
  213. personOther.ShouldNotBeNull();
  214. personOther.ShouldBe(_person);
  215. orderOther.ShouldNotBeNull();
  216. orderOther.ShouldBe(_order);
  217. orderLineOther.ShouldNotBeNull();
  218. orderLineOther.ShouldBe(_orderLine);
  219. }
  220. [Fact]
  221. public async Task InsertAsync_GivenPocoTableNameAndColumnName_ShouldInsertPoco()
  222. {
  223. await DB.InsertAsync("SpecificPeople", "Id", false, _person);
  224. var personOther =
  225. await DB.SingleAsync<Person>(
  226. $"SELECT * From {DB.Provider.EscapeTableName("SpecificPeople")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id);
  227. personOther.ShouldNotBeNull();
  228. personOther.ShouldBe(_person);
  229. }
  230. [Fact]
  231. public async Task InsertAsync_WhenInsertingRelatedPocosGivenPocoTableNameAndColumnName_ShouldInsertPocos()
  232. {
  233. await DB.InsertAsync("SpecificPeople", "Id", false, _person);
  234. _order.PersonId = _person.Id;
  235. await DB.InsertAsync("SpecificOrders", "Id", _order);
  236. _orderLine.OrderId = _order.Id;
  237. await DB.InsertAsync("SpecificOrderLines", "Id", _orderLine);
  238. var personOther =
  239. await DB.SingleAsync<Person>(
  240. $"SELECT * FROM {DB.Provider.EscapeTableName("SpecificPeople")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id);
  241. var orderOther =
  242. await DB.SingleAsync<Order>($"SELECT * FROM {DB.Provider.EscapeTableName("SpecificOrders")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0",
  243. _order.Id);
  244. var orderLineOther =
  245. await DB.SingleAsync<OrderLine>(
  246. $"SELECT * FROM {DB.Provider.EscapeTableName("SpecificOrderLines")} WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _orderLine.Id);
  247. personOther.ShouldNotBeNull();
  248. personOther.ShouldBe(_person);
  249. orderOther.ShouldNotBeNull();
  250. orderOther.ShouldBe(_order);
  251. orderLineOther.ShouldNotBeNull();
  252. orderLineOther.ShouldBe(_orderLine);
  253. }
  254. [Fact]
  255. public async Task InsertAsync_GivenTableNameAndAnonymousType_ShouldInsertPoco()
  256. {
  257. var log = new { Description = "Test log", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  258. var logId = await DB.InsertAsync("TransactionLogs", log);
  259. var otherLog = await DB.SingleAsync<TransactionLog>($"SELECT * FROM {DB.Provider.EscapeTableName("TransactionLogs")}");
  260. logId.ShouldBeNull();
  261. otherLog.Description.ShouldBe(log.Description);
  262. otherLog.CreatedOn.ShouldBe(log.CreatedOn);
  263. }
  264. [Fact]
  265. public async Task InsertAsync_GivenNullByteArray_ShouldNotThrow()
  266. {
  267. await DB.InsertAsync("BugInvestigation_10R9LZYK", "Id", true, new { TestColumn1 = (byte[]) null });
  268. (await DB.ExecuteScalarAsync<int>($"SELECT * FROM {DB.Provider.EscapeTableName("BugInvestigation_10R9LZYK")}")).ShouldBe(1);
  269. }
  270. [Fact]
  271. public async Task InsertAsync_GivenNonNullByteArray_ShouldNotThrow()
  272. {
  273. await DB.InsertAsync("BugInvestigation_10R9LZYK", "Id", true, new { TestColumn1 = new byte[] { 1, 2, 3 } });
  274. (await DB.ExecuteScalarAsync<int>($"SELECT * FROM {DB.Provider.EscapeTableName("BugInvestigation_10R9LZYK")}")).ShouldBe(1);
  275. }
  276. [Fact]
  277. public async Task InsertAsync_GivenPocoWithNullDateTime_ShouldNotThrow()
  278. {
  279. _person.Dob = null;
  280. var id = await DB.InsertAsync(_person);
  281. var personOther = await DB.SingleAsync<Person>(_person.Id);
  282. _person.Id.ShouldBe(id);
  283. personOther.ShouldNotBeNull();
  284. personOther.ShouldBe(_person);
  285. }
  286. [Fact]
  287. public async Task InsertAsync_GivenConventionalPocoWithTable_ShouldInsertPoco()
  288. {
  289. var id = await DB.InsertAsync("People", _person);
  290. var otherPerson = await DB.SingleAsync<Person>(id);
  291. _person.Id.ShouldBe(id);
  292. otherPerson.ShouldNotBeNull();
  293. otherPerson.ShouldBe(_person);
  294. }
  295. [Fact]
  296. public async Task InsertAsync_GivenConventionalPocoWithTableAndPrimaryKey_ShouldInsertPoco()
  297. {
  298. var id = await DB.InsertAsync("People", "Id", _person);
  299. var otherPerson = await DB.SingleAsync<Person>(id);
  300. _person.Id.ShouldBe(id);
  301. otherPerson.ShouldNotBeNull();
  302. otherPerson.ShouldBe(_person);
  303. }
  304. [Fact]
  305. public async Task InsertAsync_GivenPocoWithPrimaryNullableValueType_ShouldBeValid()
  306. {
  307. var note = new NoteNullablePrimary { Text = _note.Text, CreatedOn = _note.CreatedOn };
  308. var id = await DB.InsertAsync(note);
  309. var noteOther = await DB.SingleAsync<NoteNullablePrimary>(note.Id);
  310. note.Id.ShouldBe(id);
  311. noteOther.ShouldNotBeNull();
  312. noteOther.ShouldBe(note);
  313. }
  314. [Fact]
  315. public async Task InsertAsync_GivenTableNamePrimaryKeyNameAndAnonymousType_ShouldInsertPoco()
  316. {
  317. var note = new { Text = "Test note", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  318. var id = await DB.InsertAsync("Note", "Id", note);
  319. var otherNote = await DB.SingleAsync<Note>(id);
  320. otherNote.Text.ShouldBe(note.Text);
  321. otherNote.CreatedOn.ShouldBe(note.CreatedOn);
  322. }
  323. [Fact]
  324. public async Task InsertAsync_GivenTableNamePrimaryKeyNameAndAnonymousTypeWithNullablePrimaryKey_ShouldInsertPoco()
  325. {
  326. var note = new { Id = (int?) null, Text = "Test note", CreatedOn = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc) };
  327. var id = await DB.InsertAsync("Note", "Id", note);
  328. var otherNote = await DB.SingleAsync<Note>(id);
  329. id.ShouldNotBeNull();
  330. otherNote.Text.ShouldBe(note.Text);
  331. otherNote.CreatedOn.ShouldBe(note.CreatedOn);
  332. }
  333. [Fact]
  334. public async Task InsertAsync_GivenTableNamePrimaryKeyNameAndAnonymousTypeWithStaticPrimaryKey_ShouldInsertPoco()
  335. {
  336. var person = new { Id = Guid.NewGuid(), Age = 18, Dob = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc), Height = 180, FullName = "Peta" };
  337. var id = await DB.InsertAsync("People", "Id", person);
  338. var otherPerson = await DB.SingleAsync<Person>(id);
  339. id.ShouldNotBeNull();
  340. otherPerson.Id.ShouldBe(person.Id);
  341. otherPerson.Age.ShouldBe(person.Age);
  342. otherPerson.Dob.ShouldBe(person.Dob);
  343. otherPerson.Height.ShouldBe(person.Height);
  344. otherPerson.Name.ShouldBe(person.FullName);
  345. }
  346. }
  347. }