|
|
using DbContextLib;
|
|
|
using StubbedContextLib;
|
|
|
using Entities;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
using System.Linq;
|
|
|
using Xunit;
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
|
|
namespace UnitTests
|
|
|
{
|
|
|
public class UnitTest1
|
|
|
{
|
|
|
[Fact]
|
|
|
public void Delete_TestPerson()
|
|
|
{
|
|
|
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(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<LibraryContext>().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<LibraryContext>().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<LibraryContext>().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<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))
|
|
|
{
|
|
|
|
|
|
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<73>e";
|
|
|
context.SaveChanges();
|
|
|
}
|
|
|
}
|
|
|
[Fact]
|
|
|
public void Modify_TestPerson()
|
|
|
{
|
|
|
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(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<LibraryContext>().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<LibraryContext>().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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
} |