diff --git a/tp1/Entities/BookEntity.cs b/tp1/Entities/BookEntity.cs index 1849e64..d913247 100644 --- a/tp1/Entities/BookEntity.cs +++ b/tp1/Entities/BookEntity.cs @@ -6,10 +6,9 @@ using System.Threading.Tasks; namespace Entities { - public class BookEntity { - public long ID { get; set; } + public long Id { get; set; } public string Title { get; set; } public string Author { get; set; } public string Isbn { get; set; } diff --git a/tp1/StubbedContextLib/Migrations/20240209155948_Mg.Designer.cs b/tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs similarity index 70% rename from tp1/StubbedContextLib/Migrations/20240209155948_Mg.Designer.cs rename to tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs index 154b565..6702883 100644 --- a/tp1/StubbedContextLib/Migrations/20240209155948_Mg.Designer.cs +++ b/tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs @@ -1,89 +1,115 @@ -// -using System; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; -using StubbedContextLib; - -#nullable disable - -namespace StubbedContextLib.Migrations -{ - [DbContext(typeof(StubbedContext))] - [Migration("20240209155948_Mg")] - partial class Mg - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); - - modelBuilder.Entity("Entities.BookEntity", b => - { - b.Property("ID") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("Author") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("Isbn") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("OwnerId") - .HasColumnType("INTEGER"); - - b.Property("PersonId") - .HasColumnType("INTEGER"); - - b.Property("Title") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("ID"); - - b.HasIndex("OwnerId"); - - b.ToTable("BooksSet"); - }); - - modelBuilder.Entity("Entities.PersonEntity", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); - - b.Property("FirstName") - .IsRequired() - .HasColumnType("TEXT"); - - b.Property("LastName") - .IsRequired() - .HasColumnType("TEXT"); - - b.HasKey("Id"); - - b.ToTable("PersonSet"); - }); - - modelBuilder.Entity("Entities.BookEntity", b => - { - b.HasOne("Entities.PersonEntity", "Owner") - .WithMany("Books") - .HasForeignKey("OwnerId"); - - b.Navigation("Owner"); - }); - - modelBuilder.Entity("Entities.PersonEntity", b => - { - b.Navigation("Books"); - }); -#pragma warning restore 612, 618 - } - } -} +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using StubbedContextLib; + +#nullable disable + +namespace StubbedContextLib.Migrations +{ + [DbContext(typeof(StubbedContext))] + [Migration("20240215122943_Mg")] + partial class Mg + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.1"); + + modelBuilder.Entity("Entities.BookEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Author") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Isbn") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("OwnerId") + .HasColumnType("INTEGER"); + + b.Property("PersonId") + .HasColumnType("INTEGER"); + + b.Property("Title") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("OwnerId"); + + b.ToTable("BookEntity"); + + b.HasData( + new + { + Id = 1L, + Author = "test", + Isbn = "test", + PersonId = 1, + Title = "test" + }, + new + { + Id = 2L, + Author = "test2", + Isbn = "test2", + PersonId = 1, + Title = "test2" + }); + }); + + modelBuilder.Entity("Entities.PersonEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("FirstName") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("LastName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("PersonEntity"); + + b.HasData( + new + { + Id = 1, + FirstName = "coco", + LastName = "test" + }); + }); + + modelBuilder.Entity("Entities.BookEntity", b => + { + b.HasOne("Entities.PersonEntity", "Owner") + .WithMany("Books") + .HasForeignKey("OwnerId"); + + b.Navigation("Owner"); + }); + + modelBuilder.Entity("Entities.PersonEntity", b => + { + b.Navigation("Books"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/tp1/StubbedContextLib/Migrations/20240209155948_Mg.cs b/tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs similarity index 61% rename from tp1/StubbedContextLib/Migrations/20240209155948_Mg.cs rename to tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs index f03f257..c2ec2e4 100644 --- a/tp1/StubbedContextLib/Migrations/20240209155948_Mg.cs +++ b/tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs @@ -1,65 +1,81 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace StubbedContextLib.Migrations -{ - /// - public partial class Mg : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "PersonSet", - columns: table => new - { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - FirstName = table.Column(type: "TEXT", nullable: false), - LastName = table.Column(type: "TEXT", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_PersonSet", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "BooksSet", - columns: table => new - { - ID = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), - Title = table.Column(type: "TEXT", nullable: false), - Author = table.Column(type: "TEXT", nullable: false), - Isbn = table.Column(type: "TEXT", nullable: false), - PersonId = table.Column(type: "INTEGER", nullable: false), - OwnerId = table.Column(type: "INTEGER", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_BooksSet", x => x.ID); - table.ForeignKey( - name: "FK_BooksSet_PersonSet_OwnerId", - column: x => x.OwnerId, - principalTable: "PersonSet", - principalColumn: "Id"); - }); - - migrationBuilder.CreateIndex( - name: "IX_BooksSet_OwnerId", - table: "BooksSet", - column: "OwnerId"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "BooksSet"); - - migrationBuilder.DropTable( - name: "PersonSet"); - } - } -} +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace StubbedContextLib.Migrations +{ + /// + public partial class Mg : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "PersonEntity", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + FirstName = table.Column(type: "TEXT", nullable: false), + LastName = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_PersonEntity", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "BookEntity", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Title = table.Column(type: "TEXT", nullable: false), + Author = table.Column(type: "TEXT", nullable: false), + Isbn = table.Column(type: "TEXT", nullable: false), + PersonId = table.Column(type: "INTEGER", nullable: false), + OwnerId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_BookEntity", x => x.Id); + table.ForeignKey( + name: "FK_BookEntity_PersonEntity_OwnerId", + column: x => x.OwnerId, + principalTable: "PersonEntity", + principalColumn: "Id"); + }); + + migrationBuilder.InsertData( + table: "BookEntity", + columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" }, + values: new object[,] + { + { 1L, "test", "test", null, 1, "test" }, + { 2L, "test2", "test2", null, 1, "test2" } + }); + + migrationBuilder.InsertData( + table: "PersonEntity", + columns: new[] { "Id", "FirstName", "LastName" }, + values: new object[] { 1, "coco", "test" }); + + migrationBuilder.CreateIndex( + name: "IX_BookEntity_OwnerId", + table: "BookEntity", + column: "OwnerId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "BookEntity"); + + migrationBuilder.DropTable( + name: "PersonEntity"); + } + } +} diff --git a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs index 3e7e88c..5ecd249 100644 --- a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs +++ b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs @@ -19,7 +19,7 @@ namespace StubbedContextLib.Migrations modelBuilder.Entity("Entities.BookEntity", b => { - b.Property("ID") + b.Property("Id") .ValueGeneratedOnAdd() .HasColumnType("INTEGER"); @@ -41,11 +41,29 @@ namespace StubbedContextLib.Migrations .IsRequired() .HasColumnType("TEXT"); - b.HasKey("ID"); + b.HasKey("Id"); b.HasIndex("OwnerId"); - b.ToTable("BooksSet"); + b.ToTable("BookEntity"); + + b.HasData( + new + { + Id = 1L, + Author = "test", + Isbn = "test", + PersonId = 1, + Title = "test" + }, + new + { + Id = 2L, + Author = "test2", + Isbn = "test2", + PersonId = 1, + Title = "test2" + }); }); modelBuilder.Entity("Entities.PersonEntity", b => @@ -64,7 +82,15 @@ namespace StubbedContextLib.Migrations b.HasKey("Id"); - b.ToTable("PersonSet"); + b.ToTable("PersonEntity"); + + b.HasData( + new + { + Id = 1, + FirstName = "coco", + LastName = "test" + }); }); modelBuilder.Entity("Entities.BookEntity", b => diff --git a/tp1/StubbedContextLib/StubbedContext.cs b/tp1/StubbedContextLib/StubbedContext.cs index 4f79e00..231e61b 100644 --- a/tp1/StubbedContextLib/StubbedContext.cs +++ b/tp1/StubbedContextLib/StubbedContext.cs @@ -12,19 +12,34 @@ namespace StubbedContextLib { public class StubbedContext : LibraryContext { - public List Persons { get; set; } = new List(); - public List Books { get; set; } = new List(); + public DbSet Persons { get; set; } + public DbSet Books { get; set; } public StubbedContext() { - BookEntity b0 = new BookEntity() { Title = "test", Author = "test", Isbn = "test" }; - BookEntity b1 = new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2" }; - Books.Add(b0); - Books.Add(b1); - Collection CB = new Collection(); - CB.Add(b0); - CB.Add(b1); - Persons.Add(new PersonEntity() { FirstName = "coco", LastName = "test", Books = CB }); + + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity().HasData( + new PersonEntity() { FirstName = "coco", LastName = "test", Id = 1 } + ); + + modelBuilder.Entity().HasData( + new BookEntity() { Title = "test", Author = "test", Isbn = "test" , Id = 1, PersonId = 1}, + new BookEntity() { Title = "test2", Author = "test2", Isbn = "test2",Id = 2, PersonId = 1 } + ); + + } + + + public StubbedContext(DbContextOptions options) + : base(options) + { } + } } diff --git a/tp1/TestStub/Program.cs b/tp1/TestStub/Program.cs index abff6f4..ff698eb 100644 --- a/tp1/TestStub/Program.cs +++ b/tp1/TestStub/Program.cs @@ -9,15 +9,16 @@ using Microsoft.EntityFrameworkCore; // See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { context.Database.EnsureCreated(); PersonEntity p1 = new PersonEntity() { FirstName = "Tom", LastName = "Rambeau" }; PersonEntity p2 = new PersonEntity() { FirstName = "Erwan", LastName = "Manager" }; + BookEntity chewie = new BookEntity() { Title = "mistake", Author = "test1", Isbn ="test1"}; - BookEntity the100 = new BookEntity() { Title = "the100", Author = "test4", Isbn = "test4", Owner = p1 }; + BookEntity the100 = new BookEntity() { Title = "the100", Author = "test4", Isbn = "test4"}; BookEntity GOT = new BookEntity() { Title = "GOT", Author = "lastTest", Isbn = "lastTest"}; context.Add(p1); @@ -29,17 +30,17 @@ using (var context = new LibraryContext()) context.SaveChanges(); } -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { foreach (var n in context.BooksSet) { - Console.WriteLine($"Books: {n.ID} - {n.Title}"); + Console.WriteLine($"Books: {n.Id} - {n.Title}"); } context.SaveChanges(); } -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("t")).First(); Console.WriteLine($"{eBooks.Title} (made by {eBooks.Author})"); @@ -47,48 +48,50 @@ using (var context = new LibraryContext()) context.SaveChanges(); } -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { Console.WriteLine("Deletes one item from de database"); var impostor = context.BooksSet .SingleOrDefault(n => n.Title.Equals("mistake")); context.Remove(impostor); + context.PersonSet.Include(pers => pers.Books).ToList(); context.SaveChanges(); } -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { Console.WriteLine("All people"); var people = context.PersonSet; foreach(var p in people) { - Console.WriteLine($"firstname: {p.FirstName}, lastname: {p.LastName}"); + Console.WriteLine($"firstname: {p.FirstName}, lastname: {p.LastName}"); } } //emprunt -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { var books = context.BooksSet; - foreach(var book in books) + + context.PersonSet.Include(pers => pers.Books).ToList(); + var person = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).First(); + foreach (var book in books) { if (book.Owner == null) { - book.Owner = context.PersonSet.Where(b => b.FirstName.StartsWith("E")).Include(b => b.Books).First(); - Console.WriteLine($" nouveau propriétaire du livre: {book.Owner.FirstName}"); + person.Books.Add(book); break; } } - context.SaveChanges(); } //Rendu -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { - + context.PersonSet.Include(pers => pers.Books).ToList(); var books = context.BooksSet; if (books != null) { @@ -97,14 +100,13 @@ using (var context = new LibraryContext()) //Console.WriteLine(book.Owner.FirstName); if (book.Owner != null) { - Console.WriteLine($" propriétaire du livre avant rendu : {book.Owner.FirstName}"); + Console.WriteLine($" \n propriétaire du livre {book.Title} avant rendu : {book.Owner.FirstName}"); book.Owner = null; - Console.WriteLine("propriétaire a rendu le libre"); - break; + Console.WriteLine($" \n propriétaire a rendu le libre {book.Title} "); } else { - Console.WriteLine("livre sans propriétaire" + book.Title); + Console.WriteLine("\n livre sans propriétaire : " + book.Title); } } } @@ -114,7 +116,7 @@ using (var context = new LibraryContext()) //DeleteAllItem -using (var context = new LibraryContext()) +using (var context = new StubbedContext()) { var allBooks = context.BooksSet; if ( allBooks != null) diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db index 4c5cf76..53ca108 100644 Binary files a/tp1/TestStub/tp.Books.db and b/tp1/TestStub/tp.Books.db differ diff --git a/tp1/TestStub/tp.Books.db-shm b/tp1/TestStub/tp.Books.db-shm new file mode 100644 index 0000000..ac1fa32 Binary files /dev/null and b/tp1/TestStub/tp.Books.db-shm differ diff --git a/tp1/TestStub/tp.Books.db-wal b/tp1/TestStub/tp.Books.db-wal new file mode 100644 index 0000000..de876e1 Binary files /dev/null and b/tp1/TestStub/tp.Books.db-wal differ diff --git a/tp1/UnitTests/UnitTest1.cs b/tp1/UnitTests/UnitTest1.cs index d01262c..ad78092 100644 --- a/tp1/UnitTests/UnitTest1.cs +++ b/tp1/UnitTests/UnitTest1.cs @@ -1,4 +1,5 @@ using DbContextLib; +using StubbedContextLib; using Entities; using Microsoft.Extensions.Options; using Microsoft.EntityFrameworkCore; @@ -11,38 +12,184 @@ namespace UnitTests public class UnitTest1 { [Fact] - public void Add_Test() + public void Add_TestBooks() { //connection must be opened to use In-memory database var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); - - var options = new DbContextOptionsBuilder.UseSqlLite().options; + var options = new DbContextOptionsBuilder().UseSqlite().Options; //prepares the database with one instance of the context - using (var context = new LibraryContext(options)) + using (var context = new StubbedContext(options)) { //context.Database.OpenConnection(); context.Database.EnsureCreated(); - - BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "" }; - BookEntity princeOfPersia = new BookEntity ( "princeOfPersia", "", "" ); - BookEntity PercyJackson = new BookEntity ("PercyJackson", "", "" ); + context.PersonSet.Include(pers => pers.Books).ToList(); + BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 }; + BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; + BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; context.BooksSet.Add(the100); context.BooksSet.Add(princeOfPersia); context.BooksSet.Add(PercyJackson); context.SaveChanges(); } + + //uses another instance of the context to do the tests + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + Assert.Equal(5, context.BooksSet.Count()); + Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count()); + } + } + + [Fact] + public void Add_TestPersons() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + context.PersonSet.Include(pers => pers.Books).ToList(); + PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 }; + PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 }; + context.PersonSet.Add(p1); + context.PersonSet.Add(p2); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests - using (var context = new LibraryContext(options)) + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); + Assert.Equal(3, context.PersonSet.Count()); + Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Granc")).Count()); + } + } + + + [Fact] + public void Modify_TestBook() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + + //prepares the database with one instance of the context + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + + context.PersonSet.Include(pers => pers.Books).ToList(); + BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 }; + BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; + BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; + context.BooksSet.Add(the100); + context.BooksSet.Add(princeOfPersia); + context.BooksSet.Add(PercyJackson); + context.SaveChanges(); + } + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + context.PersonSet.Include(pers => pers.Books).ToList(); + string nameToFind = "princeOfPersia"; + Assert.Equal(2, context.Books.Where(n => n.Title.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Books.Where(n => n.Title.ToLower().Contains(nameToFind)).First(); + ewok.Title = "l'Odyssée"; + context.SaveChanges(); + } + } + [Fact] + public void Modify_TestPerson() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + + //prepares the database with one instance of the context + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + + context.PersonSet.Include(pers => pers.Books).ToList(); + PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 }; + PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 }; + context.PersonSet.Add(p1); + context.PersonSet.Add(p2); + context.SaveChanges(); + } + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + context.PersonSet.Include(pers => pers.Books).ToList(); + string nameToFind = "Jean"; + Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).Count()); + var person = context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).First(); + person.FirstName = "Jacques"; + context.SaveChanges(); + } + } + + public void Delete_TestPerson() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + + context.PersonSet.Include(pers => pers.Books).ToList(); + var nb = context.PersonSet.Count(); + if (nb > 0) + { + context.PersonSet.Remove(context.PersonSet.FirstOrDefault()); + } + + Assert.Equal(context.PersonSet.Count(), nb - 1); + context.SaveChanges(); + } + } - Assert.Equal(3, context.BooksSet.Count()); - Assert.Equal("the100", context.BooksSet.First().Title); + public void Delete_TestBook() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder().UseSqlite().Options; + var nb; + //prepares the database with one instance of the context + + //prepares the database with one instance of the context + using (var context = new StubbedContext(options)) + { + + context.PersonSet.Include(pers => pers.Books).ToList(); + nb = context.BooksSet.Count(); + if (nb > 0) + { + context.BooksSet.Remove(context.BooksSet.FirstOrDefault()); + } + Assert.Equal(context.BooksSet.Count, nb - 1); + context.SaveChanges(); } } + + + } } \ No newline at end of file