Delete.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using PetaPoco.Core;
  3. using PetaPoco.Tests.Integration.Databases;
  4. using PetaPoco.Tests.Integration.Databases.MSSQL;
  5. using PetaPoco.Tests.Integration.Documentation.Pocos;
  6. using Shouldly;
  7. using Xunit;
  8. namespace PetaPoco.Tests.Integration.Documentation
  9. {
  10. [Collection("MssqlTests")]
  11. public class Deletes : BaseDatabase
  12. {
  13. public Deletes()
  14. : base(new MssqlDBTestProvider())
  15. {
  16. PocoData.FlushCaches();
  17. }
  18. [Fact]
  19. public void DeleteByPoco()
  20. {
  21. // Create the person
  22. var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };
  23. // Tell PetaPoco to insert it
  24. DB.Insert(person);
  25. // Obviously, we find only 1 matching person in the db
  26. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });
  27. count.ShouldBe(1);
  28. // Tell PetaPoco to delete it
  29. DB.Delete(person);
  30. // Obviously, we should now have none in the db
  31. count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);
  32. count.ShouldBe(0);
  33. }
  34. [Fact]
  35. public void DeleteByPrimaryKey()
  36. {
  37. // Clear out any notes and reset the ID sequence counter
  38. DB.Execute("TRUNCATE TABLE [Note]");
  39. // Add a note
  40. var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
  41. DB.Insert(note);
  42. // As note.id is auto increment, we should have an id of 1
  43. note.Id.ShouldBe(1);
  44. // Obviously, we should find only one matching note in the db
  45. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
  46. count.ShouldBe(1);
  47. // Now, tell PetaPoco to delete a note with the id of 1
  48. DB.Delete<Note>(note.Id);
  49. // Obviously, we should now have none in the db
  50. count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
  51. count.ShouldBe(0);
  52. }
  53. [Fact]
  54. public void DeleteCustomWhere()
  55. {
  56. // Clear out any notes and reset the ID sequence counter
  57. DB.Execute("TRUNCATE TABLE [Note]");
  58. // Add a note
  59. var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
  60. DB.Insert(note);
  61. // As note.id is auto increment, we should have an id of 1
  62. note.Id.ShouldBe(1);
  63. // Obviously, we should find only one matching note in the db
  64. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
  65. count.ShouldBe(1);
  66. // Now, we'll tell PetaPoco how to delete the note
  67. DB.Delete<Note>("WHERE [Id] = @Id", new { note.Id });
  68. // Obviously, we should now have none in the db
  69. count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
  70. count.ShouldBe(0);
  71. }
  72. [Fact]
  73. public void DeleteCustomSqlWhere()
  74. {
  75. // Clear out any notes and reset the ID sequence counter
  76. DB.Execute("TRUNCATE TABLE [Note]");
  77. // Add a note
  78. var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
  79. DB.Insert(note);
  80. // As note.id is auto increment, we should have an id of 1
  81. note.Id.ShouldBe(1);
  82. // Obviously, we should find only one matching note in the db
  83. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
  84. count.ShouldBe(1);
  85. // Now, we'll tell PetaPoco how to delete the note
  86. var sql = new Sql();
  87. sql.Where("[Id] = @Id", new { note.Id });
  88. DB.Delete<Note>(sql);
  89. // Obviously, we should now have none in the db
  90. count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
  91. count.ShouldBe(0);
  92. }
  93. [Fact]
  94. public void DeleteAdvanced()
  95. {
  96. // Create the person
  97. var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };
  98. // Tell PetaPoco to insert it, but to the table SpecificPeople and not People
  99. DB.Insert("SpecificPeople", person);
  100. // Obviously, we find only 1 matching person in the db
  101. var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @Id", new { person.Id });
  102. count.ShouldBe(1);
  103. // Tell PetaPoco to delete it, but in the table SpecificPeople and not People
  104. DB.Delete("SpecificPeople", "Id", person);
  105. // Obviously, we should now have none in the db
  106. count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @0", person.Id);
  107. count.ShouldBe(0);
  108. }
  109. }
  110. }