diff --git a/tp1/DbContextLib/LibraryContext.cs b/tp1/DbContextLib/LibraryContext.cs
index 16fd76b..8ef5245 100644
--- a/tp1/DbContextLib/LibraryContext.cs
+++ b/tp1/DbContextLib/LibraryContext.cs
@@ -29,6 +29,40 @@ namespace DbContextLib
: base(options)
{ }
+ public void AddBook(BookEntity book)
+ {
+ if (!BooksSet.Contains(book))
+ {
+ BooksSet.Add(book);
+ }
+ }
+
+ public void AddPerson(PersonEntity person)
+ {
+ if (!PersonSet.Contains(person))
+ {
+ PersonSet.Add(person);
+ }
+ }
+ public void RemoveAll()
+ {
+
+ if (BooksSet.Count() != 0)
+ {
+ foreach (var book in BooksSet)
+ {
+ BooksSet.Remove(book);
+ }
+ }
+ if (PersonSet.Count() != 0)
+ {
+ foreach (var person in PersonSet)
+ {
+ PersonSet.Remove(person);
+ }
+ }
+
+ }
}
}
diff --git a/tp1/DbContextLib/Migrations/20240216040156_Mg.Designer.cs b/tp1/DbContextLib/Migrations/20240216040156_Mg.Designer.cs
new file mode 100644
index 0000000..1b33c6f
--- /dev/null
+++ b/tp1/DbContextLib/Migrations/20240216040156_Mg.Designer.cs
@@ -0,0 +1,89 @@
+//
+using System;
+using DbContextLib;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace DbContextLib.Migrations
+{
+ [DbContext(typeof(LibraryContext))]
+ [Migration("20240216040156_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
+ }
+ }
+}
diff --git a/tp1/DbContextLib/Migrations/20240216040156_Mg.cs b/tp1/DbContextLib/Migrations/20240216040156_Mg.cs
new file mode 100644
index 0000000..4b9829f
--- /dev/null
+++ b/tp1/DbContextLib/Migrations/20240216040156_Mg.cs
@@ -0,0 +1,65 @@
+using Microsoft.EntityFrameworkCore.Migrations;
+
+#nullable disable
+
+namespace DbContextLib.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");
+ }
+ }
+}
diff --git a/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs b/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs
new file mode 100644
index 0000000..522eb5f
--- /dev/null
+++ b/tp1/DbContextLib/Migrations/LibraryContextModelSnapshot.cs
@@ -0,0 +1,86 @@
+//
+using System;
+using DbContextLib;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+
+#nullable disable
+
+namespace DbContextLib.Migrations
+{
+ [DbContext(typeof(LibraryContext))]
+ partial class LibraryContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(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
+ }
+ }
+}
diff --git a/tp1/Entities/BookEntity.cs b/tp1/Entities/BookEntity.cs
index b107f2a..5652736 100644
--- a/tp1/Entities/BookEntity.cs
+++ b/tp1/Entities/BookEntity.cs
@@ -9,6 +9,7 @@ namespace Entities
{
public class BookEntity
{
+
public long Id { get; set; }
public string Title { get; set; }
public string Author { get; set; }
@@ -17,6 +18,6 @@ namespace Entities
public int PersonId { get; set; }
public PersonEntity? Owner { get; set; }
-
+
}
}
diff --git a/tp1/Model/Book.cs b/tp1/Model/Book.cs
index 16ef9d3..9d66c9a 100644
--- a/tp1/Model/Book.cs
+++ b/tp1/Model/Book.cs
@@ -1,4 +1,5 @@
using System.Reflection;
+using System.Text;
namespace Model
{
@@ -31,7 +32,7 @@ namespace Model
get { return isbn; }
set { isbn = value; }
}
- public Book(long id, string title, string author, string isbn)
+ public Book(long id, string title = "", string author = "", string isbn = "")
{
Id = id;
Title = title;
@@ -39,6 +40,11 @@ namespace Model
Isbn = isbn;
}
+ public string Tostring(StringBuilder sb)
+ {
+ return Id.ToString() + " " + Title.ToString() + " " + Author.ToString() + " " + Isbn.ToString() ;
+ }
+
}
diff --git a/tp1/Model2Entities/DbDataManager.cs b/tp1/Model2Entities/DbDataManager.cs
index cf4d1ea..ad99d40 100644
--- a/tp1/Model2Entities/DbDataManager.cs
+++ b/tp1/Model2Entities/DbDataManager.cs
@@ -1,13 +1,40 @@
using DbContextLib;
using Entities;
+using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Model;
namespace Model2Entities
{
public class DbDataManager : IDataManager
{
+ public void CreateBook(Book book) {
+ using (var context = new LibraryContext())
+ {
+ context.Database.EnsureCreated();
+ if (!context.BooksSet.Contains(book.ConvertToEntity())){
+ context.BooksSet.Add(book.ConvertToEntity());
+ }
+ context.SaveChanges();
+ }
+ }
+
+
+ public List GetAllBooks()
+ {
+
+ List books = new List();
+ using (var context = new LibraryContext())
+ {
+ foreach (var bookEntity in context.BooksSet) {
+
+ books.Add(bookEntity.ConvertToModel());
+ }
+
+ return books;
+ }
+ }
public Book GetBookById(long id)
{
- List books = new List();
+
using (var context = new LibraryContext())
{
foreach (var i in context.BooksSet)
@@ -21,6 +48,93 @@ namespace Model2Entities
}
}
+ public Book GetBookByAuthor(string author)
+ {
+
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ if (i.Author == author)
+ {
+ return i.ConvertToModel();
+ }
+ }
+ return null;
+ }
+ }
+
+ public Book GetBookByTitle(string title)
+ {
+
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ if (i.Title == title)
+ {
+ return i.ConvertToModel();
+ }
+ }
+ return null;
+ }
+ }
+ public Book GetBookByIsbn(string isbn)
+ {
+
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ if (i.Isbn == isbn)
+ {
+ return i.ConvertToModel();
+ }
+ }
+ return null;
+ }
+ }
+ public void UpdateAuthor(long id, string author)
+ {
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ if (i.Id == id)
+ {
+ i.Author = author;
+ }
+ }
+ context.SaveChanges();
+ }
+ }
+
+ public void DeleteBook(long id)
+ {
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ if (i.Id == id)
+ {
+ context.Remove(i);
+ }
+
+ }
+ context.SaveChanges();
+ }
+ }
+ public void DeleteAll()
+ {
+ using (var context = new LibraryContext())
+ {
+ foreach (var i in context.BooksSet)
+ {
+ context.Remove(i);
+ }
+ context.SaveChanges();
+ }
+ }
}
}
diff --git a/tp1/Model2Entities/Extensions.cs b/tp1/Model2Entities/Extensions.cs
index 9ec1105..3dd6d22 100644
--- a/tp1/Model2Entities/Extensions.cs
+++ b/tp1/Model2Entities/Extensions.cs
@@ -16,5 +16,9 @@ namespace Model2Entities
return new Book(book.Id, book.Title, book.Author, book.Isbn);
}
+ public static BookEntity ConvertToEntity(this Book book)
+ {
+ return new BookEntity() {Id= book.Id, Title = book.Title, Author = book.Author, Isbn = book.Isbn };
+ }
}
}
diff --git a/tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs b/tp1/StubbedContextLib/Migrations/20240215195621_Mg.Designer.cs
similarity index 98%
rename from tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs
rename to tp1/StubbedContextLib/Migrations/20240215195621_Mg.Designer.cs
index 0b53312..e88af90 100644
--- a/tp1/StubbedContextLib/Migrations/20240215154956_Mg.Designer.cs
+++ b/tp1/StubbedContextLib/Migrations/20240215195621_Mg.Designer.cs
@@ -11,7 +11,7 @@ using StubbedContextLib;
namespace StubbedContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
- [Migration("20240215154956_Mg")]
+ [Migration("20240215195621_Mg")]
partial class Mg
{
///
diff --git a/tp1/StubbedContextLib/Migrations/20240215154956_Mg.cs b/tp1/StubbedContextLib/Migrations/20240215195621_Mg.cs
similarity index 100%
rename from tp1/StubbedContextLib/Migrations/20240215154956_Mg.cs
rename to tp1/StubbedContextLib/Migrations/20240215195621_Mg.cs
diff --git a/tp1/TestModel2Entities/Program.cs b/tp1/TestModel2Entities/Program.cs
index 3ed0e5d..11ad140 100644
--- a/tp1/TestModel2Entities/Program.cs
+++ b/tp1/TestModel2Entities/Program.cs
@@ -1,3 +1,70 @@
-// See https://aka.ms/new-console-template for more information
+using Model;
+using Entities;
+using Model2Entities;
+using Microsoft.Extensions.DependencyModel;
+using DbContextLib;
+
+// See https://aka.ms/new-console-template for more information
+
+
+
Console.WriteLine("Hello, World!");
+DbDataManager dbDataManager = new DbDataManager();
+
+
+ dbDataManager.CreateBook(new Book(1, "mistake", "test1", "test1"));
+ dbDataManager.CreateBook(new Book(2, "the100", "test2", "test2"));
+ dbDataManager.CreateBook(new Book(3, "GOT", "lastTest", "lastTest"));
+
+
+
+Console.WriteLine("test de récupération de tout les livres");
+
+List books = dbDataManager.GetAllBooks();
+foreach (var book in books)
+{
+ Console.WriteLine("Titre du livre : " + book.Title);
+}
+
+Console.WriteLine("\ntest de récupération de livre par ID");
+Book b1 = dbDataManager.GetBookById(3);
+if (b1 != null)
+{
+
+ Console.WriteLine("Le livre : " + b1.Title);
+}
+
+Console.WriteLine("\ntest de récupération de livre par Autheur");
+Book b2 = dbDataManager.GetBookByIsbn("test2");
+Console.WriteLine("Le livre : " + b2.Title);
+
+
+Console.WriteLine("\ntest de récupération de livre par Titre");
+Book b3 = dbDataManager.GetBookByTitle("the100");
+Console.WriteLine("Le livre : " + b3.Title);
+
+Console.WriteLine("\ntest de récupération de livre par ISBn");
+Book b4 = dbDataManager.GetBookByAuthor("lastTest");
+Console.WriteLine("Le livre : " + b4.Title);
+
+Console.WriteLine("\n MAJ de l'auteur du livre th100");
+dbDataManager.UpdateAuthor(2, "Kass Morgan");
+Book the100 = dbDataManager.GetBookByAuthor("Kass Morgan");
+Console.WriteLine(the100.Title + " " + the100.Author);
+
+Console.WriteLine("\nsupresion du livre ou id = 1");
+dbDataManager.DeleteBook(1);
+
+
+Book test = dbDataManager.GetBookById(1);
+if (test == null)
+{
+ Console.WriteLine("livre supprimée");
+}
+
+Console.WriteLine("\nsuppression de tous les livres");
+
+
+
+dbDataManager.DeleteAll();
diff --git a/tp1/TestModel2Entities/TestModel2Entities.csproj b/tp1/TestModel2Entities/TestModel2Entities.csproj
index 2150e37..5a96c29 100644
--- a/tp1/TestModel2Entities/TestModel2Entities.csproj
+++ b/tp1/TestModel2Entities/TestModel2Entities.csproj
@@ -7,4 +7,19 @@
enable
+
+
+
+ all
+ runtime; build; native; contentfiles; analyzers; buildtransitive
+
+
+
+
+
+
+
+
+
+
diff --git a/tp1/TestModel2Entities/tp.Books.db b/tp1/TestModel2Entities/tp.Books.db
new file mode 100644
index 0000000..d7f473e
Binary files /dev/null and b/tp1/TestModel2Entities/tp.Books.db differ
diff --git a/tp1/TestStub/tp.Books.db b/tp1/TestStub/tp.Books.db
index 3692517..0affe3b 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 097e0ab..6d2a96f 100644
--- a/tp1/UnitTests/UnitTest1.cs
+++ b/tp1/UnitTests/UnitTest1.cs
@@ -6,13 +6,348 @@ using Microsoft.EntityFrameworkCore;
using System.Linq;
using Xunit;
using Microsoft.Data.Sqlite;
+using System;
+using static System.Reflection.Metadata.BlobBuilder;
namespace UnitTests
{
public class UnitTest1
{
-
+ [Fact]
+ 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().UseSqlite().Options;
+
+ //prepares the database with one instance of the context
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.Database.EnsureCreated();
+ 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.AddBook(the100);
+ context.AddBook(princeOfPersia);
+ context.AddBook(PercyJackson);
+ context.SaveChanges();
+ }
+
+
+ //uses another instance of the context to do the tests
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext(options))
+ {
+ context.Database.EnsureCreated();
+ Assert.Equal(3, context.BooksSet.Count());
+ Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count());
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
+
+ [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))
+ using (var context = new StubbedContext())
+ {
+ //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.AddPerson(p1);
+ context.AddPerson(p2);
+ context.SaveChanges();
+ }
+
+
+ //uses another instance of the context to do the tests
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.Database.EnsureCreated();
+ Assert.Equal(2, context.PersonSet.Count());
+ Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Jean")).Count());
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
+
+
+ [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
+
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.Database.EnsureCreated();
+ 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.AddBook(the100);
+ context.AddBook(princeOfPersia);
+ context.AddBook(PercyJackson);
+ context.SaveChanges();
+
+ }
+
+ //prepares the database with one instance of the context
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.Database.EnsureCreated();
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ var book = context.BooksSet.Where(n => n.Title.Contains("princeOfPersia")).FirstOrDefault();
+ book.Title = "l'Odyssée";
+ Assert.Equal("l'Odyssée", book.Title);
+ context.RemoveAll();
+ 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))
+ using (var context = new StubbedContext())
+ {
+
+ 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.AddPerson(p1);
+ context.AddPerson(p2);
+ context.SaveChanges();
+ }
+
+ //prepares the database with one instance of the context
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ string nameToFind = "Jean";
+ Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).Count());
+ var person = context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).First();
+ person.FirstName = "Jacques";
+
+ Assert.Equal("Jacques", person.FirstName);
+
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
+
+ [Fact]
+ 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))
+ using (var context = new StubbedContext())
+ {
+ PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
+ PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
+ context.AddPerson(p1);
+ context.AddPerson(p2);
+ context.SaveChanges();
+ }
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ var Persons = context.PersonSet;
+ if (Persons.Count() != 0)
+ {
+ foreach (var person in Persons)
+ {
+ context.PersonSet.Remove(person);
+ }
+ }
+ context.SaveChanges();
+ Assert.Equal( 0, context.PersonSet.Count());
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
+
+ [Fact]
+ public void Delete_TestBook()
+ {
+ 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())
+ {
+ 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.AddBook(the100);
+ context.AddBook(princeOfPersia);
+ context.AddBook(PercyJackson);
+ context.SaveChanges();
+ }
+
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ var Books = context.BooksSet;
+ if (Books.Count() != 0)
+ {
+ foreach (var book in Books)
+ {
+ context.BooksSet.Remove(book);
+ }
+ }
+ context.SaveChanges();
+ Assert.Equal(0, context.BooksSet.Count());
+ context.RemoveAll();
+ context.SaveChanges();
+
+ }
+ }
+
+
+ [Fact]
+ public void Update_TestEmprunt()
+ {
+ 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())
+ {
+
+ 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.AddPerson(p1);
+ context.AddPerson(p2);
+ 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.AddBook(the100);
+ context.AddBook(princeOfPersia);
+ context.AddBook(PercyJackson);
+ context.SaveChanges();
+
+ }
+
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ var books = context.BooksSet;
+
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ var person = context.PersonSet.Where(b => b.FirstName.Contains("Jean")).First();
+ foreach (var book in books)
+ {
+ if (book.Owner == null)
+ {
+ book.Owner = person;
+ break;
+ }
+ }
+
+ context.SaveChanges();
+ }
+
+ using (var context = new StubbedContext())
+ {
+ Assert.NotNull(context.BooksSet.Where(b => b.Owner.FirstName.Contains("Jean")).First());
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
+
+ [Fact]
+ public void Update_TestRendu()
+ {
+ 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())
+ {
+
+ 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.AddPerson(p1);
+ context.AddPerson(p2);
+ 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.AddBook(the100);
+ context.AddBook(princeOfPersia);
+ context.AddBook(PercyJackson);
+ context.SaveChanges();
+
+ }
+
+ //using (var context = new StubbedContext(options))
+ using (var context = new StubbedContext())
+ {
+ var books = context.BooksSet;
+
+ context.PersonSet.Include(pers => pers.Books).ToList();
+ var person = context.PersonSet.Where(b => b.FirstName.StartsWith("Jean")).First();
+ var testbook = new BookEntity();
+ foreach (var book in books)
+ {
+ if (book.Owner == null)
+ {
+ book.Owner = person;
+ break;
+ }
+ }
+
+ foreach (var book in books)
+ {
+ //Console.WriteLine(book.Owner.FirstName);
+ if (book.Owner != null)
+ {
+ book.Owner = null;
+ }
+ }
+ Assert.Equal(context.BooksSet.Count(), context.BooksSet.Where(b => b.Owner == null).Count());
+ context.RemoveAll();
+ context.SaveChanges();
+ }
+ }
}
}
\ No newline at end of file