You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
League-of-Legends_Project/EntityFramework_LoL/Sources/TestEF/TestChampions.cs

147 lines
4.8 KiB

using Business;
using Entities;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Model;
using Shared;
namespace TestEF
{
public class TestChampions
{
[Fact]
public async void Test_Add()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseSqlite(connection)
.Options;
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
Champion batman = new("Batman", ChampionClass.Assassin);
Champion endeavor = new("Endeavor", ChampionClass.Tank);
Champion escanor = new("Escanor", ChampionClass.Fighter);
await manager.AddItem(batman);
await manager.AddItem(endeavor);
await manager.AddItem(escanor);
}
//uses another instance of the context to do the tests
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
var nbItems = await manager.GetNbItems();
Assert.Equal(3, nbItems);
var items = await manager.GetItemsByName("Batman", 0, nbItems);
Assert.Equal("Batman", items.First().Name);
}
}
[Fact]
public async void Test_Update()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
Champion batman = new("Batman", ChampionClass.Assassin);
Champion endeavor = new("Endeavor", ChampionClass.Tank);
Champion escanor = new("Escanor", ChampionClass.Fighter);
await manager.AddItem(batman);
await manager.AddItem(endeavor);
await manager.AddItem(escanor);
}
//uses another instance of the context to do the tests
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
var itemsByName = await manager.GetItemsByName("E", 0, 3);
Assert.Equal(2, itemsByName.Count());
var escanor = context.champions.Where(n => n.Name.Contains("Esc")).First();
escanor.Class = ChampionClass.Tank;
itemsByName = await manager.GetItemsByClass(ChampionClass.Tank, 0, 3);
Assert.Equal(2, itemsByName.Count());
context.SaveChanges();
}
}
[Fact]
public async void Test_Delete()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolDbContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
Champion batman = new("Batman", ChampionClass.Assassin);
Champion endeavor = new("Endeavor", ChampionClass.Tank);
Champion escanor = new("Escanor", ChampionClass.Fighter);
await manager.AddItem(batman);
await manager.AddItem(endeavor);
await manager.AddItem(escanor);
}
//uses another instance of the context to do the tests
using (var context = new LolDbContext(options))
{
var manager = new DbData(context).ChampionsMgr;
context.Database.EnsureCreated();
var endeavor = (await manager.GetItemsByName("Endeavor", 0, 3)).First();
var itemsByName = await manager.DeleteItem(endeavor);
Assert.Equal(2, await manager.GetNbItems());
}
}
}
}