From 5aacc394b5605dda378cf037fb011be2664b25b5 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 15 Feb 2024 19:47:37 +0100 Subject: [PATCH] modification test --- tp1/UnitTests/UnitTest1.cs | 251 ------------------------------------- 1 file changed, 251 deletions(-) diff --git a/tp1/UnitTests/UnitTest1.cs b/tp1/UnitTests/UnitTest1.cs index 2bd9991..097e0ab 100644 --- a/tp1/UnitTests/UnitTest1.cs +++ b/tp1/UnitTests/UnitTest1.cs @@ -11,259 +11,8 @@ namespace UnitTests { public class UnitTest1 { - [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)) - { - - context.PersonSet.Include(pers => pers.Books).ToList(); - var allPers = context.PersonSet; - foreach (var person in allPers) - { - context.PersonSet.Remove(person); - } - - Assert.Equal(0, context.PersonSet.Count()); - 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(options)) - { - context.PersonSet.Include(pers => pers.Books).ToList(); - var allBooks = context.BooksSet; - foreach (var book in allBooks) - { - context.BooksSet.Remove(book); - } - - Assert.Equal(0, context.BooksSet.Count()); - context.SaveChanges(); - } - } - - - [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)) - { - //context.Database.OpenConnection(); - context.Database.EnsureCreated(); - context.PersonSet.Include(pers => pers.Books).ToList(); - BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =8 }; - BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 10 }; - BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 9 }; - 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 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.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(); - } - } - [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 - - 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(); - } - } - - [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(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 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(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(); - } - } - - } } \ No newline at end of file