Skip to content
Permalink
Browse files

Merge pull request #515 from CollaboratingPlatypus/docs-delete

Add documentation for delete
  • Loading branch information...
pleb committed Apr 14, 2019
2 parents fe341cb + 0aaa885 commit 458f0c73aa855f5bf525cec3cdb288895c43e1d3
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,140 @@
using System;
using System.Linq;
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
using PetaPoco.Tests.Integration.Documentation.Pocos;
using Shouldly;
using Xunit;

namespace PetaPoco.Tests.Integration.Documentation
{
[Collection("MssqlTests")]
public class Deletes : BaseDatabase
{
public Deletes()
: base(new MssqlDBTestProvider())
{
PocoData.FlushCaches();
}

[Fact]
public void DeleteByPoco()
{
// Create the person
var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };

// Tell PetaPoco to insert it
DB.Insert(person);

// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);

// Tell PetaPoco to delete it
DB.Delete(person);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteByPrimaryKey()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, tell PetaPoco to delete a note with the id of 1
DB.Delete<Note>(note.Id);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteCustomWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, we'll tell PetaPoco how to delete the note
DB.Delete<Note>("WHERE [Id] = @Id", new { note.Id });

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteCustomSqlWhere()
{
// Clear out any notes and reset the ID sequence counter
DB.Execute("TRUNCATE TABLE [Note]");

// Add a note
var note = new Note { Text = "This is my note", CreatedOn = DateTime.UtcNow };
DB.Insert(note);

// As note.id is auto increment, we should have an id of 1
note.Id.ShouldBe(1);

// Obviously, we should find only one matching note in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @Id", new { note.Id });
count.ShouldBe(1);

// Now, we'll tell PetaPoco how to delete the note
var sql = new Sql();
sql.Where("[Id] = @Id", new { note.Id });
DB.Delete<Note>(sql);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [Note] WHERE [Id] = @0", note.Id);
count.ShouldBe(0);
}

[Fact]
public void DeleteAdvanced()
{
// Create the person
var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };

// Tell PetaPoco to insert it, but to the table SpecificPeople and not People
DB.Insert("SpecificPeople", person);

// Obviously, we find only 1 matching person in the db
var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @Id", new { person.Id });
count.ShouldBe(1);

// Tell PetaPoco to delete it, but in the table SpecificPeople and not People
DB.Delete("SpecificPeople", "Id", person);

// Obviously, we should now have none in the db
count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [SpecificPeople] WHERE [Id] = @0", person.Id);
count.ShouldBe(0);
}
}
}
@@ -3,6 +3,7 @@
using PetaPoco.Core;
using PetaPoco.Tests.Integration.Databases;
using PetaPoco.Tests.Integration.Databases.MSSQL;
using PetaPoco.Tests.Integration.Documentation.Pocos;
using Shouldly;
using Xunit;

@@ -253,120 +254,4 @@ public void InsertDynamicUnknownPocoWithConventionalNaming()
((string) xfile.FileName).ShouldBe((string) clone.FileName);
}
}

public class UnconventionalPoco
{
public int PrimaryKey { get; set; }

public string Text { get; set; }

public void ShouldBe(UnconventionalPoco other)
{
PrimaryKey.ShouldBe(other.PrimaryKey);
Text.ShouldBe(other.Text);
}
}

[TableName("People")]
[PrimaryKey("Id", AutoIncrement = false)]
public class Person
{
[Column]
public Guid Id { get; set; }

[Column(Name = "FullName")]
public string Name { get; set; }

[Column]
public long Age { get; set; }

[Column]
public int Height { get; set; }

[Column]
public DateTime? Dob { get; set; }

[Ignore]
public string NameAndAge => $"{Name} is of {Age}";

public void ShouldBe(Person other)
{
Id.ShouldBe(other.Id);
Name.ShouldBe(other.Name);
Age.ShouldBe(other.Age);
Height.ShouldBe(other.Height);
Dob.ShouldBe(other.Dob);
}
}

public class Note
{
public int Id { get; set; }

public DateTime CreatedOn { get; set; }

public string Text { get; set; }
}

[ExplicitColumns]
[TableName("Orders")]
[PrimaryKey("Id")]
public class Order
{
[Column]
public int Id { get; set; }

[Column]
public Guid PersonId { get; set; }

[Column]
public string PoNumber { get; set; }

[Column]
public DateTime CreatedOn { get; set; }

[Column]
public string CreatedBy { get; set; }

[Column("OrderStatus")]
public OrderStatus Status { get; set; }

public void ShouldBe(Order other)
{
Id.ShouldBe(other.Id);
PersonId.ShouldBe(other.PersonId);
PoNumber.ShouldBe(other.PoNumber);
Status.ShouldBe(other.Status);
CreatedOn.ShouldBe(other.CreatedOn);
CreatedBy.ShouldBe(other.CreatedBy);
}
}

public enum OrderStatus
{
Pending,
Accepted,
Rejected,
Deleted
}

[TableName("OrderLines")]
[PrimaryKey("Id")]
public class OrderLine
{
[Column]
public int Id { get; set; }

[Column]
public int OrderId { get; set; }

[Column(Name = "Qty")]
public short Quantity { get; set; }

[Column]
public decimal SellPrice { get; set; }

[ResultColumn]
public decimal Total { get; set; }
}
}
@@ -0,0 +1,13 @@
using System;

namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
public class Note
{
public int Id { get; set; }

public DateTime CreatedOn { get; set; }

public string Text { get; set; }
}
}
@@ -0,0 +1,39 @@
using System;
using Shouldly;

namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
[ExplicitColumns]
[TableName("Orders")]
[PrimaryKey("Id")]
public class Order
{
[Column]
public int Id { get; set; }

[Column]
public Guid PersonId { get; set; }

[Column]
public string PoNumber { get; set; }

[Column]
public DateTime CreatedOn { get; set; }

[Column]
public string CreatedBy { get; set; }

[Column("OrderStatus")]
public OrderStatus Status { get; set; }

public void ShouldBe(Order other)
{
Id.ShouldBe(other.Id);
PersonId.ShouldBe(other.PersonId);
PoNumber.ShouldBe(other.PoNumber);
Status.ShouldBe(other.Status);
CreatedOn.ShouldBe(other.CreatedOn);
CreatedBy.ShouldBe(other.CreatedBy);
}
}
}
@@ -0,0 +1,22 @@
namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
[TableName("OrderLines")]
[PrimaryKey("Id")]
public class OrderLine
{
[Column]
public int Id { get; set; }

[Column]
public int OrderId { get; set; }

[Column(Name = "Qty")]
public short Quantity { get; set; }

[Column]
public decimal SellPrice { get; set; }

[ResultColumn]
public decimal Total { get; set; }
}
}
@@ -0,0 +1,10 @@
namespace PetaPoco.Tests.Integration.Documentation.Pocos
{
public enum OrderStatus
{
Pending,
Accepted,
Rejected,
Deleted
}
}
Oops, something went wrong.

0 comments on commit 458f0c7

Please sign in to comment.
You can’t perform that action at this time.