diff --git a/Sources/EFLol/EFLol.csproj b/Sources/EFLol/EFLol.csproj index 740e3bf..3d9abc2 100644 --- a/Sources/EFLol/EFLol.csproj +++ b/Sources/EFLol/EFLol.csproj @@ -1,27 +1,27 @@ - - - - Exe - net6.0 - enable - enable - $(MSBuildProjectDirectory) - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - + + + + Exe + net6.0 + enable + enable + $(MSBuildProjectDirectory) + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + + + + + diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index a6fec54..da2b1dd 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -2,35 +2,52 @@ using EFLol; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; namespace TestUnitaire { public class TestEfLol { [Fact] - public void TestAddIntoDB() + public async Task TestAddInMemory() { // Arrange - ChampionEntity Zeus = new ChampionEntity - { - Name = "Zeus", - Bio = "Zeus is the king of the gods." - }; + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); - // Act - var context = new ChampionContext(); - context.Champions.Add(Zeus); - context.SaveChanges(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; - // Assert - var champion = context.Champions.FirstOrDefault(c => c.Name == "Zeus"); - if (champion == null) + // 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 { - 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); + 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); + } } } }