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.
116 lines
4.4 KiB
116 lines
4.4 KiB
using EFLol;
|
|
using Microsoft.Data.Sqlite;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace TestUnitaire
|
|
{
|
|
public class TestEfLol
|
|
{
|
|
[Theory]
|
|
[InlineData("Zeus", "Dieu de la foudre", true)]
|
|
[InlineData("Hades", "Dieu des enfers", true)]
|
|
[InlineData("Aphrodite", "Déesse de l'amour", true)]
|
|
[InlineData("AresAresAresAresAresAresAresAresAresAres",
|
|
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
|
|
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre" +
|
|
"Dieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerreDieu de la guerre", false)]
|
|
public async Task TestAddInMemory(String name, String bio, bool expected)
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<MyDbContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
// Act
|
|
using (var context = new MyDbContext(options))
|
|
{
|
|
await context.Database.EnsureCreatedAsync();
|
|
var Dieu = new ChampionEntity
|
|
{
|
|
Name = name,
|
|
Bio = bio
|
|
};
|
|
|
|
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
|
|
Assert.Null(found);
|
|
|
|
await context.Champions.AddAsync(Dieu);
|
|
await context.SaveChangesAsync();
|
|
|
|
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name);
|
|
Assert.NotNull(found);
|
|
|
|
Assert.Equal(1, await context.Champions.CountAsync());
|
|
Assert.Equal(name, found.Name);
|
|
|
|
// Test if the max length of the name is respected (30) and the max length of the bio is respected (256)
|
|
if (expected)
|
|
{
|
|
Assert.True(found.Name.Length <= 30);
|
|
Assert.True(found.Bio.Length <= 256);
|
|
}
|
|
else
|
|
{
|
|
Assert.False(found.Bio.Length <= 256);
|
|
Assert.False(found.Name.Length <= 30);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
[Fact]
|
|
public void ModifyTestInMemory()
|
|
{
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
connection.Open();
|
|
|
|
var options = new DbContextOptionsBuilder<MyDbContext>()
|
|
.UseSqlite(connection)
|
|
.Options;
|
|
|
|
//prepares the database with one instance of the context
|
|
using (var context = new MyDbContext(options))
|
|
{
|
|
//context.Database.OpenConnection();
|
|
context.Database.EnsureCreated();
|
|
|
|
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();
|
|
}
|
|
|
|
//uses another instance of the context to do the tests
|
|
using (var context = new MyDbContext(options))
|
|
{
|
|
context.Database.EnsureCreated();
|
|
|
|
string nameToFind = "ew";
|
|
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
|
nameToFind = "wo";
|
|
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();
|
|
}
|
|
|
|
//uses another instance of the context to do the tests
|
|
using (var context = new MyDbContext(options))
|
|
{
|
|
context.Database.EnsureCreated();
|
|
|
|
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());
|
|
}
|
|
}
|
|
}
|
|
} |