|
|
@ -1,97 +1,117 @@
|
|
|
|
using EFLol;
|
|
|
|
using EFLol;
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TestUnitaire
|
|
|
|
namespace TestUnitaire
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class TestEfLol
|
|
|
|
public class TestEfLol
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public async Task TestAddInMemory()
|
|
|
|
[Theory]
|
|
|
|
{
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
[InlineData("Zeus", "Dieu de la foudre", true)]
|
|
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
|
|
[InlineData("Hades", "Dieu des enfers", true)]
|
|
|
|
connection.Open();
|
|
|
|
[InlineData("Aphrodite", "Déesse de l'amour", true)]
|
|
|
|
|
|
|
|
[InlineData("AresAresAresAresAresAresAresAresAresAres",
|
|
|
|
var options = new DbContextOptionsBuilder<ChampionContext>()
|
|
|
|
"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" +
|
|
|
|
.UseSqlite(connection)
|
|
|
|
"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" +
|
|
|
|
.Options;
|
|
|
|
"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)
|
|
|
|
// Act
|
|
|
|
{
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
|
|
{
|
|
|
|
connection.Open();
|
|
|
|
await context.Database.EnsureCreatedAsync();
|
|
|
|
|
|
|
|
var Zeus = new ChampionEntity
|
|
|
|
var options = new DbContextOptionsBuilder<MyDbContext>()
|
|
|
|
{
|
|
|
|
.UseSqlite(connection)
|
|
|
|
Name = "Zeus",
|
|
|
|
.Options;
|
|
|
|
Bio = "Zeus is the king of the gods."
|
|
|
|
|
|
|
|
};
|
|
|
|
using (var context = new MyDbContext(options))
|
|
|
|
var Poseidon = new ChampionEntity
|
|
|
|
{
|
|
|
|
{
|
|
|
|
await context.Database.EnsureCreatedAsync();
|
|
|
|
Name = "Poseidon",
|
|
|
|
var Dieu = new ChampionEntity
|
|
|
|
Bio = "Poseidon is the king of the sea."
|
|
|
|
{
|
|
|
|
};
|
|
|
|
Name = name,
|
|
|
|
|
|
|
|
Bio = bio
|
|
|
|
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
|
|
|
|
};
|
|
|
|
Assert.Null(found);
|
|
|
|
|
|
|
|
|
|
|
|
ChampionEntity found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
|
|
|
|
await context.Champions.AddAsync(Zeus);
|
|
|
|
Assert.Null(found);
|
|
|
|
await context.Champions.AddAsync(Poseidon);
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
await context.Champions.AddAsync(Dieu);
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == "Zeus");
|
|
|
|
|
|
|
|
Assert.NotNull(found);
|
|
|
|
found = await context.Champions.SingleOrDefaultAsync(c => c.Name == name);
|
|
|
|
|
|
|
|
Assert.NotNull(found);
|
|
|
|
Assert.Equal(2, await context.Champions.CountAsync());
|
|
|
|
|
|
|
|
Assert.Equal("Zeus", found.Name);
|
|
|
|
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)
|
|
|
|
[Fact]
|
|
|
|
if (expected)
|
|
|
|
public void ModifyTestInMemory()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Assert.True(found.Name.Length <= 30);
|
|
|
|
var options = new DbContextOptionsBuilder<ChampionContext>()
|
|
|
|
Assert.True(found.Bio.Length <= 256);
|
|
|
|
.UseInMemoryDatabase(databaseName: "Modify_Test_database")
|
|
|
|
}
|
|
|
|
.Options;
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
Assert.False(found.Bio.Length <= 256);
|
|
|
|
|
|
|
|
Assert.False(found.Name.Length <= 30);
|
|
|
|
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." };
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
context.Champions.Add(chewie);
|
|
|
|
public void ModifyTestInMemory()
|
|
|
|
context.Champions.Add(yoda);
|
|
|
|
{
|
|
|
|
context.Champions.Add(ewok);
|
|
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
|
|
context.SaveChanges();
|
|
|
|
connection.Open();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var options = new DbContextOptionsBuilder<MyDbContext>()
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
.UseSqlite(connection)
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
.Options;
|
|
|
|
{
|
|
|
|
|
|
|
|
string nameToFind = "ew";
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
|
|
|
using (var context = new MyDbContext(options))
|
|
|
|
nameToFind = "ewo";
|
|
|
|
{
|
|
|
|
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
|
|
|
//context.Database.OpenConnection();
|
|
|
|
var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
|
|
|
|
context.Database.EnsureCreated();
|
|
|
|
ewok.Name = "Wicket";
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
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." };
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
|
|
|
|
{
|
|
|
|
context.Champions.Add(chewie);
|
|
|
|
string nameToFind = "ew";
|
|
|
|
context.Champions.Add(yoda);
|
|
|
|
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
|
|
|
context.Champions.Add(ewok);
|
|
|
|
nameToFind = "wick";
|
|
|
|
context.SaveChanges();
|
|
|
|
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//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());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|