correction des tests

master^2
Tom RAMBEAU 1 year ago
parent e3e8566103
commit be6777ab42

@ -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);
}
}
}
}
}

@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
using System.Linq;
using Xunit;
using Microsoft.Data.Sqlite;
using System;
using static System.Reflection.Metadata.BlobBuilder;
namespace UnitTests
{
@ -20,27 +22,30 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
//context.Database.OpenConnection();
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.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson);
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))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
Assert.Equal(5, context.BooksSet.Count());
Assert.Equal(3, context.BooksSet.Count());
Assert.Equal(1, context.BooksSet.Where(b => b.Title.Contains("the100")).Count());
context.RemoveAll();
context.SaveChanges();
}
}
@ -53,25 +58,29 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
//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.PersonSet.Add(p1);
context.PersonSet.Add(p2);
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(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
Assert.Equal(3, context.PersonSet.Count());
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Granc")).Count());
Assert.Equal(2, context.PersonSet.Count());
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Jean")).Count());
context.RemoveAll();
context.SaveChanges();
}
}
@ -85,28 +94,31 @@ namespace UnitTests
//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(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.BooksSet.Add(the100);
context.BooksSet.Add(princeOfPersia);
context.BooksSet.Add(PercyJackson);
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(options))
using (var context = new StubbedContext())
{
context.Database.EnsureCreated();
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";
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();
}
}
@ -120,25 +132,31 @@ namespace UnitTests
//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(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.PersonSet.Add(p1);
context.PersonSet.Add(p2);
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(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.ToLower().Contains(nameToFind)).Count());
var person = context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).First();
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();
}
}
@ -151,17 +169,31 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
//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 nb = context.PersonSet.Count();
if (nb > 0)
var Persons = context.PersonSet;
if (Persons.Count() != 0)
{
context.PersonSet.Remove(context.PersonSet.FirstOrDefault());
foreach (var person in Persons)
{
context.PersonSet.Remove(person);
}
Assert.Equal(context.PersonSet.Count(), nb - 1);
}
context.SaveChanges();
Assert.Equal( 0, context.PersonSet.Count());
context.RemoveAll();
context.SaveChanges();
}
}
@ -174,77 +206,130 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().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();
}
//prepares the database with one instance of the context
using (var context = new StubbedContext(options))
//using (var context = new StubbedContext(options))
using (var context = new StubbedContext())
{
context.PersonSet.Include(pers => pers.Books).ToList();
var nb = context.BooksSet.Count();
if (nb > 0)
var Books = context.BooksSet;
if (Books.Count() != 0)
{
foreach (var book in Books)
{
context.BooksSet.Remove(context.BooksSet.FirstOrDefault());
context.BooksSet.Remove(book);
}
Assert.Equal(context.BooksSet.Count(), nb - 1);
}
context.SaveChanges();
Assert.Equal(0, context.BooksSet.Count());
context.RemoveAll();
context.SaveChanges();
}
}
[Fact]
public void Delete_TestEmprunt()
public void Update_TestEmprunt()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext())
{
//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.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("coco")).First();
var testbook = new BookEntity();
var person = context.PersonSet.Where(b => b.FirstName.Contains("Jean")).First();
foreach (var book in books)
{
if (book.Owner == null)
{
person.Books.Add(book);
testbook = book;
book.Owner = person;
break;
}
}
Assert.Equal(testbook.Owner, person);
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 Delete_TestRendu()
public void Update_TestRendu()
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
using (var context = new StubbedContext())
{
//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.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("coco")).First();
var person = context.PersonSet.Where(b => b.FirstName.StartsWith("Jean")).First();
var testbook = new BookEntity();
foreach (var book in books)
{
if (book.Owner == null)
{
person.Books.Add(book);
testbook = book;
book.Owner = person;
break;
}
}
@ -257,7 +342,8 @@ namespace UnitTests
book.Owner = null;
}
}
Assert.Null(testbook.Owner);
Assert.Equal(context.BooksSet.Count(), context.BooksSet.Where(b => b.Owner == null).Count());
context.RemoveAll();
context.SaveChanges();
}
}

Loading…
Cancel
Save