|
|
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 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 =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();
|
|
|
}
|
|
|
|
|
|
|
|
|
//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.Books.Where(n => n.Title.ToLower().Contains(nameToFind)).Count());
|
|
|
var ewok = context.Books.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
|
|
|
|
|
|
//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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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 nb = context.PersonSet.Count();
|
|
|
if (nb > 0)
|
|
|
{
|
|
|
context.PersonSet.Remove(context.PersonSet.FirstOrDefault());
|
|
|
}
|
|
|
|
|
|
Assert.Equal(context.PersonSet.Count(), nb - 1);
|
|
|
context.SaveChanges();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void Delete_TestBook()
|
|
|
{
|
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
|
connection.Open();
|
|
|
var options = new DbContextOptionsBuilder<LibraryContext>().UseSqlite().Options;
|
|
|
var nb;
|
|
|
//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();
|
|
|
nb = context.BooksSet.Count();
|
|
|
if (nb > 0)
|
|
|
{
|
|
|
context.BooksSet.Remove(context.BooksSet.FirstOrDefault());
|
|
|
}
|
|
|
Assert.Equal(context.BooksSet.Count, nb - 1);
|
|
|
context.SaveChanges();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
} |