From 0c51fa72bd85869168ba090deb1d312fe7939517 Mon Sep 17 00:00:00 2001 From: Tom Rambeau Date: Thu, 15 Feb 2024 15:18:00 +0100 Subject: [PATCH] fin part2 --- tp1/Entities/BookEntity.cs | 3 +- ...igner.cs => 20240215122943_Mg.Designer.cs} | 204 ++++++++++-------- ...40209155948_Mg.cs => 20240215122943_Mg.cs} | 146 +++++++------ .../Migrations/StubbedContextModelSnapshot.cs | 34 ++- tp1/StubbedContextLib/StubbedContext.cs | 35 ++- tp1/TestStub/Program.cs | 42 ++-- tp1/TestStub/tp.Books.db | Bin 28672 -> 28672 bytes tp1/TestStub/tp.Books.db-shm | Bin 0 -> 32768 bytes tp1/TestStub/tp.Books.db-wal | Bin 0 -> 1145392 bytes tp1/UnitTests/UnitTest1.cs | 169 ++++++++++++++- 10 files changed, 432 insertions(+), 201 deletions(-) rename tp1/StubbedContextLib/Migrations/{20240209155948_Mg.Designer.cs => 20240215122943_Mg.Designer.cs} (70%) rename tp1/StubbedContextLib/Migrations/{20240209155948_Mg.cs => 20240215122943_Mg.cs} (61%) create mode 100644 tp1/TestStub/tp.Books.db-shm create mode 100644 tp1/TestStub/tp.Books.db-wal 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 4c5cf76d481389f7d7952d73cbdbf72f73c106b7..53ca108a44bc38508bdeadc6f3f1efdbcc3aa2cd 100644 GIT binary patch delta 542 zcmZp8z}WDBaY7c~QU<PnH#09KwZbzZ-YGvn z+cmEwv!pWKzdSFs$TI~Y!tNa8>KNjx;OXb$8lj+sRkf0Wzn_8AVaK42=Ar82CSJ7F4*!KQVw$iieSfK~yw3KRLf7wYUVNfQkP; z1OEs9`h>MGJGJs4p qLJ-1?>_DqTA>trm5at5g_??0O5C3AGc{lUubD!KSC=ke9pTfp2ZY;~#$eNj#l3L*z5$}|r zpIscBS`zPHo|jtWnF8apI|sQshPWzt`nkA9C@5i3siffVr=SE=ucV-%1T{iQvzd{V zU0ha{u{E?LF)1e%rp75>6Ji+~)bJ2TCm&Z7GkCcafI!L9WpXr+8Vkr_liPVt@PS~hG+3+$k z%QM2=sRuNWmlGxlcTxgB6oO)vzkY$FxHt#Uh@zC#qLS3&5)&|?3)I^tEGx^w;O-xi zlUQ650+fQ$K)Ebwpe}~o%;J*7>{O5rLogv>$G=gh*SkGk|0mvDl=; p1=I%)$OrsT2z2#Depv-w8=!=gHINXsVr6Dz)V2gNxj-x(3jhFc%69+& diff --git a/tp1/TestStub/tp.Books.db-shm b/tp1/TestStub/tp.Books.db-shm new file mode 100644 index 0000000000000000000000000000000000000000..ac1fa32fc1b49c7cc870bd7c89bb5a227e9a954e GIT binary patch literal 32768 zcmeI)Rd5tR5XSNUKm?(Ps=g1dWgcZcBa?(XjH5FCQLySwcbMX@hHcz}Z9 z>#Eu9n(Le0`|Zv>w%65UIHHX;40uGa4z5=26El1D{2qG)B0fx9)O)r6+;u*Fz9SDr zpX&9C^^Ub2;c2b=$rfK`za5>d)Y%#?)^J?kakl4L5t;a; zCOd^FOHF)fMMrutnDNYIH9I-RWghaL5PGlwg-5D;475N{tJ9(j!EhrqVF*h&A`*pY zL?QI;ZG@=PjX-;d} z(vD7ap({PRs+i*#>u!EHxtr?G9D(Z39r2y=(pm3 zDRi>>iw4IX4?GEv7g32pEaH-oBqSpxX~{q)vXYZL*rdBgO)!vFvP literal 0 HcmV?d00001 diff --git a/tp1/TestStub/tp.Books.db-wal b/tp1/TestStub/tp.Books.db-wal new file mode 100644 index 0000000000000000000000000000000000000000..de876e1aad56a7c7f5139f1528b1a20551755760 GIT binary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mY**5I_KdsVYFcz>wkvD)Yw+?5HB4JW;NAf$IOa-yxrP@a}1u zc!56Z3ry92b&MMU1Q0*~0R#|0009ILK%l?{MiMXJ^Cg=V$_Tw?l<7W&OJD!=k{`X; z=r?@|RZuD2CGVF0_fnmFn@zkBKmY**5I_I{1Q0*~0R#|0U@!r_T1^jy8{!>yw9$^* zDB%J!>LN?4Fc^<)8@7d?HCm#te3YZ&f;ss2XLi!XM4PFQ!fB*srAb.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