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.
177 lines
6.1 KiB
177 lines
6.1 KiB
namespace Tests;
|
|
|
|
using System.Threading.Tasks;
|
|
using System.Xml.Linq;
|
|
using DataManagers;
|
|
using EFLib;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Model;
|
|
|
|
public class DbChampionManager_UT
|
|
{
|
|
|
|
[Theory]
|
|
[InlineData(true, true, 8, "Aurelien", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(true, false, 8, "Marche sans ID", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(false, true, 8, "Marche pas", ChampionClass.Tank, "tro bo le type")]
|
|
public async Task Add_TestAsync(bool isValid, bool hasId,int id, string name, ChampionClass championClass, string bio)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LolContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
|
|
using (var db = new DbDataManager(new EFLib.LolContext(options)))
|
|
{
|
|
await db.EnsureCreatedAsync();
|
|
|
|
Champion champion = new Champion(name, championClass, bio);
|
|
if (hasId)
|
|
{
|
|
champion = new Champion(id, name, championClass, bio);
|
|
}
|
|
Champion? c = await db.ChampionsMgr.AddItem(champion);
|
|
|
|
if (isValid)
|
|
{
|
|
Assert.NotNull(c);
|
|
}
|
|
else
|
|
{
|
|
Champion? c1 = await db.ChampionsMgr.AddItem(champion);
|
|
Assert.Null(c1);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true, true, 18, "Aurelien", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(true, false, 19, "Marche sans Id", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(false, true, 20, "Marche pas", ChampionClass.Tank, "tro bo le type")]
|
|
public async Task Delete_TestAsync(bool isValid, bool hasId, int id, string name, ChampionClass championClass, string bio)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LolContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
using (var db = new DbDataManager(new EFLib.LolContext(options)))
|
|
{
|
|
await db.Context.Database.EnsureCreatedAsync();
|
|
|
|
Champion champion = new Champion(name, championClass, bio);
|
|
if (hasId)
|
|
{
|
|
champion = new Champion(id, name, championClass, bio);
|
|
}
|
|
if (isValid)
|
|
{
|
|
Champion? c = await db.ChampionsMgr.AddItem(champion);
|
|
var retour = await db.ChampionsMgr.DeleteItem(champion);
|
|
Assert.True(retour);
|
|
}
|
|
else
|
|
{
|
|
var retour = await db.ChampionsMgr.DeleteItem(champion);
|
|
Assert.False(retour);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(true, true, 18, "Aurelien", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(true, false, 19, "Marche sans Id", ChampionClass.Tank, "tro bo le type")]
|
|
[InlineData(false, true, 20, "Marche pas", ChampionClass.Tank, "tro bo le type")]
|
|
public async Task Modify_TestAsync(bool isValid, bool hasId, int id, string name, ChampionClass championClass, string bio)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LolContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
using (var db = new DbDataManager(new EFLib.LolContext(options)))
|
|
{
|
|
await db.Context.Database.EnsureCreatedAsync();
|
|
|
|
Champion champion = new Champion(name, championClass, bio);
|
|
if (hasId)
|
|
{
|
|
champion = new Champion(id, name, championClass, bio);
|
|
}
|
|
if (isValid)
|
|
{
|
|
Champion? c = await db.ChampionsMgr.AddItem(champion);
|
|
var retour = await db.ChampionsMgr.UpdateItem(c, new Champion(champion.Id, champion.Name, bio: "blabla"));
|
|
Assert.Equal("blabla", retour.Bio);
|
|
}
|
|
else
|
|
{
|
|
var retour = await db.ChampionsMgr.UpdateItem(champion, new Champion(champion.Id, champion.Name, bio: "blabla"));
|
|
Assert.Null(retour);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("name", false)]
|
|
[InlineData("bio", false)]
|
|
[InlineData("class", false)]
|
|
[InlineData("name", true)]
|
|
[InlineData("bio", true)]
|
|
[InlineData("class", true)]
|
|
public async Task GetItems_TestAsync(string orderingProperty, bool reverse)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LolContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
using (var db = new DbDataManager(new EFLib.LolContext(options)))
|
|
{
|
|
await db.Context.Database.EnsureCreatedAsync();
|
|
|
|
Champion champion = new Champion("test1", ChampionClass.Unknown, "test1");
|
|
Champion champion1 = new Champion("test2", ChampionClass.Assassin, "test2");
|
|
Champion champion2 = new Champion("test3", ChampionClass.Fighter, "test3");
|
|
|
|
await db.ChampionsMgr.AddItem(champion);
|
|
await db.ChampionsMgr.AddItem(champion1);
|
|
await db.ChampionsMgr.AddItem(champion2);
|
|
|
|
|
|
var list = await db.ChampionsMgr.GetItems(0, 10, orderingProperty, reverse);
|
|
|
|
Assert.Equal(3, list.Count());
|
|
//if (reverse) Assert.Equal("test3", list.First().Name);
|
|
//else Assert.Equal("test1", list.First().Name);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public async Task getNbItems_TestASync()
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<LolContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
using (var db = new DbDataManager(new EFLib.LolContext(options)))
|
|
{
|
|
await db.Context.Database.EnsureCreatedAsync();
|
|
Assert.Equal(3, await db.ChampionsMgr.GetNbItems());
|
|
}
|
|
}
|
|
|
|
} |