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.

129 lines
5.4 KiB

using EntityFrameworkLib;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Model;
using System.Diagnostics;
namespace UnitTestsSQLiteInMemory
{
public class ChampionsDbTests
{
[Fact]
public void Add_Champions_Test()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolContext>()
.UseInMemoryDatabase(databaseName: "TestChampions_database")
.Options;
using(var context = new LolContext(options))
{
ChampionEntity akali = new ChampionEntity { Name = "Akali", Bio = "Bio Akali", Icon = "Icon Akali", Class = ChampionClass.Assassin };
ChampionEntity alistar = new ChampionEntity { Name = "Alistar", Bio = "Bio Alistar", Icon = "Icon Alistar", Class = ChampionClass.Support };
ChampionEntity bard = new ChampionEntity { Name = "Bard", Bio = "Bio Bard", Icon = "Icon Bard", Class = ChampionClass.Support };
context.Champions.Add(akali);
context.Champions.Add(alistar);
context.Champions.Add(bard);
context.SaveChanges();
}
using(var context = new LolContext(options))
{
Assert.Equal(3, context.Champions.Count());
Assert.Equal("Akali", context.Champions.First().Name);
}
}
[Fact]
public void Modify_Champions_Test()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new LolContext(options))
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
ChampionEntity akali = new ChampionEntity { Name = "Akali", Bio = "Bio Akali", Icon = "Icon Akali", Class = ChampionClass.Assassin };
ChampionEntity akshan = new ChampionEntity { Name = "Akshan", Bio = "Bio Akshan", Icon = "Icon Akshan", Class = ChampionClass.Fighter };
ChampionEntity twitch = new ChampionEntity { Name = "Twitch", Bio = "Bio Twitch", Icon = "icon Twitch", Class = ChampionClass.Assassin };
context.Champions.Add(akali);
context.Champions.Add(akshan);
context.Champions.Add(twitch);
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new LolContext(options))
{
context.Database.EnsureCreated();
string nameToFind = "ak";
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "wi";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
var akshan = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
akshan.Name = "Volibear";
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new LolContext(options))
{
context.Database.EnsureCreated();
string nameToFind = "i";
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "voli";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
}
}
[Fact]
public void Delete_Champions_Test()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<LolContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new LolContext(options))
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
ChampionEntity veigar = new ChampionEntity { Name = "Veigar", Bio = "Bio Veigar", Icon = "Icon Veigar", Class = ChampionClass.Fighter };
ChampionEntity yone = new ChampionEntity { Name = "Yone", Bio = "Bio Yone", Icon = "Icon Yone", Class = ChampionClass.Assassin };
ChampionEntity yasuo = new ChampionEntity { Name = "Yasuo", Bio = "Bio Yasuo", Icon = "icon Yasuo", Class = ChampionClass.Assassin };
context.Champions.Add(veigar);
context.Champions.Add(yone);
context.Champions.Add(yasuo);
context.SaveChanges();
context.Champions.Remove(veigar);
context.Champions.Remove(yone);
context.Champions.Remove(yasuo);
context.SaveChanges();
}
}
}
}