using System; using System.Threading.Tasks; using PetaPoco.Tests.Integration.Models; using Shouldly; using Xunit; namespace PetaPoco.Tests.Integration.Databases { public abstract class BaseDeleteTests : BaseDatabase { private Note _note = new Note { Text = "A test note", CreatedOn = new DateTime(1955, 1, 11, 4, 2, 4, DateTimeKind.Utc) }; private Note _note2 = new Note { Text = "A test note 2", CreatedOn = new DateTime(1985, 1, 11, 4, 2, 4, DateTimeKind.Utc) }; private Order _order = new Order { PoNumber = "Peta's Order", Status = OrderStatus.Accepted, CreatedOn = new DateTime(1948, 1, 11, 4, 2, 4, DateTimeKind.Utc), CreatedBy = "Harry" }; private OrderLine _orderLine = new OrderLine { Quantity = 5, SellPrice = 4.99m, Status = OrderLineStatus.Pending }; private Person _person = new Person { Id = Guid.NewGuid(), Age = 18, Dob = new DateTime(1945, 1, 12, 5, 9, 4, DateTimeKind.Utc), Height = 180, Name = "Peta" }; protected BaseDeleteTests(DBTestProvider provider) : base(provider) { } [Fact] public void Delete_GivenPoco_ShouldDeletePoco() { // Arrange DB.Insert(_person); _order.PersonId = _person.Id; DB.Insert(_order); _orderLine.OrderId = _order.Id; DB.Insert(_orderLine); DB.Insert(_note); // Act DB.Delete(_orderLine); DB.Delete(_order); DB.Delete(_person); DB.Delete(_note); _person = DB.SingleOrDefault(_person.Id); _order = DB.SingleOrDefault(_order.Id); _orderLine = DB.SingleOrDefault(_orderLine.Id); _note = DB.SingleOrDefault(_note.Id); // Assert _person.ShouldBeNull(); _order.ShouldBeNull(); _orderLine.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public void Delete_GivenPocoOrPrimaryKey_ShouldDeletePoco() { DB.Insert(_note); DB.Insert(_note2); DB.Insert(_person); DB.Delete(_person.Id).ShouldBe(1); DB.Delete(_note).ShouldBe(1); DB.Delete(new { _note2.Id }).ShouldBe(1); _person = DB.SingleOrDefault(_person.Id); _note = DB.SingleOrDefault(_note.Id); _note2 = DB.SingleOrDefault(_note2.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); _note2.ShouldBeNull(); } [Fact] public void Delete_GivenTableNamePrimaryKeyNameAndPoco_ShouldDeletePoco() { DB.Insert(_person); DB.Insert(_note); DB.Delete("People", "Id", _person).ShouldBe(1); DB.Delete("Note", "Id", _note).ShouldBe(1); _person = DB.SingleOrDefault(_person.Id); _note = DB.SingleOrDefault(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public void Delete_GivenTableNamePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldDeletePoco() { DB.Insert(_person); DB.Insert(_note); DB.Delete("People", "Id", _person, _person.Id).ShouldBe(1); DB.Delete("Note", "Id", _note, _note.Id).ShouldBe(1); _person = DB.SingleOrDefault(_person.Id); _note = DB.SingleOrDefault(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public void Delete_GivenSqlAndArgs_ShouldDeletePoco() { DB.Insert(_note); DB.Insert(_person); DB.Delete($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id).ShouldBe(1); DB.Delete($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id).ShouldBe(1); _person = DB.SingleOrDefault(_person.Id); _note = DB.SingleOrDefault(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public void Delete_GivenSql_ShouldDeletePoco() { DB.Insert(_note); DB.Insert(_person); DB.Delete(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id)).ShouldBe(1); DB.Delete(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id)).ShouldBe(1); _person = DB.SingleOrDefault(_person.Id); _note = DB.SingleOrDefault(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenPoco_ShouldDeletePoco() { // Arrange await DB.InsertAsync(_person); _order.PersonId = _person.Id; await DB.InsertAsync(_order); _orderLine.OrderId = _order.Id; await DB.InsertAsync(_orderLine); await DB.InsertAsync(_note); // Act await DB.DeleteAsync(_orderLine); await DB.DeleteAsync(_order); await DB.DeleteAsync(_person); await DB.DeleteAsync(_note); _person = await DB.SingleOrDefaultAsync(_person.Id); _order = await DB.SingleOrDefaultAsync(_order.Id); _orderLine = await DB.SingleOrDefaultAsync(_orderLine.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); // Assert _person.ShouldBeNull(); _order.ShouldBeNull(); _orderLine.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenPocoOrPrimaryKey_ShouldDeletePoco() { await DB.InsertAsync(_note); await DB.InsertAsync(_note2); await DB.InsertAsync(_person); (await DB.DeleteAsync(_person.Id)).ShouldBe(1); (await DB.DeleteAsync(_note)).ShouldBe(1); (await DB.DeleteAsync(new { _note2.Id })).ShouldBe(1); _person = await DB.SingleOrDefaultAsync(_person.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); _note2 = await DB.SingleOrDefaultAsync(_note2.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); _note2.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenTableNamePrimaryKeyNameAndPoco_ShouldDeletePoco() { await DB.InsertAsync(_person); await DB.InsertAsync(_note); (await DB.DeleteAsync("People", "Id", _person)).ShouldBe(1); (await DB.DeleteAsync("Note", "Id", _note)).ShouldBe(1); _person = await DB.SingleOrDefaultAsync(_person.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenTableNamePrimaryKeyNamePocoAndPrimaryKeyValue_ShouldDeletePoco() { await DB.InsertAsync(_person); await DB.InsertAsync(_note); (await DB.DeleteAsync("People", "Id", _person, _person.Id)).ShouldBe(1); (await DB.DeleteAsync("Note", "Id", _note, _note.Id)).ShouldBe(1); _person = await DB.SingleOrDefaultAsync(_person.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenSqlAndArgs_ShouldDeletePoco() { await DB.InsertAsync(_note); await DB.InsertAsync(_person); (await DB.DeleteAsync($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id)).ShouldBe(1); (await DB.DeleteAsync($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id)).ShouldBe(1); _person = await DB.SingleOrDefaultAsync(_person.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } [Fact] public async Task DeleteAsync_GivenSql_ShouldDeletePoco() { await DB.InsertAsync(_note); await DB.InsertAsync(_person); (await DB.DeleteAsync(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _note.Id))).ShouldBe(1); (await DB.DeleteAsync(new Sql($"WHERE {DB.Provider.EscapeSqlIdentifier("Id")} = @0", _person.Id))).ShouldBe(1); _person = await DB.SingleOrDefaultAsync(_person.Id); _note = await DB.SingleOrDefaultAsync(_note.Id); _person.ShouldBeNull(); _note.ShouldBeNull(); } } }