correction des tests

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

@ -29,6 +29,40 @@ namespace DbContextLib
: base(options) : 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 System.Linq;
using Xunit; using Xunit;
using Microsoft.Data.Sqlite; using Microsoft.Data.Sqlite;
using System;
using static System.Reflection.Metadata.BlobBuilder;
namespace UnitTests namespace UnitTests
{ {
@ -20,27 +22,30 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().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(options))
using (var context = new StubbedContext())
{ {
//context.Database.OpenConnection();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList(); context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 }; BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id =3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.BooksSet.Add(the100); context.AddBook(the100);
context.BooksSet.Add(princeOfPersia); context.AddBook(princeOfPersia);
context.BooksSet.Add(PercyJackson); context.AddBook(PercyJackson);
context.SaveChanges(); context.SaveChanges();
} }
//uses another instance of the context to do the tests //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(); 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()); 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; var options = new DbContextOptionsBuilder<LibraryContext>().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(options))
using (var context = new StubbedContext())
{ {
//context.Database.OpenConnection(); //context.Database.OpenConnection();
context.Database.EnsureCreated(); context.Database.EnsureCreated();
context.PersonSet.Include(pers => pers.Books).ToList(); context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 }; PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 }; PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.PersonSet.Add(p1); context.AddPerson(p1);
context.PersonSet.Add(p2); context.AddPerson(p2);
context.SaveChanges(); context.SaveChanges();
} }
//uses another instance of the context to do the tests //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(); context.Database.EnsureCreated();
Assert.Equal(3, context.PersonSet.Count()); Assert.Equal(2, context.PersonSet.Count());
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Granc")).Count()); Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains("Jean")).Count());
context.RemoveAll();
context.SaveChanges();
} }
} }
@ -84,29 +93,32 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context //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(); context.PersonSet.Include(pers => pers.Books).ToList();
BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 }; BookEntity the100 = new BookEntity { Title = "the100", Author = "", Isbn = "", Id = 3 };
BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 }; BookEntity princeOfPersia = new BookEntity { Title = "princeOfPersia", Author = "", Isbn = "", Id = 4 };
BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 }; BookEntity PercyJackson = new BookEntity { Title = "PercyJackson", Author = "", Isbn = "", Id = 5 };
context.BooksSet.Add(the100); context.AddBook(the100);
context.BooksSet.Add(princeOfPersia); context.AddBook(princeOfPersia);
context.BooksSet.Add(PercyJackson); context.AddBook(PercyJackson);
context.SaveChanges(); context.SaveChanges();
} }
//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(); context.PersonSet.Include(pers => pers.Books).ToList();
string nameToFind = "princeOfPersia"; var book = context.BooksSet.Where(n => n.Title.Contains("princeOfPersia")).FirstOrDefault();
Assert.Equal(2, context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).Count()); book.Title = "l'Odyssée";
var ewok = context.BooksSet.Where(n => n.Title.ToLower().Contains(nameToFind)).First(); Assert.Equal("l'Odyssée", book.Title);
ewok.Title = "l'Odyssée"; context.RemoveAll();
context.SaveChanges(); 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
//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(); context.PersonSet.Include(pers => pers.Books).ToList();
PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 }; PersonEntity p1 = new PersonEntity { FirstName = "Franc", LastName = "Bertinelli", Id = 2 };
PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 }; PersonEntity p2 = new PersonEntity { FirstName = "Jean", LastName = "Dubois", Id = 3 };
context.PersonSet.Add(p1); context.AddPerson(p1);
context.PersonSet.Add(p2); context.AddPerson(p2);
context.SaveChanges(); context.SaveChanges();
} }
//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(); context.PersonSet.Include(pers => pers.Books).ToList();
string nameToFind = "Jean"; string nameToFind = "Jean";
Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).Count()); Assert.Equal(1, context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).Count());
var person = context.PersonSet.Where(p => p.FirstName.ToLower().Contains(nameToFind)).First(); var person = context.PersonSet.Where(p => p.FirstName.Contains(nameToFind)).First();
person.FirstName = "Jacques"; person.FirstName = "Jacques";
Assert.Equal("Jacques", person.FirstName);
context.RemoveAll();
context.SaveChanges(); context.SaveChanges();
} }
} }
@ -151,17 +169,31 @@ namespace UnitTests
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().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(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(); context.PersonSet.Include(pers => pers.Books).ToList();
var nb = context.PersonSet.Count(); var Persons = context.PersonSet;
if (nb > 0) if (Persons.Count() != 0)
{ {
context.PersonSet.Remove(context.PersonSet.FirstOrDefault()); foreach (var person in Persons)
{
context.PersonSet.Remove(person);
}
} }
context.SaveChanges();
Assert.Equal(context.PersonSet.Count(), nb - 1); Assert.Equal( 0, context.PersonSet.Count());
context.RemoveAll();
context.SaveChanges(); context.SaveChanges();
} }
} }
@ -172,79 +204,132 @@ namespace UnitTests
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
//prepares the database with one instance of the context
//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())
{
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(); context.PersonSet.Include(pers => pers.Books).ToList();
var nb = context.BooksSet.Count(); var Books = context.BooksSet;
if (nb > 0) if (Books.Count() != 0)
{ {
context.BooksSet.Remove(context.BooksSet.FirstOrDefault()); foreach (var book in Books)
{
context.BooksSet.Remove(book);
}
} }
Assert.Equal(context.BooksSet.Count(), nb - 1);
context.SaveChanges(); context.SaveChanges();
Assert.Equal(0, context.BooksSet.Count());
context.RemoveAll();
context.SaveChanges();
} }
} }
[Fact] [Fact]
public void Delete_TestEmprunt() public void Update_TestEmprunt()
{ {
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().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())
{
//prepares the database with one instance of the context context.PersonSet.Include(pers => pers.Books).ToList();
using (var context = new StubbedContext(options)) 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; var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList(); 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.Contains("Jean")).First();
var testbook = new BookEntity();
foreach (var book in books) foreach (var book in books)
{ {
if (book.Owner == null) if (book.Owner == null)
{ {
person.Books.Add(book); book.Owner = person;
testbook = book;
break; 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(); context.SaveChanges();
} }
} }
[Fact] [Fact]
public void Delete_TestRendu() public void Update_TestRendu()
{ {
var connection = new SqliteConnection("DataSource=:memory:"); var connection = new SqliteConnection("DataSource=:memory:");
connection.Open(); connection.Open();
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options; var options = new DbContextOptionsBuilder<LibraryContext>().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())
{
//prepares the database with one instance of the context context.PersonSet.Include(pers => pers.Books).ToList();
using (var context = new StubbedContext(options)) 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; var books = context.BooksSet;
context.PersonSet.Include(pers => pers.Books).ToList(); 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(); var testbook = new BookEntity();
foreach (var book in books) foreach (var book in books)
{ {
if (book.Owner == null) if (book.Owner == null)
{ {
person.Books.Add(book); book.Owner = person;
testbook = book;
break; break;
} }
} }
@ -257,7 +342,8 @@ namespace UnitTests
book.Owner = null; book.Owner = null;
} }
} }
Assert.Null(testbook.Owner); Assert.Equal(context.BooksSet.Count(), context.BooksSet.Where(b => b.Owner == null).Count());
context.RemoveAll();
context.SaveChanges(); context.SaveChanges();
} }
} }

Loading…
Cancel
Save