|
|
@ -2,50 +2,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using EFLol;
|
|
|
|
using EFLol;
|
|
|
|
|
|
|
|
using Microsoft.Data.Sqlite;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
|
|
namespace TestUnitaire
|
|
|
|
namespace TestUnitaire
|
|
|
|
{
|
|
|
|
{
|
|
|
|
public class TestEfLol
|
|
|
|
public class TestEfLol
|
|
|
|
{
|
|
|
|
{
|
|
|
|
[Fact]
|
|
|
|
|
|
|
|
public void TestAddIntoDB()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Arrange
|
|
|
|
|
|
|
|
ChampionEntity Zeus = new ChampionEntity
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Name = "Zeus",
|
|
|
|
|
|
|
|
Bio = "Zeus is the king of the gods."
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Act
|
|
|
|
|
|
|
|
var context = new ChampionContext();
|
|
|
|
|
|
|
|
context.Champions.Add(Zeus);
|
|
|
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
|
|
|
|
var champion = context.Champions.FirstOrDefault(c => c.Name == "Zeus");
|
|
|
|
|
|
|
|
if (champion == null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Assert.True(false, "Champion not found in database.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert.NotNull(champion);
|
|
|
|
|
|
|
|
Assert.Equal("Zeus", champion.Name);
|
|
|
|
|
|
|
|
Assert.Equal("Zeus is the king of the gods.", champion.Bio);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//InMemoryTest
|
|
|
|
//InMemoryTest
|
|
|
|
[Fact]
|
|
|
|
[Fact]
|
|
|
|
public void AddInMemory()
|
|
|
|
public void AddInMemory()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//connection must be opened to use In-memory database
|
|
|
|
|
|
|
|
var connection = new SqliteConnection("DataSource=:memory:");
|
|
|
|
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
|
|
|
|
var options = new DbContextOptionsBuilder<ChampionContext>()
|
|
|
|
var options = new DbContextOptionsBuilder<ChampionContext>()
|
|
|
|
.UseInMemoryDatabase(databaseName: "Add_Test_database").Options;
|
|
|
|
.UseSqlite(connection)
|
|
|
|
|
|
|
|
.Options;
|
|
|
|
|
|
|
|
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
|
|
|
|
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
//context.Database.OpenConnection();
|
|
|
|
|
|
|
|
context.Database.EnsureCreated();
|
|
|
|
|
|
|
|
|
|
|
|
ChampionEntity chewie = new ChampionEntity { Name = "Chewbacca", Bio = "Zeus is the king of the gods." };
|
|
|
|
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 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." };
|
|
|
|
ChampionEntity ewok = new ChampionEntity { Name = "Ewok", Bio = "Zeus is the king of the gods." };
|
|
|
@ -56,9 +39,11 @@ namespace TestUnitaire
|
|
|
|
context.SaveChanges();
|
|
|
|
context.SaveChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//prepares the database with one instance of the context
|
|
|
|
//uses another instance of the context to do the tests
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
using (var context = new ChampionContext(options))
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
context.Database.EnsureCreated();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.Equal(3, context.Champions.Count());
|
|
|
|
Assert.Equal(3, context.Champions.Count());
|
|
|
|
Assert.Equal("Chewbacca", context.Champions.First().Name);
|
|
|
|
Assert.Equal("Chewbacca", context.Champions.First().Name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|