From 5d02ef15233920fff53c49705dbe404d75ca3ebd Mon Sep 17 00:00:00 2001 From: baollier1 Date: Sat, 4 Feb 2023 11:39:52 +0100 Subject: [PATCH 1/2] add modifytestin memory --- Sources/EFLol/EFLol.csproj | 1 + Sources/Model/Model.csproj | 1 + Sources/Shared/Shared.csproj | 1 + Sources/StubLib/StubLib.csproj | 1 + Sources/TestUnitaire/TestEfLol.cs | 76 ++++++++++++++++++- Sources/TestUnitaire/TestUnitaire.csproj | 1 + .../Tests/ConsoleTests/ConsoleTests.csproj | 1 + Sources/apiLOL/apiLOL.csproj | 1 + 8 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Sources/EFLol/EFLol.csproj b/Sources/EFLol/EFLol.csproj index 740e3bf..bbc403a 100644 --- a/Sources/EFLol/EFLol.csproj +++ b/Sources/EFLol/EFLol.csproj @@ -14,6 +14,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Sources/Model/Model.csproj b/Sources/Model/Model.csproj index 63fa530..6b14603 100644 --- a/Sources/Model/Model.csproj +++ b/Sources/Model/Model.csproj @@ -14,6 +14,7 @@ + all diff --git a/Sources/Shared/Shared.csproj b/Sources/Shared/Shared.csproj index 000356c..2d1f651 100644 --- a/Sources/Shared/Shared.csproj +++ b/Sources/Shared/Shared.csproj @@ -8,6 +8,7 @@ + all diff --git a/Sources/StubLib/StubLib.csproj b/Sources/StubLib/StubLib.csproj index a51603e..8b38e43 100644 --- a/Sources/StubLib/StubLib.csproj +++ b/Sources/StubLib/StubLib.csproj @@ -8,6 +8,7 @@ + all diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index a6fec54..91b5a99 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -2,7 +2,8 @@ using EFLol; - +using Microsoft.EntityFrameworkCore; + namespace TestUnitaire { public class TestEfLol @@ -31,6 +32,79 @@ namespace TestUnitaire Assert.NotNull(champion); Assert.Equal("Zeus", champion.Name); Assert.Equal("Zeus is the king of the gods.", champion.Bio); + } + + + //InMemoryTest + [Fact] + public void AddInMemory() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Add_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)) + { + Assert.Equal(3, context.Champions.Count()); + Assert.Equal("Chewbacca", context.Champions.First().Name); + } + } + + [Fact] + public void ModifyTestInMemory() + { + var options = new DbContextOptionsBuilder() + .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()); + } } } } diff --git a/Sources/TestUnitaire/TestUnitaire.csproj b/Sources/TestUnitaire/TestUnitaire.csproj index e625409..e417697 100644 --- a/Sources/TestUnitaire/TestUnitaire.csproj +++ b/Sources/TestUnitaire/TestUnitaire.csproj @@ -9,6 +9,7 @@ + diff --git a/Sources/Tests/ConsoleTests/ConsoleTests.csproj b/Sources/Tests/ConsoleTests/ConsoleTests.csproj index 69373bc..9364fb3 100644 --- a/Sources/Tests/ConsoleTests/ConsoleTests.csproj +++ b/Sources/Tests/ConsoleTests/ConsoleTests.csproj @@ -16,6 +16,7 @@ + all diff --git a/Sources/apiLOL/apiLOL.csproj b/Sources/apiLOL/apiLOL.csproj index f112330..4775ca4 100644 --- a/Sources/apiLOL/apiLOL.csproj +++ b/Sources/apiLOL/apiLOL.csproj @@ -8,6 +8,7 @@ + all From ba9e84ef4be50391310857c7df0ef7553291b6c3 Mon Sep 17 00:00:00 2001 From: baollier1 Date: Sat, 4 Feb 2023 11:58:53 +0100 Subject: [PATCH 2/2] add test modify --- Sources/Model/Model.csproj | 1 + Sources/Shared/Shared.csproj | 1 + Sources/StubLib/StubLib.csproj | 1 + Sources/TestUnitaire/TestEfLol.cs | 43 ++++++------------- Sources/TestUnitaire/TestUnitaire.csproj | 1 + .../Tests/ConsoleTests/ConsoleTests.csproj | 1 + Sources/apiLOL/apiLOL.csproj | 1 + 7 files changed, 20 insertions(+), 29 deletions(-) diff --git a/Sources/Model/Model.csproj b/Sources/Model/Model.csproj index 6b14603..9844555 100644 --- a/Sources/Model/Model.csproj +++ b/Sources/Model/Model.csproj @@ -15,6 +15,7 @@ + all diff --git a/Sources/Shared/Shared.csproj b/Sources/Shared/Shared.csproj index 2d1f651..a35cef7 100644 --- a/Sources/Shared/Shared.csproj +++ b/Sources/Shared/Shared.csproj @@ -9,6 +9,7 @@ + all diff --git a/Sources/StubLib/StubLib.csproj b/Sources/StubLib/StubLib.csproj index 8b38e43..65ff122 100644 --- a/Sources/StubLib/StubLib.csproj +++ b/Sources/StubLib/StubLib.csproj @@ -9,6 +9,7 @@ + all diff --git a/Sources/TestUnitaire/TestEfLol.cs b/Sources/TestUnitaire/TestEfLol.cs index 91b5a99..ecb5a1a 100644 --- a/Sources/TestUnitaire/TestEfLol.cs +++ b/Sources/TestUnitaire/TestEfLol.cs @@ -2,50 +2,33 @@ using EFLol; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; namespace TestUnitaire { 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 [Fact] public void AddInMemory() { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "Add_Test_database").Options; + .UseSqlite(connection) + .Options; //prepares the database with one instance of the context - 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 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." }; @@ -56,9 +39,11 @@ namespace TestUnitaire 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)) { + context.Database.EnsureCreated(); + Assert.Equal(3, context.Champions.Count()); Assert.Equal("Chewbacca", context.Champions.First().Name); } diff --git a/Sources/TestUnitaire/TestUnitaire.csproj b/Sources/TestUnitaire/TestUnitaire.csproj index e417697..c7b05a5 100644 --- a/Sources/TestUnitaire/TestUnitaire.csproj +++ b/Sources/TestUnitaire/TestUnitaire.csproj @@ -10,6 +10,7 @@ + diff --git a/Sources/Tests/ConsoleTests/ConsoleTests.csproj b/Sources/Tests/ConsoleTests/ConsoleTests.csproj index 9364fb3..557bdc5 100644 --- a/Sources/Tests/ConsoleTests/ConsoleTests.csproj +++ b/Sources/Tests/ConsoleTests/ConsoleTests.csproj @@ -17,6 +17,7 @@ + all diff --git a/Sources/apiLOL/apiLOL.csproj b/Sources/apiLOL/apiLOL.csproj index 4775ca4..82733d5 100644 --- a/Sources/apiLOL/apiLOL.csproj +++ b/Sources/apiLOL/apiLOL.csproj @@ -9,6 +9,7 @@ + all