From 35c719e7b4e029e84d329ad55ebd109cd91b8dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Chevaldonn=C3=A9?= Date: Wed, 1 Jan 2020 23:35:34 +0100 Subject: [PATCH] updated sample ex_041_004; readme should be renewed --- Exemples.sln | 92 ++++++++++++---- .../Program.cs | 3 +- ...ex_041_004_ConsoleTests_w_SqlServer.csproj | 12 +++ .../ex_041_004_TestingInMemory/ReadMe.md | 20 ++-- .../ex_041_004_TestingInMemory.csproj | 3 +- .../NounoursDB_Tests.cs | 22 ++-- .../ex_041_004_UnitTests_w_InMemory.csproj} | 0 .../NounoursDB_Tests.cs | 102 ++++++++++++++++++ ..._041_004_UnitTests_w_SQLiteInMemory.csproj | 21 ++++ 9 files changed, 230 insertions(+), 45 deletions(-) rename p08_BDD_EntityFramework/{ex_041_004_TestingInMemory => ex_041_004_ConsoleTests_w_SqlServer}/Program.cs (89%) create mode 100644 p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/ex_041_004_ConsoleTests_w_SqlServer.csproj rename p08_BDD_EntityFramework/{ex_041_004_UnitTests => ex_041_004_UnitTests_w_InMemory}/NounoursDB_Tests.cs (72%) rename p08_BDD_EntityFramework/{ex_041_004_UnitTests/ex_041_004_UnitTests.csproj => ex_041_004_UnitTests_w_InMemory/ex_041_004_UnitTests_w_InMemory.csproj} (100%) create mode 100644 p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/NounoursDB_Tests.cs create mode 100644 p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/ex_041_004_UnitTests_w_SQLiteInMemory.csproj diff --git a/Exemples.sln b/Exemples.sln index cc31b2c..04c3473 100644 --- a/Exemples.sln +++ b/Exemples.sln @@ -546,10 +546,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_LISEZ-MOI_ !!", "_LISEZ-MO EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_041_001_ConnectionStrings", "p08_BDD_EntityFramework\ex_041_001_ConnectionStrings\ex_041_001_ConnectionStrings.csproj", "{097B0926-6663-4B7E-BBBE-DAA7EE278C7C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_041_004_UnitTests", "p08_BDD_EntityFramework\ex_041_004_UnitTests\ex_041_004_UnitTests.csproj", "{00BDA989-5CC0-42C0-A2D4-B79429F2FE17}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_041_004_TestingInMemory", "p08_BDD_EntityFramework\ex_041_004_TestingInMemory\ex_041_004_TestingInMemory.csproj", "{166633F1-D1B3-4038-B015-B238A14C97C3}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ex_041_004_UnitTests_w_InMemory", "p08_BDD_EntityFramework\ex_041_004_UnitTests_w_InMemory\ex_041_004_UnitTests_w_InMemory.csproj", "{8C4BC92E-2E7D-4447-805F-0BCAF986022E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ex_041_004_ConsoleTests_w_SqlServer", "p08_BDD_EntityFramework\ex_041_004_ConsoleTests_w_SqlServer\ex_041_004_ConsoleTests_w_SqlServer.csproj", "{4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ex_041_004_UnitTests_w_SQLiteInMemory", "p08_BDD_EntityFramework\ex_041_004_UnitTests_w_SQLiteInMemory\ex_041_004_UnitTests_w_SQLiteInMemory.csproj", "{2BE75812-1DC2-4E9A-8D99-99456F11AB0D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -4526,26 +4530,6 @@ Global {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x64.Build.0 = Release|Any CPU {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x86.ActiveCfg = Release|Any CPU {097B0926-6663-4B7E-BBBE-DAA7EE278C7C}.Release|x86.Build.0 = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|Any CPU.Build.0 = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|ARM.ActiveCfg = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|ARM.Build.0 = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|ARM64.ActiveCfg = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|ARM64.Build.0 = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|x64.ActiveCfg = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|x64.Build.0 = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|x86.ActiveCfg = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Debug|x86.Build.0 = Debug|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|Any CPU.ActiveCfg = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|Any CPU.Build.0 = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|ARM.ActiveCfg = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|ARM.Build.0 = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|ARM64.ActiveCfg = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|ARM64.Build.0 = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|x64.ActiveCfg = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|x64.Build.0 = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|x86.ActiveCfg = Release|Any CPU - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17}.Release|x86.Build.0 = Release|Any CPU {166633F1-D1B3-4038-B015-B238A14C97C3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {166633F1-D1B3-4038-B015-B238A14C97C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {166633F1-D1B3-4038-B015-B238A14C97C3}.Debug|ARM.ActiveCfg = Debug|Any CPU @@ -4566,6 +4550,66 @@ Global {166633F1-D1B3-4038-B015-B238A14C97C3}.Release|x64.Build.0 = Release|Any CPU {166633F1-D1B3-4038-B015-B238A14C97C3}.Release|x86.ActiveCfg = Release|Any CPU {166633F1-D1B3-4038-B015-B238A14C97C3}.Release|x86.Build.0 = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|ARM.ActiveCfg = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|ARM.Build.0 = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|ARM64.Build.0 = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|x64.ActiveCfg = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|x64.Build.0 = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|x86.ActiveCfg = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Debug|x86.Build.0 = Debug|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|Any CPU.Build.0 = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|ARM.ActiveCfg = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|ARM.Build.0 = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|ARM64.ActiveCfg = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|ARM64.Build.0 = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|x64.ActiveCfg = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|x64.Build.0 = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|x86.ActiveCfg = Release|Any CPU + {8C4BC92E-2E7D-4447-805F-0BCAF986022E}.Release|x86.Build.0 = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|ARM.ActiveCfg = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|ARM.Build.0 = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|ARM64.Build.0 = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|x64.ActiveCfg = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|x64.Build.0 = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|x86.ActiveCfg = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Debug|x86.Build.0 = Debug|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|Any CPU.Build.0 = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|ARM.ActiveCfg = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|ARM.Build.0 = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|ARM64.ActiveCfg = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|ARM64.Build.0 = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|x64.ActiveCfg = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|x64.Build.0 = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|x86.ActiveCfg = Release|Any CPU + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C}.Release|x86.Build.0 = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|ARM.ActiveCfg = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|ARM.Build.0 = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|ARM64.ActiveCfg = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|ARM64.Build.0 = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|x64.ActiveCfg = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|x64.Build.0 = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|x86.ActiveCfg = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Debug|x86.Build.0 = Debug|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|Any CPU.Build.0 = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|ARM.ActiveCfg = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|ARM.Build.0 = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|ARM64.ActiveCfg = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|ARM64.Build.0 = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|x64.ActiveCfg = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|x64.Build.0 = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|x86.ActiveCfg = Release|Any CPU + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -4824,8 +4868,10 @@ Global {56C5A51B-16E9-4F93-9C32-8F91710391E8} = {65DF0C9A-4F06-4361-8DD9-E8360B6325CA} {AB06CC3B-6D82-48D1-89FC-14C96C77FE7F} = {65DF0C9A-4F06-4361-8DD9-E8360B6325CA} {097B0926-6663-4B7E-BBBE-DAA7EE278C7C} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} - {00BDA989-5CC0-42C0-A2D4-B79429F2FE17} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} {166633F1-D1B3-4038-B015-B238A14C97C3} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} + {8C4BC92E-2E7D-4447-805F-0BCAF986022E} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} + {4AE5E57F-2D9B-40BD-B1C3-B39AE756172C} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} + {2BE75812-1DC2-4E9A-8D99-99456F11AB0D} = {56C5A51B-16E9-4F93-9C32-8F91710391E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8D31C3AE-36FF-4667-A2A7-0E670245A59E} diff --git a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/Program.cs b/p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/Program.cs similarity index 89% rename from p08_BDD_EntityFramework/ex_041_004_TestingInMemory/Program.cs rename to p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/Program.cs index 6b83ec0..395b034 100644 --- a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/Program.cs +++ b/p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/Program.cs @@ -1,6 +1,7 @@ using System; +using ex_041_004_TestingInMemory; -namespace ex_041_004_TestingInMemory +namespace ex_041_004_ConsoleTests_w_SqlServer { class Program { diff --git a/p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/ex_041_004_ConsoleTests_w_SqlServer.csproj b/p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/ex_041_004_ConsoleTests_w_SqlServer.csproj new file mode 100644 index 0000000..8ca548b --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_004_ConsoleTests_w_SqlServer/ex_041_004_ConsoleTests_w_SqlServer.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.0 + + + + + + + diff --git a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ReadMe.md b/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ReadMe.md index d214572..d755f32 100644 --- a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ReadMe.md +++ b/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ReadMe.md @@ -1,17 +1,21 @@ -# ex_041_004_InMemory -*31/12/2019 ⋅ Marc Chevaldonné* +# ex_041_004_TestingInMemory +*01/01/2020 ⋅ Marc Chevaldonné* --- -Le fournisseur **InMemory** est utilisé pour tester des bases de données sans avoir à créer réellement une base de données. -Ceci permet de tester les accès à votre "base" de manière toutefois approximative. -En effet, **InMemory** n'est pas une base de données relationnelle : il y a donc des limitations. -Cet exemple montre comment utiliser **InMemory** à travers une injection de dépendance. En d'autres termes, vous continuez à définir votre chaîne de connexion sur une base de données, mais vous permettez néanmoins l'utilisation, à la demande, de **InMemory** pour des tests. -Puisque ce fournisseur devient intéressant dans le cas de tests, j'ai donc ajouté un 2ème projet lié à cet exemple, permettant d'avoir accès à des tests unitaires utilisant **InMemory**. -Pour le reste de l'exemple, celui-ci n'apporte rien de nouveau par rapport à l'exemple ex_041_001. +Lorsqu'on cherche à tester notre code et nos accès à la base de données, on n'a pas nécessairement envie de créer la base juste pour les tests. +Pour cela, il existe des solutions et des fournisseurs permettant de tester les bases sans avoir à réellement les créer : +* le fournisseur **InMemory** permet ceci mais de manière approximative, car **InMemory** n'est pas une base de données relationnelle : il y a donc des limitations. +* **SQLite** possède un mode *In-Memory* qui lui, permet de tester une base de données relationnelle, sans avoir à créer une base de données. + +Cet exemple montre comment utiliser **InMemory** et **SQLite in-memory** à travers une injection de dépendance. En d'autres termes, vous continuez à définir votre chaîne de connexion sur une base de données, mais vous permettez néanmoins l'utilisation, à la demande, de **InMemory** pour des tests. +Puisque ce fournisseur devient intéressant dans le cas de tests, j'ai donc ajouté un 2ème projet lié à cet exemple, permettant d'avoir accès à des tests unitaires utilisant **InMemory** ou de **SQLite in-memory**. +Pour le reste de l'exemple, celui-ci n'apporte rien de nouveau par rapport à l'exemple ex_041_001 concernant l'utilisation d'**Entity Framework Core**. --- +## Pourquoi autant de projets dans cet exemple ? + ## Comment a été construit cet exemple ? Cet exemple est tout d'abord construit de la même manière que l'exemple *ex_041_001_ConnectionStrings*. Il ne faut pas oublier les NuGet nécessaires : diff --git a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ex_041_004_TestingInMemory.csproj b/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ex_041_004_TestingInMemory.csproj index dc4bf52..065d1ee 100644 --- a/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ex_041_004_TestingInMemory.csproj +++ b/p08_BDD_EntityFramework/ex_041_004_TestingInMemory/ex_041_004_TestingInMemory.csproj @@ -1,8 +1,7 @@ - Exe - netcoreapp3.0 + netstandard2.0 diff --git a/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_InMemory/NounoursDB_Tests.cs similarity index 72% rename from p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs rename to p08_BDD_EntityFramework/ex_041_004_UnitTests_w_InMemory/NounoursDB_Tests.cs index f0fc322..f74ab0c 100644 --- a/p08_BDD_EntityFramework/ex_041_004_UnitTests/NounoursDB_Tests.cs +++ b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_InMemory/NounoursDB_Tests.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using System.Linq; using Xunit; -namespace ex_041_004_UnitTests +namespace ex_041_004_UnitTests_w_InMemory { public class NounoursDB_Tests { @@ -14,7 +14,7 @@ namespace ex_041_004_UnitTests .UseInMemoryDatabase(databaseName: "Add_Test_database") .Options; - // Run the test against one instance of the context + //prepares the database with one instance of the context using (var context = new NounoursContext(options)) { Nounours chewie = new Nounours { Nom = "Chewbacca" }; @@ -27,7 +27,7 @@ namespace ex_041_004_UnitTests context.SaveChanges(); } - // Use a separate instance of the context to verify correct data was saved to database + //prepares the database with one instance of the context using (var context = new NounoursContext(options)) { Assert.Equal(3, context.Nounours.Count()); @@ -42,7 +42,7 @@ namespace ex_041_004_UnitTests .UseInMemoryDatabase(databaseName: "Modify_Test_database") .Options; - // Run the test against one instance of the context + //prepares the database with one instance of the context using (var context = new NounoursContext(options)) { Nounours chewie = new Nounours { Nom = "Chewbacca" }; @@ -55,25 +55,25 @@ namespace ex_041_004_UnitTests context.SaveChanges(); } - // Use a separate instance of the context to verify correct data was saved to database + //prepares the database with one instance of the context using (var context = new NounoursContext(options)) { string nameToFind = "ew"; - Assert.Equal(2, context.Nounours.Where(n => n.Nom.Contains(nameToFind, System.StringComparison.CurrentCultureIgnoreCase)).Count()); + Assert.Equal(2, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); nameToFind = "ewo"; - Assert.Equal(1, context.Nounours.Where(n => n.Nom.Contains(nameToFind, System.StringComparison.CurrentCultureIgnoreCase)).Count()); - var ewok = context.Nounours.Where(n => n.Nom.Contains(nameToFind, System.StringComparison.CurrentCultureIgnoreCase)).First(); + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).First(); ewok.Nom = "Wicket"; context.SaveChanges(); } - // Use a separate instance of the context to verify correct data was saved to database + //prepares the database with one instance of the context using (var context = new NounoursContext(options)) { string nameToFind = "ew"; - Assert.Equal(1, context.Nounours.Where(n => n.Nom.Contains(nameToFind, System.StringComparison.CurrentCultureIgnoreCase)).Count()); + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); nameToFind = "wick"; - Assert.Equal(1, context.Nounours.Where(n => n.Nom.Contains(nameToFind, System.StringComparison.CurrentCultureIgnoreCase)).Count()); + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); } } } diff --git a/p08_BDD_EntityFramework/ex_041_004_UnitTests/ex_041_004_UnitTests.csproj b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_InMemory/ex_041_004_UnitTests_w_InMemory.csproj similarity index 100% rename from p08_BDD_EntityFramework/ex_041_004_UnitTests/ex_041_004_UnitTests.csproj rename to p08_BDD_EntityFramework/ex_041_004_UnitTests_w_InMemory/ex_041_004_UnitTests_w_InMemory.csproj diff --git a/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/NounoursDB_Tests.cs b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/NounoursDB_Tests.cs new file mode 100644 index 0000000..b1f1eb6 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/NounoursDB_Tests.cs @@ -0,0 +1,102 @@ +using ex_041_004_TestingInMemory; +using Microsoft.EntityFrameworkCore; +using System.Linq; +using Xunit; +using Microsoft.Data.Sqlite; + + +namespace ex_041_004_UnitTests_w_SQLiteInMemory +{ + public class NounoursDB_Tests + { + [Fact] + public void Add_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + //prepares the database with one instance of the context + using (var context = new NounoursContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Nounours chewie = new Nounours { Nom = "Chewbacca" }; + Nounours yoda = new Nounours { Nom = "Yoda" }; + Nounours ewok = new Nounours { Nom = "Ewok" }; + + context.Nounours.Add(chewie); + context.Nounours.Add(yoda); + context.Nounours.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new NounoursContext(options)) + { + context.Database.EnsureCreated(); + + Assert.Equal(3, context.Nounours.Count()); + Assert.Equal("Chewbacca", context.Nounours.First().Nom); + } + } + + [Fact] + public void Modify_Test() + { + //connection must be opened to use In-memory database + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + //prepares the database with one instance of the context + using (var context = new NounoursContext(options)) + { + //context.Database.OpenConnection(); + context.Database.EnsureCreated(); + + Nounours chewie = new Nounours { Nom = "Chewbacca" }; + Nounours yoda = new Nounours { Nom = "Yoda" }; + Nounours ewok = new Nounours { Nom = "Ewok" }; + + context.Nounours.Add(chewie); + context.Nounours.Add(yoda); + context.Nounours.Add(ewok); + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new NounoursContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(2, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wo"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + var ewok = context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).First(); + ewok.Nom = "Wicket"; + context.SaveChanges(); + } + + //uses another instance of the context to do the tests + using (var context = new NounoursContext(options)) + { + context.Database.EnsureCreated(); + + string nameToFind = "ew"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + nameToFind = "wick"; + Assert.Equal(1, context.Nounours.Where(n => n.Nom.ToLower().Contains(nameToFind)).Count()); + } + } + } +} diff --git a/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/ex_041_004_UnitTests_w_SQLiteInMemory.csproj b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/ex_041_004_UnitTests_w_SQLiteInMemory.csproj new file mode 100644 index 0000000..9ee8fa3 --- /dev/null +++ b/p08_BDD_EntityFramework/ex_041_004_UnitTests_w_SQLiteInMemory/ex_041_004_UnitTests_w_SQLiteInMemory.csproj @@ -0,0 +1,21 @@ + + + + netcoreapp3.0 + + false + + + + + + + + + + + + + + +