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.

98 lines
2.8 KiB

using EFLol;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace TestUnitaire
{
public class TestEfLol
{
[Fact]
public async Task TestAddInMemory()
{
// Arrange
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<ChampionContext>()
.UseSqlite(connection)
.Options;
// Act
using (var context = new ChampionContext(options))
{
await context.Database.EnsureCreatedAsync();
var Zeus = new ChampionEntity
{
Name = "Zeus",
Bio = "Zeus is the king of the gods."
};
var Poseidon = new ChampionEntity
{
Name = "Poseidon",
Bio = "Poseidon is the king of the sea."
};
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
Assert.Null(found);
await context.Champions.AddAsync(Zeus);
await context.Champions.AddAsync(Poseidon);
await context.SaveChangesAsync();
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
Assert.NotNull(found);
Assert.Equal(2, await context.Champions.CountAsync());
Assert.Equal("Zeus", found.Name);
}
}
[Fact]
public void ModifyTestInMemory()
{
var options = new DbContextOptionsBuilder<ChampionContext>()
.UseInMemoryDatabase(databaseName: "Modify_Test_database")
.Options;
//prepares the database with one instance of the context
using (var context = new ChampionContext(options))
{
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." };
ChampionEntity yoda = new ChampionEntity { Name = "Yoda", Bio = "Zeus is the king of the gods." };
ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." };
context.Champions.Add(chewie);
context.Champions.Add(yoda);
context.Champions.Add(ewok);
context.SaveChanges();
}
//prepares the database with one instance of the context
using (var context = new ChampionContext(options))
{
string nameToFind = "ew";
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "ewo";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
ewok.Name = "Wicket";
context.SaveChanges();
}
//prepares the database with one instance of the context
using (var context = new ChampionContext(options))
{
string nameToFind = "ew";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "wick";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
}
}
}
}