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 36be9b0..538a123 100644 Binary files a/tp1/TestStub/tp.Books.db and b/tp1/TestStub/tp.Books.db differ diff --git a/tp1/UnitTests/UnitTest1.cs b/tp1/UnitTests/UnitTest1.cs index 412e432..d775f8c 100644 --- a/tp1/UnitTests/UnitTest1.cs +++ b/tp1/UnitTests/UnitTest1.cs @@ -104,8 +104,8 @@ namespace UnitTests { 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(); + 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(); + } + } + }