From e3e85661035ff01d4d6f2ef68c00408c8e3f7a69 Mon Sep 17 00:00:00 2001 From: Tom Rambeau Date: Thu, 15 Feb 2024 15:48:54 +0100 Subject: [PATCH] MAJ des derniers tests --- tp1/Entities/BookEntity.cs | 2 + ...igner.cs => 20240215144801_Mg.Designer.cs} | 6 +- ...40215122943_Mg.cs => 20240215144801_Mg.cs} | 32 ++++---- .../Migrations/StubbedContextModelSnapshot.cs | 4 +- tp1/StubbedContextLib/StubbedContext.cs | 2 - tp1/TestStub/tp.Books.db | Bin 28672 -> 28672 bytes tp1/UnitTests/UnitTest1.cs | 76 +++++++++++++++++- 7 files changed, 97 insertions(+), 25 deletions(-) rename tp1/StubbedContextLib/Migrations/{20240215122943_Mg.Designer.cs => 20240215144801_Mg.Designer.cs} (96%) rename tp1/StubbedContextLib/Migrations/{20240215122943_Mg.cs => 20240215144801_Mg.cs} (81%) diff --git a/tp1/Entities/BookEntity.cs b/tp1/Entities/BookEntity.cs index d913247..25062c3 100644 --- a/tp1/Entities/BookEntity.cs +++ b/tp1/Entities/BookEntity.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Entities { + [Table("TableBook")] public class BookEntity { public long Id { get; set; } diff --git a/tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs b/tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs similarity index 96% rename from tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs rename to tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs index 6702883..91a00cc 100644 --- a/tp1/StubbedContextLib/Migrations/20240215122943_Mg.Designer.cs +++ b/tp1/StubbedContextLib/Migrations/20240215144801_Mg.Designer.cs @@ -11,7 +11,7 @@ using StubbedContextLib; namespace StubbedContextLib.Migrations { [DbContext(typeof(StubbedContext))] - [Migration("20240215122943_Mg")] + [Migration("20240215144801_Mg")] partial class Mg { /// @@ -48,7 +48,7 @@ namespace StubbedContextLib.Migrations b.HasIndex("OwnerId"); - b.ToTable("BookEntity"); + b.ToTable("TableBook"); b.HasData( new @@ -85,7 +85,7 @@ namespace StubbedContextLib.Migrations b.HasKey("Id"); - b.ToTable("PersonEntity"); + b.ToTable("PersonSet"); b.HasData( new diff --git a/tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs b/tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs similarity index 81% rename from tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs rename to tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs index c2ec2e4..1cbb84f 100644 --- a/tp1/StubbedContextLib/Migrations/20240215122943_Mg.cs +++ b/tp1/StubbedContextLib/Migrations/20240215144801_Mg.cs @@ -13,7 +13,7 @@ namespace StubbedContextLib.Migrations protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "PersonEntity", + name: "PersonSet", columns: table => new { Id = table.Column(type: "INTEGER", nullable: false) @@ -23,11 +23,11 @@ namespace StubbedContextLib.Migrations }, constraints: table => { - table.PrimaryKey("PK_PersonEntity", x => x.Id); + table.PrimaryKey("PK_PersonSet", x => x.Id); }); migrationBuilder.CreateTable( - name: "BookEntity", + name: "TableBook", columns: table => new { Id = table.Column(type: "INTEGER", nullable: false) @@ -40,16 +40,21 @@ namespace StubbedContextLib.Migrations }, constraints: table => { - table.PrimaryKey("PK_BookEntity", x => x.Id); + table.PrimaryKey("PK_TableBook", x => x.Id); table.ForeignKey( - name: "FK_BookEntity_PersonEntity_OwnerId", + name: "FK_TableBook_PersonSet_OwnerId", column: x => x.OwnerId, - principalTable: "PersonEntity", + principalTable: "PersonSet", principalColumn: "Id"); }); migrationBuilder.InsertData( - table: "BookEntity", + table: "PersonSet", + columns: new[] { "Id", "FirstName", "LastName" }, + values: new object[] { 1, "coco", "test" }); + + migrationBuilder.InsertData( + table: "TableBook", columns: new[] { "Id", "Author", "Isbn", "OwnerId", "PersonId", "Title" }, values: new object[,] { @@ -57,14 +62,9 @@ namespace StubbedContextLib.Migrations { 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", + name: "IX_TableBook_OwnerId", + table: "TableBook", column: "OwnerId"); } @@ -72,10 +72,10 @@ namespace StubbedContextLib.Migrations protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( - name: "BookEntity"); + name: "TableBook"); migrationBuilder.DropTable( - name: "PersonEntity"); + name: "PersonSet"); } } } diff --git a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs index 5ecd249..061de25 100644 --- a/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs +++ b/tp1/StubbedContextLib/Migrations/StubbedContextModelSnapshot.cs @@ -45,7 +45,7 @@ namespace StubbedContextLib.Migrations b.HasIndex("OwnerId"); - b.ToTable("BookEntity"); + b.ToTable("TableBook"); b.HasData( new @@ -82,7 +82,7 @@ namespace StubbedContextLib.Migrations b.HasKey("Id"); - b.ToTable("PersonEntity"); + b.ToTable("PersonSet"); b.HasData( new diff --git a/tp1/StubbedContextLib/StubbedContext.cs b/tp1/StubbedContextLib/StubbedContext.cs index 231e61b..3012431 100644 --- a/tp1/StubbedContextLib/StubbedContext.cs +++ b/tp1/StubbedContextLib/StubbedContext.cs @@ -12,8 +12,6 @@ namespace StubbedContextLib { public class StubbedContext : LibraryContext { - public DbSet Persons { get; set; } - public DbSet Books { get; set; } public StubbedContext() { diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db index 36be9b0a421b04b91ec5688c2c8193dafa7e54b3..538a1230c96f22060f33321e60de0362c8ed3bb1 100644 GIT binary patch delta 499 zcmZp8z}WDBaY7c~0S3NH{A>AGc{lTDaUa+$DB!_epTWj1ZYs~%$eEd!l3L*z5g(G6 zl#}X|pPwD?U!Ip*{}`y` zI=?0_6SF)c+@(xBjLh-~yCBLhGw{FWzYL6uB&Q gI7k?Txxlu4XW;+C{~h9>34)v~%nXc7%*+g&02>XI)&Kwi literal 28672 zcmeI)&u`jh7zgn81p--C;%r1AO;q)br&M9B@`I+!s$EJ_!%)Fy#ElYZ6LN}^qJga% zqqfI%>eN4>#~n89w8O6ZGj`aqw_UbNciV1z>_9P&skXy3A$?!Ni|zM)pZ9$~uY*MR zvHw}6<(lHeb~=VDZn8NRi?KUGFvenZPtkpl613yp2Z;}M-trh@>nFdZr9WBn-S61! z&(aU+ztZ1d%FqE41Rwwb2tWV=5P$##AP_CkPNuRuEBq|ovYO_T@?m4ww!c&?x8*)< zXpb$^DL02_QhRl!pev%R-c=5T99dfynkwXB8(FN${XlZ=tM{f-*_9RkyyF_jZF5*_ zxR>nrp%->5N+?izMo^N=O__$ED|`+mwZ_98$1;z(HA|=RUNZF@@#su{EZsW;$KliW=%5 z@<3%c7n(=EFgTlHa5jzexT7m1i=tLn$|cpGV!&I}m7-Et)IDXNRQwU0xAazp#+FOZ zMDO``!R$4 z2KN&q(Za*~7i%31`VUsS*mAnAN`oDCgF1Oz-L$xhTU|`Ita~3x061)f~Iod*B|>H9&(6g{7&L zW6A6nJDmAX1O1gXdamssHzNCKY(<`q(-Y0K_hgf%JC}ZCbVGsw1Rwwb2tWV=5P$## zAOHafKwy#uF7x3u92ckD}u(70&}kDS6-j_doythvaQP zjc#5c5(FRs0SG_<0uX=z1Rwwb2tWV=|EoZ8DM$U^S)=Te#cPz`TBU4r;RDJ)oQiXv ak!erN3(A-L 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(); + Assert.Equal(2, context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).Count()); + var ewok = context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).First(); ewok.Title = "l'Odyssée"; context.SaveChanges(); } @@ -190,6 +190,78 @@ namespace UnitTests } + [Fact] + public void Delete_TestEmprunt() + { + 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)) + { + var books = context.BooksSet; + + context.PersonSet.Include(pers => pers.Books).ToList(); + var person = context.PersonSet.Where(b => b.FirstName.StartsWith("coco")).First(); + var testbook = new BookEntity(); + foreach (var book in books) + { + if (book.Owner == null) + { + person.Books.Add(book); + testbook = book; + break; + } + } + + Assert.Equal(testbook.Owner, person); + context.SaveChanges(); + } + } + + [Fact] + public void Delete_TestRendu() + { + 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)) + { + var books = context.BooksSet; + + context.PersonSet.Include(pers => pers.Books).ToList(); + var person = context.PersonSet.Where(b => b.FirstName.StartsWith("coco")).First(); + var testbook = new BookEntity(); + foreach (var book in books) + { + if (book.Owner == null) + { + person.Books.Add(book); + testbook = book; + break; + } + } + + foreach (var book in books) + { + //Console.WriteLine(book.Owner.FirstName); + if (book.Owner != null) + { + book.Owner = null; + } + } + Assert.Null(testbook.Owner); + context.SaveChanges(); + } + } + }