From a3d86c625bc6724d0fb30569dc46b852bd60ee39 Mon Sep 17 00:00:00 2001 From: David D'ALMEIDA Date: Thu, 14 Mar 2024 23:45:35 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Mise=20=C3=A0=20jour=20de=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e00e2f0..97126b3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # EF_WebAPI -This repository make a meeting of EF and WebAPI parts. \ No newline at end of file +This repository make a meeting of EF and WebAPI parts. + +FROM /src dir + +do + +```bash +dotnet ef migrations add --project StubbedContextLib/StubbedContextLib.csproj --startup-project HeartTrackAPI/HeartTrackAPI.csproj --context StubbedContextLib.TrainingStubbedContext --configuration Debug Initial --output-dir Migrations +``` +then + +```bash +dotnet ef database update --project StubbedContextLib/StubbedContextLib.csproj --startup-project HeartTrackAPI/HeartTrackAPI.csproj --context StubbedContextLib.TrainingStubbedContext --configuration Debug +``` From 205fb3882ae814835c524869ef84b95c134885b7 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 00:53:23 +0100 Subject: [PATCH 2/8] Tests --- .../StubbedContextLib.csproj | 4 + .../UnitTestsEntities/ActivityEntityTests.cs | 149 ++++++++++++++++++ src/Tests/UnitTestsEntities/UnitTest1.cs | 10 -- .../UnitTestsEntities.csproj | 5 + src/UnitTestsEntities2/UnitTest1.cs | 11 ++ .../UnitTestsEntities2.csproj | 23 +++ 6 files changed, 192 insertions(+), 10 deletions(-) create mode 100644 src/Tests/UnitTestsEntities/ActivityEntityTests.cs delete mode 100644 src/Tests/UnitTestsEntities/UnitTest1.cs create mode 100644 src/UnitTestsEntities2/UnitTest1.cs create mode 100644 src/UnitTestsEntities2/UnitTestsEntities2.csproj diff --git a/src/StubbedContextLib/StubbedContextLib.csproj b/src/StubbedContextLib/StubbedContextLib.csproj index bdd147f..0c5f479 100644 --- a/src/StubbedContextLib/StubbedContextLib.csproj +++ b/src/StubbedContextLib/StubbedContextLib.csproj @@ -13,6 +13,10 @@ + + + + net8.0 enable diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs new file mode 100644 index 0000000..bc8e091 --- /dev/null +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -0,0 +1,149 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + +public class ActivityEntityTests +{ + /*[Fact] + public void Add_Activity_Success() + { + + + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + // Act + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.SaveChanges(); + } + + // Assert + using (var context = new StubbedContext(options)) + { + var savedActivity = context.Activities.First(a => a.Type == "Running"); + Assert.NotNull(savedActivity); + } + } + + [Fact] + public void Update_Activity_Success() + { + // Arrange + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Update_Activity_Success") + .Options; + + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.SaveChanges(); + } + + // Act + using (var context = new StubbedContext(options)) + { + var savedActivity = context.Activities.First(a => a.Type == "Running"); + savedActivity.Type = "Walking"; + context.SaveChanges(); + } + + // Assert + using (var context = new StubbedContext(options)) + { + var updatedActivity = context.Activities.First(a => a.Type == "Walking"); + Assert.NotNull(updatedActivity); + } + } + + [Fact] + public void Delete_Activity_Success() + { + // Arrange + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "Delete_Activity_Success") + .Options; + + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.SaveChanges(); + } + + // Act + using (var context = new StubbedContext(options)) + { + var savedActivity = context.Activities.First(a => a.Type == "Running"); + context.Activities.Remove(savedActivity); + context.SaveChanges(); + } + + // Assert + using (var context = new StubbedContext(options)) + { + var deletedActivity = context.Activities.FirstOrDefault(a => a.Type == "Running"); + Assert.Null(deletedActivity); + } + }*/ +} + diff --git a/src/Tests/UnitTestsEntities/UnitTest1.cs b/src/Tests/UnitTestsEntities/UnitTest1.cs deleted file mode 100644 index 4494897..0000000 --- a/src/Tests/UnitTestsEntities/UnitTest1.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace UnitTestsEntities; - -public class UnitTest1 -{ - [Fact] - public void Test1() - { - - } -} \ No newline at end of file diff --git a/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj b/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj index 22b0134..e372137 100644 --- a/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj +++ b/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj @@ -10,6 +10,7 @@ + @@ -22,4 +23,8 @@ + + + + diff --git a/src/UnitTestsEntities2/UnitTest1.cs b/src/UnitTestsEntities2/UnitTest1.cs new file mode 100644 index 0000000..8b121a7 --- /dev/null +++ b/src/UnitTestsEntities2/UnitTest1.cs @@ -0,0 +1,11 @@ +namespace UnitTestsEntities2 +{ + public class UnitTest1 + { + [Fact] + public void Test1() + { + + } + } +} \ No newline at end of file diff --git a/src/UnitTestsEntities2/UnitTestsEntities2.csproj b/src/UnitTestsEntities2/UnitTestsEntities2.csproj new file mode 100644 index 0000000..9c5b30a --- /dev/null +++ b/src/UnitTestsEntities2/UnitTestsEntities2.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + From 06351b05f00b2f76e0eb57f97514d54414483b36 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 00:54:18 +0100 Subject: [PATCH 3/8] .. --- src/HeartTrack.sln | 66 +++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/src/HeartTrack.sln b/src/HeartTrack.sln index 6b62741..2427430 100644 --- a/src/HeartTrack.sln +++ b/src/HeartTrack.sln @@ -3,52 +3,51 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.0.31903.59 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{330A5DA0-0B4E-48D4-9AF3-6DCB39EE9622}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{330A5DA0-0B4E-48D4-9AF3-6DCB39EE9622}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{A07F86B3-555B-4B35-8BB1-25E844825A38}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{A07F86B3-555B-4B35-8BB1-25E844825A38}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{2F44DE6E-EFFC-42FE-AFF6-79CDC762E6D8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{2F44DE6E-EFFC-42FE-AFF6-79CDC762E6D8}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{2B227C67-3BEC-4A83-BDA0-F3918FBC0D18}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestEntities", "Tests\ConsoleTestEntities\ConsoleTestEntities.csproj", "{477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTestEntities", "Tests\ConsoleTestEntities\ConsoleTestEntities.csproj", "{477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestRelationships", "Tests\ConsoleTestRelationships\ConsoleTestRelationships.csproj", "{2D166FAD-4934-474B-96A8-6C0635156EC2}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTestRelationships", "Tests\ConsoleTestRelationships\ConsoleTestRelationships.csproj", "{2D166FAD-4934-474B-96A8-6C0635156EC2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dto", "Dto\Dto.csproj", "{562019BC-0F61-41B0-9BAE-259B92C6BFBA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dto", "Dto\Dto.csproj", "{562019BC-0F61-41B0-9BAE-259B92C6BFBA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeartTrackAPI", "HeartTrackAPI\HeartTrackAPI.csproj", "{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HeartTrackAPI", "HeartTrackAPI\HeartTrackAPI.csproj", "{C1C2EAC3-3347-466B-BFB6-2A9F11A3AE12}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{F80C60E1-1E06-46C2-96DE-42B1C7DE65BC}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TestsAPI", "TestsAPI", "{30FC2BE9-7397-445A-84AD-043CE70F4281}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClientTests", "Tests\TestsAPI\ClientTests\ClientTests.csproj", "{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientTests", "Tests\TestsAPI\ClientTests\ClientTests.csproj", "{9E4D3AC5-E6CA-4753-BD96-BF5EE793931A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{30AB7FAA-6072-40B6-A15E-9188B59144F9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{30AB7FAA-6072-40B6-A15E-9188B59144F9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestApi", "Tests\TestsAPI\UnitTestApi\UnitTestApi.csproj", "{E515C8B6-6282-4D8B-8523-7B3A13E4AF58}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestApi", "Tests\TestsAPI\UnitTestApi\UnitTestApi.csproj", "{E515C8B6-6282-4D8B-8523-7B3A13E4AF58}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestsEntities", "Tests\UnitTestsEntities\UnitTestsEntities.csproj", "{31FA8E5E-D642-4C43-A2B2-02B9832B2CEC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTestsEntities", "Tests\UnitTestsEntities\UnitTestsEntities.csproj", "{31FA8E5E-D642-4C43-A2B2-02B9832B2CEC}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model2Entities", "Model2Entities\Model2Entities.csproj", "{FA329DEF-4756-4A8B-84E9-5A625FF94CBF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model2Entities", "Model2Entities\Model2Entities.csproj", "{FA329DEF-4756-4A8B-84E9-5A625FF94CBF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubAPI", "StubAPI\StubAPI.csproj", "{C9BD0310-DC18-4356-B8A7-2B6959AF7813}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubAPI", "StubAPI\StubAPI.csproj", "{C9BD0310-DC18-4356-B8A7-2B6959AF7813}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestEFMapper", "Tests\ConsoleTestEFMapper\ConsoleTestEFMapper.csproj", "{73EA27F2-9F0C-443F-A5EE-2960C983A422}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTestEFMapper", "Tests\ConsoleTestEFMapper\ConsoleTestEFMapper.csproj", "{73EA27F2-9F0C-443F-A5EE-2960C983A422}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFMappers", "EFMappers\EFMappers.csproj", "{9397795D-F482-44C4-8443-A20AC26671AA}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFMappers", "EFMappers\EFMappers.csproj", "{9397795D-F482-44C4-8443-A20AC26671AA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIMappers", "APIMappers\APIMappers.csproj", "{41D18203-1688-43BD-A3AC-FD0C2BD81909}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIMappers", "APIMappers\APIMappers.csproj", "{41D18203-1688-43BD-A3AC-FD0C2BD81909}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestsEntities2", "UnitTestsEntities2\UnitTestsEntities2.csproj", "{43757AC2-2924-46CF-BCCD-80F1C1265B6A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {330A5DA0-0B4E-48D4-9AF3-6DCB39EE9622}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {330A5DA0-0B4E-48D4-9AF3-6DCB39EE9622}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -90,14 +89,6 @@ Global {30AB7FAA-6072-40B6-A15E-9188B59144F9}.Debug|Any CPU.Build.0 = Debug|Any CPU {30AB7FAA-6072-40B6-A15E-9188B59144F9}.Release|Any CPU.ActiveCfg = Release|Any CPU {30AB7FAA-6072-40B6-A15E-9188B59144F9}.Release|Any CPU.Build.0 = Release|Any CPU - {CB142F6B-0FF1-45B3-AB46-6F8DCD096C20}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CB142F6B-0FF1-45B3-AB46-6F8DCD096C20}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CB142F6B-0FF1-45B3-AB46-6F8DCD096C20}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CB142F6B-0FF1-45B3-AB46-6F8DCD096C20}.Release|Any CPU.Build.0 = Release|Any CPU - {B9679DCA-F4C8-45BE-A849-44E2BA814083}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B9679DCA-F4C8-45BE-A849-44E2BA814083}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B9679DCA-F4C8-45BE-A849-44E2BA814083}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B9679DCA-F4C8-45BE-A849-44E2BA814083}.Release|Any CPU.Build.0 = Release|Any CPU {E515C8B6-6282-4D8B-8523-7B3A13E4AF58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {E515C8B6-6282-4D8B-8523-7B3A13E4AF58}.Debug|Any CPU.Build.0 = Debug|Any CPU {E515C8B6-6282-4D8B-8523-7B3A13E4AF58}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -114,18 +105,10 @@ Global {C9BD0310-DC18-4356-B8A7-2B6959AF7813}.Debug|Any CPU.Build.0 = Debug|Any CPU {C9BD0310-DC18-4356-B8A7-2B6959AF7813}.Release|Any CPU.ActiveCfg = Release|Any CPU {C9BD0310-DC18-4356-B8A7-2B6959AF7813}.Release|Any CPU.Build.0 = Release|Any CPU - {06DBE9E4-6AA5-4D09-8544-D3ED91E2D980}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {06DBE9E4-6AA5-4D09-8544-D3ED91E2D980}.Debug|Any CPU.Build.0 = Debug|Any CPU - {06DBE9E4-6AA5-4D09-8544-D3ED91E2D980}.Release|Any CPU.ActiveCfg = Release|Any CPU - {06DBE9E4-6AA5-4D09-8544-D3ED91E2D980}.Release|Any CPU.Build.0 = Release|Any CPU {73EA27F2-9F0C-443F-A5EE-2960C983A422}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {73EA27F2-9F0C-443F-A5EE-2960C983A422}.Debug|Any CPU.Build.0 = Debug|Any CPU {73EA27F2-9F0C-443F-A5EE-2960C983A422}.Release|Any CPU.ActiveCfg = Release|Any CPU {73EA27F2-9F0C-443F-A5EE-2960C983A422}.Release|Any CPU.Build.0 = Release|Any CPU - {C9C9F2A5-9132-4067-B240-B299D2FCF4E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {C9C9F2A5-9132-4067-B240-B299D2FCF4E9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {C9C9F2A5-9132-4067-B240-B299D2FCF4E9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {C9C9F2A5-9132-4067-B240-B299D2FCF4E9}.Release|Any CPU.Build.0 = Release|Any CPU {9397795D-F482-44C4-8443-A20AC26671AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9397795D-F482-44C4-8443-A20AC26671AA}.Debug|Any CPU.Build.0 = Debug|Any CPU {9397795D-F482-44C4-8443-A20AC26671AA}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -134,6 +117,13 @@ Global {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Debug|Any CPU.Build.0 = Debug|Any CPU {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.ActiveCfg = Release|Any CPU {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.Build.0 = Release|Any CPU + {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} @@ -143,5 +133,9 @@ Global {E515C8B6-6282-4D8B-8523-7B3A13E4AF58} = {30FC2BE9-7397-445A-84AD-043CE70F4281} {31FA8E5E-D642-4C43-A2B2-02B9832B2CEC} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {73EA27F2-9F0C-443F-A5EE-2960C983A422} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} + {43757AC2-2924-46CF-BCCD-80F1C1265B6A} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F3487F4-66CA-4034-AC66-1BC899C9B523} EndGlobalSection EndGlobal From 55364ee99937cfb37e71eca28c892506e6c69574 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 01:30:13 +0100 Subject: [PATCH 4/8] =?UTF-8?q?:boom:=20:construction:=20D=C3=A9but=20des?= =?UTF-8?q?=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/HeartTrack.sln | 7 - .../UnitTestsEntities/ActivityEntityTests.cs | 33 +-- .../UnitTestsEntities/AthleteEntityTests.cs | 122 +++++++++++ .../DataSourceEntityTests.cs | 102 +++++++++ .../FriendshipEntityTests.cs | 206 ++++++++++++++++++ .../UnitTestsEntities/HeartRateEntityTests.cs | 172 +++++++++++++++ .../NotificationEntityTests.cs | 151 +++++++++++++ .../UnitTestsEntities/StatisticEntityTests.cs | 155 +++++++++++++ .../UnitTestsEntities/TrainingEntityTests.cs | 202 +++++++++++++++++ 9 files changed, 1122 insertions(+), 28 deletions(-) create mode 100644 src/Tests/UnitTestsEntities/AthleteEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/DataSourceEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/FriendshipEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/HeartRateEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/NotificationEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/StatisticEntityTests.cs create mode 100644 src/Tests/UnitTestsEntities/TrainingEntityTests.cs diff --git a/src/HeartTrack.sln b/src/HeartTrack.sln index 2427430..8947e4c 100644 --- a/src/HeartTrack.sln +++ b/src/HeartTrack.sln @@ -41,8 +41,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFMappers", "EFMappers\EFMa EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APIMappers", "APIMappers\APIMappers.csproj", "{41D18203-1688-43BD-A3AC-FD0C2BD81909}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestsEntities2", "UnitTestsEntities2\UnitTestsEntities2.csproj", "{43757AC2-2924-46CF-BCCD-80F1C1265B6A}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,10 +115,6 @@ Global {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Debug|Any CPU.Build.0 = Debug|Any CPU {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.ActiveCfg = Release|Any CPU {41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.Build.0 = Release|Any CPU - {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {43757AC2-2924-46CF-BCCD-80F1C1265B6A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -133,7 +127,6 @@ Global {E515C8B6-6282-4D8B-8523-7B3A13E4AF58} = {30FC2BE9-7397-445A-84AD-043CE70F4281} {31FA8E5E-D642-4C43-A2B2-02B9832B2CEC} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {73EA27F2-9F0C-443F-A5EE-2960C983A422} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} - {43757AC2-2924-46CF-BCCD-80F1C1265B6A} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {0F3487F4-66CA-4034-AC66-1BC899C9B523} diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs index bc8e091..62caaae 100644 --- a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore; public class ActivityEntityTests { - /*[Fact] + [Fact] public void Add_Activity_Success() { @@ -30,7 +30,7 @@ public class ActivityEntityTests AthleteId = 1 }; - // Act + /* using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); @@ -38,22 +38,18 @@ public class ActivityEntityTests context.SaveChanges(); } - // Assert using (var context = new StubbedContext(options)) { var savedActivity = context.Activities.First(a => a.Type == "Running"); Assert.NotNull(savedActivity); - } + Assert.Equal("Running", savedActivity.Type ); + + }*/ } [Fact] public void Update_Activity_Success() { - // Arrange - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "Update_Activity_Success") - .Options; - var activity = new ActivityEntity { Type = "Running", @@ -73,6 +69,7 @@ public class ActivityEntityTests AthleteId = 1 }; + /* using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); @@ -80,7 +77,6 @@ public class ActivityEntityTests context.SaveChanges(); } - // Act using (var context = new StubbedContext(options)) { var savedActivity = context.Activities.First(a => a.Type == "Running"); @@ -88,22 +84,18 @@ public class ActivityEntityTests context.SaveChanges(); } - // Assert using (var context = new StubbedContext(options)) { var updatedActivity = context.Activities.First(a => a.Type == "Walking"); Assert.NotNull(updatedActivity); - } + Assert.Equal("Walking", updatedActivity.Type ); + Assert.Equal(7, updatedActivity.EffortFelt ); + }*/ } [Fact] public void Delete_Activity_Success() { - // Arrange - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "Delete_Activity_Success") - .Options; - var activity = new ActivityEntity { Type = "Running", @@ -123,6 +115,7 @@ public class ActivityEntityTests AthleteId = 1 }; + /* using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); @@ -130,7 +123,6 @@ public class ActivityEntityTests context.SaveChanges(); } - // Act using (var context = new StubbedContext(options)) { var savedActivity = context.Activities.First(a => a.Type == "Running"); @@ -138,12 +130,11 @@ public class ActivityEntityTests context.SaveChanges(); } - // Assert using (var context = new StubbedContext(options)) { var deletedActivity = context.Activities.FirstOrDefault(a => a.Type == "Running"); Assert.Null(deletedActivity); - } - }*/ + }*/ + } } diff --git a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs new file mode 100644 index 0000000..878d7d8 --- /dev/null +++ b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs @@ -0,0 +1,122 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + +public class AthleteEntityTests +{ + [Fact] + public void Add_Athlete_Success() + { + var athlete = new AthleteEntity + { + Username = "john_doe", + LastName = "Doe", + FirstName = "John", + Email = "john.doe@example.com", + Sexe = "M", + Length = 180.0, + Weight = 75.5f, + Password = "password", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedAthlete = context.Athletes.First(a => a.Username == "john_doe"); + Assert.NotNull(savedAthlete); + Assert.Equal("john_doe", savedAthlete.Username); + }*/ + } + + [Fact] + public void Update_Athlete_Success() + { + var athlete = new AthleteEntity + { + Username = "jane_smith", + LastName = "Smith", + FirstName = "Jane", + Email = "jane.smith@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "password123", + DateOfBirth = new DateOnly(1995, 5, 10), + IsCoach = false + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedAthlete = context.Athletes.First(a => a.Username == "jane_smith"); + savedAthlete.Username = "jane_doe"; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedAthlete = context.Athletes.First(a => a.Username == "jane_doe"); + Assert.NotNull(updatedAthlete); + Assert.Equal("jane_doe", updatedAthlete.Username); + Assert.Equal("Smith", updatedAthlete.LastName); + }*/ + } + + [Fact] + public void Delete_Athlete_Success() + { + var athlete = new AthleteEntity + { + Username = "test_user", + LastName = "Test", + FirstName = "User", + Email = "test.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "testpassword", + DateOfBirth = new DateOnly(1985, 10, 20), + IsCoach = false + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedAthlete = context.Athletes.First(a => a.Username == "test_user"); + context.Athletes.Remove(savedAthlete); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedAthlete = context.Athletes.FirstOrDefault(a => a.Username == "test_user"); + Assert.Null(deletedAthlete); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs new file mode 100644 index 0000000..6f4c1db --- /dev/null +++ b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs @@ -0,0 +1,102 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + + +public class DataSourceEntityTests +{ + [Fact] + public void Add_DataSource_Success() + { + var dataSource = new DataSourceEntity + { + Type = "GPS", + Model = "Garmin Forerunner 945", + Precision = 0.1f + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.DataSources.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedDataSource = context.DataSources.First(d => d.Type == "GPS"); + Assert.NotNull(savedDataSource); + Assert.Equal("Garmin Forerunner 945", savedDataSource.Model); + }*/ + } + + [Fact] + public void Update_DataSource_Success() + { + var dataSource = new DataSourceEntity + { + Type = "Heart Rate Monitor", + Model = "Polar H10", + Precision = 0.2f + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.DataSources.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedDataSource = context.DataSources.First(d => d.Type == "Heart Rate Monitor"); + savedDataSource.Model = "Polar H9"; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedDataSource = context.DataSources.First(d => d.Model == "Polar H9"); + Assert.NotNull(updatedDataSource); + Assert.Equal("Heart Rate Monitor", updatedDataSource.Type); + Assert.Equal(0.2f, updatedDataSource.Precision); + }*/ + } + + [Fact] + public void Delete_DataSource_Success() + { + var dataSource = new DataSourceEntity + { + Type = "Smartwatch", + Model = "Apple Watch Series 6", + Precision = 0.05f + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.DataSources.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedDataSource = context.DataSources.First(d => d.Type == "Smartwatch"); + context.DataSources.Remove(savedDataSource); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedDataSource = context.DataSources.FirstOrDefault(d => d.Type == "Smartwatch"); + Assert.Null(deletedDataSource); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs new file mode 100644 index 0000000..492506c --- /dev/null +++ b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs @@ -0,0 +1,206 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + +public class FriendshipEntityTests +{ + [Fact] + public void Add_Friendship_Success() + { + var follower = new AthleteEntity + { + Username = "follower_user", + LastName = "Follower", + FirstName = "User", + Email = "follower.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "followerpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var following = new AthleteEntity + { + Username = "following_user", + LastName = "Following", + FirstName = "User", + Email = "following.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "followingpassword", + DateOfBirth = new DateOnly(1995, 5, 10), + IsCoach = false + }; + + var friendship = new FriendshipEntity + { + Follower = follower, + Following = following, + StartDate = DateTime.Now + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(follower); + context.Athletes.Add(following); + context.Friendships.Add(friendship); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedFriendship = context.Friendships.FirstOrDefault(); + Assert.NotNull(savedFriendship); + Assert.Equal(follower.IdAthlete, savedFriendship.FollowerId); + Assert.Equal(following.IdAthlete, savedFriendship.FollowingId); + }*/ + } + + [Fact] + public void Update_Friendship_Success() + { + var follower = new AthleteEntity + { + Username = "follower_user", + LastName = "Follower", + FirstName = "User", + Email = "follower.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "followerpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var following = new AthleteEntity + { + Username = "following_user", + LastName = "Following", + FirstName = "User", + Email = "following.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "followingpassword", + DateOfBirth = new DateOnly(1995, 5, 10), + IsCoach = false + }; + + var thirdAthlete = new AthleteEntity + { + Username = "third_user", + LastName = "Third", + FirstName = "User", + Email = "third.user@example.com", + Sexe = "M", + Length = 180.0, + Weight = 75.0f, + Password = "thirdpassword", + DateOfBirth = new DateOnly(1988, 3, 15), + IsCoach = false + }; + + var friendship = new FriendshipEntity + { + Follower = follower, + Following = following, + StartDate = DateTime.Now + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(follower); + context.Athletes.Add(following); + context.Athletes.Add(thirdAthlete); + context.Friendships.Add(friendship); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedFriendship = context.Friendships.FirstOrDefault(); + savedFriendship.Follower = thirdAthlete; // Update the follower + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedFriendship = context.Friendships.FirstOrDefault(); + Assert.NotNull(updatedFriendship); + Assert.Equal(thirdAthlete.IdAthlete, updatedFriendship.FollowerId); + }*/ + } + + [Fact] + public void Delete_Friendship_Success() + { + var follower = new AthleteEntity + { + Username = "follower_user", + LastName = "Follower", + FirstName = "User", + Email = "follower.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "followerpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var following = new AthleteEntity + { + Username = "following_user", + LastName = "Following", + FirstName = "User", + Email = "following.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "followingpassword", + DateOfBirth = new DateOnly(1995, 5, 10), + IsCoach = false + }; + + var friendship = new FriendshipEntity + { + Follower = follower, + Following = following, + StartDate = DateTime.Now + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(follower); + context.Athletes.Add(following); + context.Friendships.Add(friendship); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedFriendship = context.Friendships.FirstOrDefault(); + context.Friendships.Remove(savedFriendship); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedFriendship = context.Friendships.FirstOrDefault(); + Assert.Null(deletedFriendship); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs new file mode 100644 index 0000000..c3bc36f --- /dev/null +++ b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs @@ -0,0 +1,172 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + +public class HeartRateEntityTests +{ + [Fact] + public void Add_HeartRate_Success() + { + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + var heartRateEntry = new HeartRateEntity + { + Altitude = 100.0, + Time = new TimeOnly(9, 30), + Temperature = 20.0f, + Bpm = 150, + Longitude = 45.12345f, + Latitude = 35.6789f, + Activity = activity + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.HeartRates.Add(heartRateEntry); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedHeartRate = context.HeartRates.First(); + Assert.NotNull(savedHeartRate); + Assert.Equal(150, savedHeartRate.Bpm); + }*/ + } + + [Fact] + public void Update_HeartRate_Success() + { + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + var heartRateEntry = new HeartRateEntity + { + Altitude = 100.0, + Time = new TimeOnly(9, 30), + Temperature = 20.0f, + Bpm = 150, + Longitude = 45.12345f, + Latitude = 35.6789f, + Activity = activity + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.HeartRates.Add(heartRateEntry); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedHeartRate = context.HeartRates.First(); + savedHeartRate.Bpm = 160; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedHeartRate = context.HeartRates.First(); + Assert.NotNull(updatedHeartRate); + Assert.Equal(160, updatedHeartRate.Bpm); + }*/ + } + + [Fact] + public void Delete_HeartRate_Success() + { + var activity = new ActivityEntity + { + Type = "Running", + Date = new DateOnly(2024, 3, 15), + StartTime = new TimeOnly(9, 0), + EndTime = new TimeOnly(10, 0), + EffortFelt = 7, + Variability = 0.5f, + Variance = 0.2f, + StandardDeviation = 0.3f, + Average = 0.4f, + Maximum = 200, + Minimum = 100, + AverageTemperature = 25.5f, + HasAutoPause = true, + DataSourceId = 1, + AthleteId = 1 + }; + + var heartRateEntry = new HeartRateEntity + { + Altitude = 100.0, + Time = new TimeOnly(9, 30), + Temperature = 20.0f, + Bpm = 150, + Longitude = 45.12345f, + Latitude = 35.6789f, + Activity = activity + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Activities.Add(activity); + context.HeartRates.Add(heartRateEntry); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedHeartRate = context.HeartRates.First(); + context.HeartRates.Remove(savedHeartRate); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedHeartRate = context.HeartRates.FirstOrDefault(); + Assert.Null(deletedHeartRate); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs new file mode 100644 index 0000000..6d656a4 --- /dev/null +++ b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs @@ -0,0 +1,151 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + +public class NotificationEntityTests +{ + [Fact] + public void Add_Notification_Success() + { + var sender = new AthleteEntity + { + Username = "sender_user", + LastName = "Sender", + FirstName = "User", + Email = "sender.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "senderpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var notification = new NotificationEntity + { + Message = "Test notification", + Date = DateTime.Now, + Statut = false, + Urgence = "High", + Sender = sender + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(sender); + context.Notifications.Add(notification); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedNotification = context.Notifications.First(); + Assert.NotNull(savedNotification); + Assert.Equal("Test notification", savedNotification.Message); + }*/ + } + + [Fact] + public void Update_Notification_Success() + { + var sender = new AthleteEntity + { + Username = "sender_user", + LastName = "Sender", + FirstName = "User", + Email = "sender.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "senderpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var notification = new NotificationEntity + { + Message = "Test notification", + Date = DateTime.Now, + Statut = false, + Urgence = "High", + Sender = sender + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(sender); + context.Notifications.Add(notification); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedNotification = context.Notifications.First(); + savedNotification.Message = "Updated message"; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedNotification = context.Notifications.First(); + Assert.NotNull(updatedNotification); + Assert.Equal("Updated message", updatedNotification.Message); + }*/ + } + + [Fact] + public void Delete_Notification_Success() + { + var sender = new AthleteEntity + { + Username = "sender_user", + LastName = "Sender", + FirstName = "User", + Email = "sender.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "senderpassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var notification = new NotificationEntity + { + Message = "Test notification", + Date = DateTime.Now, + Statut = false, + Urgence = "High", + Sender = sender + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(sender); + context.Notifications.Add(notification); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedNotification = context.Notifications.First(); + context.Notifications.Remove(savedNotification); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedNotification = context.Notifications.FirstOrDefault(); + Assert.Null(deletedNotification); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs new file mode 100644 index 0000000..c91b2ab --- /dev/null +++ b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs @@ -0,0 +1,155 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + + +public class StatisticEntityTests +{ + [Fact] + public void Add_Statistic_Success() + { + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var statistic = new StatisticEntity + { + Weight = 75.0f, + AverageHeartRate = 150.0, + MaximumHeartRate = 180.0, + AverageCaloriesBurned = 500.0, + Date = new DateOnly(2024, 3, 15), + Athlete = athlete + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.Statistics.Add(statistic); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedStatistic = context.Statistics.First(); + Assert.NotNull(savedStatistic); + Assert.Equal(75.0f, savedStatistic.Weight); + }*/ + } + + [Fact] + public void Update_Statistic_Success() + { + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var statistic = new StatisticEntity + { + Weight = 75.0f, + AverageHeartRate = 150.0, + MaximumHeartRate = 180.0, + AverageCaloriesBurned = 500.0, + Date = new DateOnly(2024, 3, 15), + Athlete = athlete + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.Statistics.Add(statistic); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedStatistic = context.Statistics.First(); + savedStatistic.Weight = 80.0f; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedStatistic = context.Statistics.First(); + Assert.NotNull(updatedStatistic); + Assert.Equal(80.0f, updatedStatistic.Weight); + }*/ + } + + [Fact] + public void Delete_Statistic_Success() + { + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "M", + Length = 170.0, + Weight = 70.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 1, 1), + IsCoach = false + }; + + var statistic = new StatisticEntity + { + Weight = 75.0f, + AverageHeartRate = 150.0, + MaximumHeartRate = 180.0, + AverageCaloriesBurned = 500.0, + Date = new DateOnly(2024, 3, 15), + Athlete = athlete + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(athlete); + context.Statistics.Add(statistic); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedStatistic = context.Statistics.First(); + context.Statistics.Remove(savedStatistic); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedStatistic = context.Statistics.FirstOrDefault(); + Assert.Null(deletedStatistic); + }*/ + } +} + diff --git a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs new file mode 100644 index 0000000..84ec46e --- /dev/null +++ b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs @@ -0,0 +1,202 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; + + +public class TrainingEntityTests +{ + [Fact] + public void Add_Training_Success() + { + var coach = new AthleteEntity + { + Username = "coach_user", + LastName = "Coach", + FirstName = "User", + Email = "coach.user@example.com", + Sexe = "M", + Length = 180.0, + Weight = 75.0f, + Password = "coachpassword", + DateOfBirth = new DateOnly(1985, 5, 15), + IsCoach = true + }; + + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 3, 20), + IsCoach = false + }; + + var training = new TrainingEntity + { + Date = new DateOnly(2024, 3, 15), + Description = "Training description", + Latitude = 40.12345f, + Longitude = -74.56789f, + FeedBack = "Training feedback", + Coach = coach, + Athletes = { athlete } + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(coach); + context.Athletes.Add(athlete); + context.Trainings.Add(training); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedTraining = context.Trainings.First(); + Assert.NotNull(savedTraining); + Assert.Equal("Training description", savedTraining.Description); + }*/ + } + + [Fact] + public void Update_Training_Success() + { + var coach = new AthleteEntity + { + Username = "coach_user", + LastName = "Coach", + FirstName = "User", + Email = "coach.user@example.com", + Sexe = "M", + Length = 180.0, + Weight = 75.0f, + Password = "coachpassword", + DateOfBirth = new DateOnly(1985, 5, 15), + IsCoach = true + }; + + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 3, 20), + IsCoach = false + }; + + var training = new TrainingEntity + { + Date = new DateOnly(2024, 3, 15), + Description = "Training description", + Latitude = 40.12345f, + Longitude = -74.56789f, + FeedBack = "Training feedback", + Coach = coach, + Athletes = { athlete } + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(coach); + context.Athletes.Add(athlete); + context.Trainings.Add(training); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedTraining = context.Trainings.First(); + savedTraining.Description = "Updated training description"; + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var updatedTraining = context.Trainings.First(); + Assert.NotNull(updatedTraining); + Assert.Equal("Updated training description", updatedTraining.Description); + }*/ + } + + [Fact] + public void Delete_Training_Success() + { + var coach = new AthleteEntity + { + Username = "coach_user", + LastName = "Coach", + FirstName = "User", + Email = "coach.user@example.com", + Sexe = "M", + Length = 180.0, + Weight = 75.0f, + Password = "coachpassword", + DateOfBirth = new DateOnly(1985, 5, 15), + IsCoach = true + }; + + var athlete = new AthleteEntity + { + Username = "athlete_user", + LastName = "Athlete", + FirstName = "User", + Email = "athlete.user@example.com", + Sexe = "F", + Length = 165.0, + Weight = 60.0f, + Password = "athletepassword", + DateOfBirth = new DateOnly(1990, 3, 20), + IsCoach = false + }; + + var training = new TrainingEntity + { + Date = new DateOnly(2024, 3, 15), + Description = "Training description", + Latitude = 40.12345f, + Longitude = -74.56789f, + FeedBack = "Training feedback", + Coach = coach, + Athletes = { athlete } + }; + + /* + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + context.Athletes.Add(coach); + context.Athletes.Add(athlete); + context.Trainings.Add(training); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var savedTraining = context.Trainings.First(); + context.Trainings.Remove(savedTraining); + context.SaveChanges(); + } + + using (var context = new StubbedContext(options)) + { + var deletedTraining = context.Trainings.FirstOrDefault(); + Assert.Null(deletedTraining); + }*/ + } +} From 681ab418003aa003a3a216c2060183ac92ab8834 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 15:15:00 +0100 Subject: [PATCH 5/8] :sparkles: Migrations, :rotating_light: Tests avec bdd --- src/HeartTrackAPI/HeartTrackAPI.csproj | 4 --- src/Model2Entities/Model2Entities.csproj | 7 ----- .../StubbedContextLib.csproj | 8 ------ .../ConsoleTestEFMapper.csproj | 7 ----- .../ConsoleTestRelationships.csproj | 7 ----- .../UnitTestsEntities/ActivityEntityTests.cs | 10 +++---- .../UnitTestsEntities/DatabaseFixture.cs | 26 +++++++++++++++++++ .../UnitTestsEntities.csproj | 13 +++++++--- 8 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 src/Tests/UnitTestsEntities/DatabaseFixture.cs diff --git a/src/HeartTrackAPI/HeartTrackAPI.csproj b/src/HeartTrackAPI/HeartTrackAPI.csproj index d8fac4c..e815c27 100644 --- a/src/HeartTrackAPI/HeartTrackAPI.csproj +++ b/src/HeartTrackAPI/HeartTrackAPI.csproj @@ -14,10 +14,6 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/src/Model2Entities/Model2Entities.csproj b/src/Model2Entities/Model2Entities.csproj index d298015..d1f7751 100644 --- a/src/Model2Entities/Model2Entities.csproj +++ b/src/Model2Entities/Model2Entities.csproj @@ -13,11 +13,4 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - diff --git a/src/StubbedContextLib/StubbedContextLib.csproj b/src/StubbedContextLib/StubbedContextLib.csproj index 0c5f479..1a5589f 100644 --- a/src/StubbedContextLib/StubbedContextLib.csproj +++ b/src/StubbedContextLib/StubbedContextLib.csproj @@ -7,14 +7,6 @@ - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - diff --git a/src/Tests/ConsoleTestEFMapper/ConsoleTestEFMapper.csproj b/src/Tests/ConsoleTestEFMapper/ConsoleTestEFMapper.csproj index 1832a20..7c3fdf4 100644 --- a/src/Tests/ConsoleTestEFMapper/ConsoleTestEFMapper.csproj +++ b/src/Tests/ConsoleTestEFMapper/ConsoleTestEFMapper.csproj @@ -6,13 +6,6 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - Exe net8.0 diff --git a/src/Tests/ConsoleTestRelationships/ConsoleTestRelationships.csproj b/src/Tests/ConsoleTestRelationships/ConsoleTestRelationships.csproj index 02f9db5..10252ee 100644 --- a/src/Tests/ConsoleTestRelationships/ConsoleTestRelationships.csproj +++ b/src/Tests/ConsoleTestRelationships/ConsoleTestRelationships.csproj @@ -5,13 +5,6 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - Exe net8.0 diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs index 62caaae..eadb0dd 100644 --- a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -3,13 +3,14 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; +using Microsoft.Data.Sqlite; -public class ActivityEntityTests +public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Activity_Success() { - var activity = new ActivityEntity { @@ -29,16 +30,15 @@ public class ActivityEntityTests DataSourceId = 1, AthleteId = 1 }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); context.Activities.Add(activity); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(options)) { var savedActivity = context.Activities.First(a => a.Type == "Running"); Assert.NotNull(savedActivity); diff --git a/src/Tests/UnitTestsEntities/DatabaseFixture.cs b/src/Tests/UnitTestsEntities/DatabaseFixture.cs new file mode 100644 index 0000000..642d746 --- /dev/null +++ b/src/Tests/UnitTestsEntities/DatabaseFixture.cs @@ -0,0 +1,26 @@ +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +namespace UnitTestsEntities; + +public class DatabaseFixture : IDisposable +{ + private readonly SqliteConnection _connection; + public readonly DbContextOptions _options; + public DatabaseFixture() + { + _connection = new SqliteConnection("DataSource=:memory:"); + _connection.Open(); + + _options = new DbContextOptionsBuilder() + .UseSqlite(_connection) + .Options; + + } + public void Dispose() + { + _connection.Close(); + _connection.Dispose(); + } +} \ No newline at end of file diff --git a/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj b/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj index e372137..80251f0 100644 --- a/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj +++ b/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj @@ -1,16 +1,20 @@ - + net8.0 enable enable - - false + $(MSBuildProjectDirectory) + false true + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -24,7 +28,10 @@ + + + From bbb66c605918e90625f378e66241ffcb578c7ddc Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 19:28:04 +0100 Subject: [PATCH 6/8] :ambulance::boom: Tests in memory Entities 14/24 :white_check_mark: --- .../UnitTestsEntities/ActivityEntityTests.cs | 44 +++++++------- .../UnitTestsEntities/AthleteEntityTests.cs | 47 ++++++++------- .../DataSourceEntityTests.cs | 47 +++++++-------- .../UnitTestsEntities/DatabaseFixture.cs | 7 ++- .../FriendshipEntityTests.cs | 54 ++++++++--------- .../UnitTestsEntities/HeartRateEntityTests.cs | 53 ++++++++--------- .../NotificationEntityTests.cs | 52 ++++++++-------- .../UnitTestsEntities/StatisticEntityTests.cs | 53 ++++++++--------- .../UnitTestsEntities/TrainingEntityTests.cs | 59 +++++++++---------- 9 files changed, 200 insertions(+), 216 deletions(-) diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs index eadb0dd..de6cedf 100644 --- a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -30,21 +30,20 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running"); + var savedActivity = context.ActivitiesSet.First(a => a.Type == "Running"); Assert.NotNull(savedActivity); Assert.Equal("Running", savedActivity.Type ); - - }*/ + } } [Fact] @@ -69,28 +68,27 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running"); + var savedActivity = context.ActivitiesSet.First(a => a.Type == "Running"); savedActivity.Type = "Walking"; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedActivity = context.Activities.First(a => a.Type == "Walking"); + var updatedActivity = context.ActivitiesSet.First(a => a.Type == "Walking"); Assert.NotNull(updatedActivity); Assert.Equal("Walking", updatedActivity.Type ); Assert.Equal(7, updatedActivity.EffortFelt ); - }*/ + } } [Fact] @@ -115,26 +113,26 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running"); - context.Activities.Remove(savedActivity); + var savedActivity = context.ActivitiesSet.First(a => a.Type == "Running"); + context.ActivitiesSet.Remove(savedActivity); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedActivity = context.Activities.FirstOrDefault(a => a.Type == "Running"); + var deletedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Running"); Assert.Null(deletedActivity); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs index 878d7d8..10193dc 100644 --- a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs +++ b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs @@ -3,8 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; -public class AthleteEntityTests +public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Athlete_Success() @@ -23,20 +24,20 @@ public class AthleteEntityTests IsCoach = false }; - /* - using (var context = new StubbedContext(options)) + + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); + context.AthletesSet.Add(athlete); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedAthlete = context.Athletes.First(a => a.Username == "john_doe"); + var savedAthlete = context.AthletesSet.First(a => a.Username == "john_doe"); Assert.NotNull(savedAthlete); Assert.Equal("john_doe", savedAthlete.Username); - }*/ + } } [Fact] @@ -56,28 +57,27 @@ public class AthleteEntityTests IsCoach = false }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); + context.AthletesSet.Add(athlete); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedAthlete = context.Athletes.First(a => a.Username == "jane_smith"); + var savedAthlete = context.AthletesSet.First(a => a.Username == "jane_smith"); savedAthlete.Username = "jane_doe"; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedAthlete = context.Athletes.First(a => a.Username == "jane_doe"); + var updatedAthlete = context.AthletesSet.First(a => a.Username == "jane_doe"); Assert.NotNull(updatedAthlete); Assert.Equal("jane_doe", updatedAthlete.Username); Assert.Equal("Smith", updatedAthlete.LastName); - }*/ + } } [Fact] @@ -97,26 +97,25 @@ public class AthleteEntityTests IsCoach = false }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); + context.AthletesSet.Add(athlete); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedAthlete = context.Athletes.First(a => a.Username == "test_user"); - context.Athletes.Remove(savedAthlete); + var savedAthlete = context.AthletesSet.First(a => a.Username == "test_user"); + context.AthletesSet.Remove(savedAthlete); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedAthlete = context.Athletes.FirstOrDefault(a => a.Username == "test_user"); + var deletedAthlete = context.AthletesSet.FirstOrDefault(a => a.Username == "test_user"); Assert.Null(deletedAthlete); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs index 6f4c1db..83c7bc1 100644 --- a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs +++ b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs @@ -3,9 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; - -public class DataSourceEntityTests +public class DataSourceEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_DataSource_Success() @@ -17,20 +17,19 @@ public class DataSourceEntityTests Precision = 0.1f }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.DataSources.Add(dataSource); + context.DataSourcesSet.Add(dataSource); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedDataSource = context.DataSources.First(d => d.Type == "GPS"); + var savedDataSource = context.DataSourcesSet.First(d => d.Type == "GPS"); Assert.NotNull(savedDataSource); Assert.Equal("Garmin Forerunner 945", savedDataSource.Model); - }*/ + } } [Fact] @@ -43,28 +42,27 @@ public class DataSourceEntityTests Precision = 0.2f }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.DataSources.Add(dataSource); + context.DataSourcesSet.Add(dataSource); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedDataSource = context.DataSources.First(d => d.Type == "Heart Rate Monitor"); + var savedDataSource = context.DataSourcesSet.First(d => d.Type == "Heart Rate Monitor"); savedDataSource.Model = "Polar H9"; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedDataSource = context.DataSources.First(d => d.Model == "Polar H9"); + var updatedDataSource = context.DataSourcesSet.First(d => d.Model == "Polar H9"); Assert.NotNull(updatedDataSource); Assert.Equal("Heart Rate Monitor", updatedDataSource.Type); Assert.Equal(0.2f, updatedDataSource.Precision); - }*/ + } } [Fact] @@ -77,26 +75,25 @@ public class DataSourceEntityTests Precision = 0.05f }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.DataSources.Add(dataSource); + context.DataSourcesSet.Add(dataSource); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedDataSource = context.DataSources.First(d => d.Type == "Smartwatch"); - context.DataSources.Remove(savedDataSource); + var savedDataSource = context.DataSourcesSet.First(d => d.Type == "Smartwatch"); + context.DataSourcesSet.Remove(savedDataSource); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedDataSource = context.DataSources.FirstOrDefault(d => d.Type == "Smartwatch"); + var deletedDataSource = context.DataSourcesSet.FirstOrDefault(d => d.Type == "Smartwatch"); Assert.Null(deletedDataSource); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/DatabaseFixture.cs b/src/Tests/UnitTestsEntities/DatabaseFixture.cs index 642d746..eaae26d 100644 --- a/src/Tests/UnitTestsEntities/DatabaseFixture.cs +++ b/src/Tests/UnitTestsEntities/DatabaseFixture.cs @@ -1,4 +1,5 @@ -using Microsoft.Data.Sqlite; +using DbContextLib; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using StubbedContextLib; @@ -7,13 +8,13 @@ namespace UnitTestsEntities; public class DatabaseFixture : IDisposable { private readonly SqliteConnection _connection; - public readonly DbContextOptions _options; + public readonly DbContextOptions _options; public DatabaseFixture() { _connection = new SqliteConnection("DataSource=:memory:"); _connection.Open(); - _options = new DbContextOptionsBuilder() + _options = new DbContextOptionsBuilder() .UseSqlite(_connection) .Options; diff --git a/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs index 492506c..3e5d472 100644 --- a/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs +++ b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs @@ -3,8 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; -public class FriendshipEntityTests +public class FriendshipEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Friendship_Success() @@ -42,19 +43,18 @@ public class FriendshipEntityTests Follower = follower, Following = following, StartDate = DateTime.Now - }; + };/* - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(follower); - context.Athletes.Add(following); + context.AthletesSet.Add(follower); + context.AthletesSet.Add(following); context.Friendships.Add(friendship); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { var savedFriendship = context.Friendships.FirstOrDefault(); Assert.NotNull(savedFriendship); @@ -113,27 +113,26 @@ public class FriendshipEntityTests Follower = follower, Following = following, StartDate = DateTime.Now - }; + };/* - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(follower); - context.Athletes.Add(following); - context.Athletes.Add(thirdAthlete); + context.AthletesSet.Add(follower); + context.AthletesSet.Add(following); + context.AthletesSet.Add(thirdAthlete); context.Friendships.Add(friendship); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { var savedFriendship = context.Friendships.FirstOrDefault(); savedFriendship.Follower = thirdAthlete; // Update the follower context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { var updatedFriendship = context.Friendships.FirstOrDefault(); Assert.NotNull(updatedFriendship); @@ -144,6 +143,7 @@ public class FriendshipEntityTests [Fact] public void Delete_Friendship_Success() { + // Act var follower = new AthleteEntity { Username = "follower_user", @@ -172,31 +172,29 @@ public class FriendshipEntityTests IsCoach = false }; - var friendship = new FriendshipEntity - { - Follower = follower, - Following = following, - StartDate = DateTime.Now - }; - + /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(follower); - context.Athletes.Add(following); - context.Friendships.Add(friendship); + context.AthletesSet.Add(follower); + context.AthletesSet.Add(following); context.SaveChanges(); + var user1 = context.AthletesSet.FirstOrDefault(a => a.IdAthlete == follower.IdAthlete); + var user2 = context.AthletesSet.FirstOrDefault(a => a.IdAthlete == following.IdAthlete); + user1.Followers = user2.IdAthlete; } + + - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { var savedFriendship = context.Friendships.FirstOrDefault(); context.Friendships.Remove(savedFriendship); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { var deletedFriendship = context.Friendships.FirstOrDefault(); Assert.Null(deletedFriendship); diff --git a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs index c3bc36f..0755f02 100644 --- a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs +++ b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs @@ -3,8 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; -public class HeartRateEntityTests +public class HeartRateEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_HeartRate_Success() @@ -39,21 +40,21 @@ public class HeartRateEntityTests Activity = activity }; - /* - using (var context = new StubbedContext(options)) + + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Activities.Add(activity); - context.HeartRates.Add(heartRateEntry); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedHeartRate = context.HeartRates.First(); + var savedHeartRate = context.HeartRatesSet.First(); Assert.NotNull(savedHeartRate); Assert.Equal(150, savedHeartRate.Bpm); - }*/ + } } [Fact] @@ -89,28 +90,27 @@ public class HeartRateEntityTests Activity = activity }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Activities.Add(activity); - context.HeartRates.Add(heartRateEntry); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedHeartRate = context.HeartRates.First(); + var savedHeartRate = context.HeartRatesSet.First(); savedHeartRate.Bpm = 160; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedHeartRate = context.HeartRates.First(); + var updatedHeartRate = context.HeartRatesSet.First(); Assert.NotNull(updatedHeartRate); Assert.Equal(160, updatedHeartRate.Bpm); - }*/ + } } [Fact] @@ -146,27 +146,26 @@ public class HeartRateEntityTests Activity = activity }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Activities.Add(activity); - context.HeartRates.Add(heartRateEntry); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedHeartRate = context.HeartRates.First(); - context.HeartRates.Remove(savedHeartRate); + var savedHeartRate = context.HeartRatesSet.First(); + context.HeartRatesSet.Remove(savedHeartRate); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedHeartRate = context.HeartRates.FirstOrDefault(); + var deletedHeartRate = context.HeartRatesSet.FirstOrDefault(); Assert.Null(deletedHeartRate); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs index 6d656a4..7a1504e 100644 --- a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs +++ b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs @@ -3,8 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; -public class NotificationEntityTests +public class NotificationEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Notification_Success() @@ -32,21 +33,20 @@ public class NotificationEntityTests Sender = sender }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(sender); - context.Notifications.Add(notification); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedNotification = context.Notifications.First(); + var savedNotification = context.NotificationsSet.First(); Assert.NotNull(savedNotification); Assert.Equal("Test notification", savedNotification.Message); - }*/ + } } [Fact] @@ -75,28 +75,27 @@ public class NotificationEntityTests Sender = sender }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(sender); - context.Notifications.Add(notification); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedNotification = context.Notifications.First(); + var savedNotification = context.NotificationsSet.First(); savedNotification.Message = "Updated message"; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedNotification = context.Notifications.First(); + var updatedNotification = context.NotificationsSet.First(); Assert.NotNull(updatedNotification); Assert.Equal("Updated message", updatedNotification.Message); - }*/ + } } [Fact] @@ -125,27 +124,26 @@ public class NotificationEntityTests Sender = sender }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(sender); - context.Notifications.Add(notification); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedNotification = context.Notifications.First(); - context.Notifications.Remove(savedNotification); + var savedNotification = context.NotificationsSet.First(); + context.NotificationsSet.Remove(savedNotification); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedNotification = context.Notifications.FirstOrDefault(); + var deletedNotification = context.NotificationsSet.FirstOrDefault(); Assert.Null(deletedNotification); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs index c91b2ab..61d2e18 100644 --- a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs +++ b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs @@ -3,9 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; - -public class StatisticEntityTests +public class StatisticEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Statistic_Success() @@ -34,21 +34,20 @@ public class StatisticEntityTests Athlete = athlete }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); - context.Statistics.Add(statistic); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedStatistic = context.Statistics.First(); + var savedStatistic = context.StatisticsSet.FirstOrDefault(s => s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); Assert.NotNull(savedStatistic); Assert.Equal(75.0f, savedStatistic.Weight); - }*/ + } } [Fact] @@ -78,28 +77,27 @@ public class StatisticEntityTests Athlete = athlete }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); - context.Statistics.Add(statistic); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedStatistic = context.Statistics.First(); + var savedStatistic = context.StatisticsSet.First(); savedStatistic.Weight = 80.0f; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedStatistic = context.Statistics.First(); + var updatedStatistic = context.StatisticsSet.First(); Assert.NotNull(updatedStatistic); Assert.Equal(80.0f, updatedStatistic.Weight); - }*/ + } } [Fact] @@ -129,27 +127,26 @@ public class StatisticEntityTests Athlete = athlete }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(athlete); - context.Statistics.Add(statistic); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedStatistic = context.Statistics.First(); - context.Statistics.Remove(savedStatistic); + var savedStatistic = context.StatisticsSet.FirstOrDefault(s => s.Weight == 75.0 && s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); + context.StatisticsSet.Remove(savedStatistic); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedStatistic = context.Statistics.FirstOrDefault(); + var deletedStatistic = context.StatisticsSet.FirstOrDefault(s => s.Weight == 75.0 && s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); Assert.Null(deletedStatistic); - }*/ + } } } diff --git a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs index 84ec46e..cd499f8 100644 --- a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs +++ b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs @@ -3,9 +3,9 @@ using Xunit; using System.Linq; using Entities; using Microsoft.EntityFrameworkCore; +using StubbedContextLib; - -public class TrainingEntityTests +public class TrainingEntityTests (DatabaseFixture fixture) : IClassFixture { [Fact] public void Add_Training_Success() @@ -49,22 +49,21 @@ public class TrainingEntityTests Athletes = { athlete } }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(coach); - context.Athletes.Add(athlete); - context.Trainings.Add(training); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedTraining = context.Trainings.First(); + var savedTraining = context.TrainingsSet.First(); Assert.NotNull(savedTraining); Assert.Equal("Training description", savedTraining.Description); - }*/ + } } [Fact] @@ -109,29 +108,28 @@ public class TrainingEntityTests Athletes = { athlete } }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(coach); - context.Athletes.Add(athlete); - context.Trainings.Add(training); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedTraining = context.Trainings.First(); + var savedTraining = context.TrainingsSet.First(); savedTraining.Description = "Updated training description"; context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedTraining = context.Trainings.First(); + var updatedTraining = context.TrainingsSet.First(); Assert.NotNull(updatedTraining); Assert.Equal("Updated training description", updatedTraining.Description); - }*/ + } } [Fact] @@ -176,27 +174,26 @@ public class TrainingEntityTests Athletes = { athlete } }; - /* - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { context.Database.EnsureCreated(); - context.Athletes.Add(coach); - context.Athletes.Add(athlete); - context.Trainings.Add(training); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var savedTraining = context.Trainings.First(); - context.Trainings.Remove(savedTraining); + var savedTraining = context.TrainingsSet.First(); + context.TrainingsSet.Remove(savedTraining); context.SaveChanges(); } - using (var context = new StubbedContext(options)) + using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedTraining = context.Trainings.FirstOrDefault(); + var deletedTraining = context.TrainingsSet.FirstOrDefault(); Assert.Null(deletedTraining); - }*/ + } } } From 84ffc5c8fa0b7d9ec8cbc3f1e98987fc172c06e6 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 23:53:37 +0100 Subject: [PATCH 7/8] :rotating_light: Correction tests exitants, 20/24 :white_check_mark: manque Friendship et un Delete dans HeartRateEntityTests qui ne passent pas ... --- .../UnitTestsEntities/ActivityEntityTests.cs | 8 +++---- .../DataSourceEntityTests.cs | 4 ++-- .../FriendshipEntityTests.cs | 24 ++++++++++--------- .../UnitTestsEntities/HeartRateEntityTests.cs | 14 ++++++----- .../NotificationEntityTests.cs | 8 +++---- .../UnitTestsEntities/StatisticEntityTests.cs | 6 ++--- .../UnitTestsEntities/TrainingEntityTests.cs | 14 +++++------ 7 files changed, 41 insertions(+), 37 deletions(-) diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs index de6cedf..ed9c7f0 100644 --- a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -77,14 +77,14 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running"); + var savedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Running" && a.Maximum == 200); savedActivity.Type = "Walking"; context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedActivity = context.ActivitiesSet.First(a => a.Type == "Walking"); + var updatedActivity = context.ActivitiesSet.First(a => a.Type == "Walking" && a.Maximum == 200); Assert.NotNull(updatedActivity); Assert.Equal("Walking", updatedActivity.Type ); Assert.Equal(7, updatedActivity.EffortFelt ); @@ -123,14 +123,14 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running"); + var savedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Running" && a.EffortFelt == 7); context.ActivitiesSet.Remove(savedActivity); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Running"); + var deletedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Running" && a.EffortFelt == 7); Assert.Null(deletedActivity); } } diff --git a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs index 83c7bc1..1e0d1e0 100644 --- a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs +++ b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs @@ -84,14 +84,14 @@ public class DataSourceEntityTests (DatabaseFixture fixture) : IClassFixture d.Type == "Smartwatch"); + var savedDataSource = context.DataSourcesSet.First(d => d.Type == "Smartwatch" && d.Model == "Apple Watch Series 6"); context.DataSourcesSet.Remove(savedDataSource); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedDataSource = context.DataSourcesSet.FirstOrDefault(d => d.Type == "Smartwatch"); + var deletedDataSource = context.DataSourcesSet.FirstOrDefault(d => d.Type == "Smartwatch" && d.Model == "Apple Watch Series 6"); Assert.Null(deletedDataSource); } } diff --git a/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs index 3e5d472..8e2b815 100644 --- a/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs +++ b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs @@ -12,7 +12,7 @@ public class FriendshipEntityTests (DatabaseFixture fixture) : IClassFixture)follower }; var friendship = new FriendshipEntity @@ -43,24 +44,25 @@ public class FriendshipEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "following_user1"); + follow.Followings = (ICollection)following; context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var savedFriendship = context.Friendships.FirstOrDefault(); - Assert.NotNull(savedFriendship); - Assert.Equal(follower.IdAthlete, savedFriendship.FollowerId); - Assert.Equal(following.IdAthlete, savedFriendship.FollowingId); - }*/ + var savedAth1 = context.AthletesSet.FirstOrDefault(a => a.Username == "following_user1"); + var savedAth2 = context.AthletesSet.FirstOrDefault(a => a.Username == "follower_user1"); + Assert.Equal(savedAth2, friendship.Follower); + Assert.Equal(savedAth1, friendship.Following); + } } [Fact] diff --git a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs index 0755f02..377d960 100644 --- a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs +++ b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs @@ -51,7 +51,7 @@ public class HeartRateEntityTests (DatabaseFixture fixture) : IClassFixture h.Altitude == 100.0 && h.Temperature == 20.0f); Assert.NotNull(savedHeartRate); Assert.Equal(150, savedHeartRate.Bpm); } @@ -118,7 +118,7 @@ public class HeartRateEntityTests (DatabaseFixture fixture) : IClassFixture h.Altitude == 105.0); + var savedActivity = context.ActivitiesSet.FirstOrDefault(a => a.Type == "Run" && a.EffortFelt == 7 && a.Average == 0.4f); context.HeartRatesSet.Remove(savedHeartRate); + context.ActivitiesSet.Remove(savedActivity); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedHeartRate = context.HeartRatesSet.FirstOrDefault(); + var deletedHeartRate = context.HeartRatesSet.FirstOrDefault(h => h.Altitude == 105.0); Assert.Null(deletedHeartRate); } } diff --git a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs index 7a1504e..1fbc3a6 100644 --- a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs +++ b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs @@ -43,7 +43,7 @@ public class NotificationEntityTests (DatabaseFixture fixture) : IClassFixture n.Message == "Test notification" && n.Statut == false && n.Urgence == "High"); Assert.NotNull(savedNotification); Assert.Equal("Test notification", savedNotification.Message); } @@ -117,7 +117,7 @@ public class NotificationEntityTests (DatabaseFixture fixture) : IClassFixture n.Message == "Test notification Suppression"); context.NotificationsSet.Remove(savedNotification); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedNotification = context.NotificationsSet.FirstOrDefault(); + var deletedNotification = context.NotificationsSet.FirstOrDefault(n => n.Message == "Test notification Suppression"); Assert.Null(deletedNotification); } } diff --git a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs index 61d2e18..d466d34 100644 --- a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs +++ b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs @@ -119,7 +119,7 @@ public class StatisticEntityTests (DatabaseFixture fixture) : IClassFixture s.Weight == 75.0 && s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); + var savedStatistic = context.StatisticsSet.FirstOrDefault(s => s.Weight == 85.0 ); context.StatisticsSet.Remove(savedStatistic); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedStatistic = context.StatisticsSet.FirstOrDefault(s => s.Weight == 75.0 && s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); + var deletedStatistic = context.StatisticsSet.FirstOrDefault(s => s.Weight == 85.0 ); Assert.Null(deletedStatistic); } } diff --git a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs index cd499f8..7503bed 100644 --- a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs +++ b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs @@ -41,7 +41,7 @@ public class TrainingEntityTests (DatabaseFixture fixture) : IClassFixture t.Description == "Training Description" && t.FeedBack == "Training feedback"); Assert.NotNull(savedTraining); - Assert.Equal("Training description", savedTraining.Description); + Assert.Equal("Training Description", savedTraining.Description); } } @@ -166,10 +166,10 @@ public class TrainingEntityTests (DatabaseFixture fixture) : IClassFixture t.Description == "Training description suppression" && t.FeedBack == "Training feedback suppression"); context.TrainingsSet.Remove(savedTraining); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedTraining = context.TrainingsSet.FirstOrDefault(); + var deletedTraining = context.TrainingsSet.FirstOrDefault(t => t.Description == "Training description suppression" && t.FeedBack == "Training feedback suppression"); Assert.Null(deletedTraining); } } From 5de63bcd14fc46be29c6a51d9100fced03cd308b Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Sat, 16 Mar 2024 00:38:35 +0100 Subject: [PATCH 8/8] =?UTF-8?q?:tada:=20Tests=20fini=20car=20bloqu=C3=A9?= =?UTF-8?q?=20->=2032/36=20:white=5Fcheck=5Fmark:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnitTestsEntities/ActivityEntityTests.cs | 85 ++++++++++++++++++ .../UnitTestsEntities/AthleteEntityTests.cs | 66 ++++++++++++++ .../DataSourceEntityTests.cs | 45 ++++++++++ .../UnitTestsEntities/HeartRateEntityTests.cs | 57 ++++++++++++ .../NotificationEntityTests.cs | 65 ++++++++++++++ .../UnitTestsEntities/StatisticEntityTests.cs | 50 +++++++++++ .../UnitTestsEntities/TrainingEntityTests.cs | 86 +++++++++++++++++++ 7 files changed, 454 insertions(+) diff --git a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs index ed9c7f0..f505bd7 100644 --- a/src/Tests/UnitTestsEntities/ActivityEntityTests.cs +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -134,5 +134,90 @@ public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture a.Type == "Running" && a.Date == new DateOnly(2024, 3, 15)); + Assert.NotNull(savedActivity); + Assert.Equal("Running", savedActivity.Type); + Assert.Equal(new DateOnly(2024, 3, 15), savedActivity.Date); + Assert.Equal(new TimeOnly(9, 0), savedActivity.StartTime); + Assert.Equal(new TimeOnly(10, 0), savedActivity.EndTime); + Assert.Equal(7, savedActivity.EffortFelt); + Assert.Equal(0.5f, savedActivity.Variability); + Assert.Equal(0.2f, savedActivity.Variance); + Assert.Equal(0.3f, savedActivity.StandardDeviation); + Assert.Equal(0.4f, savedActivity.Average); + Assert.Equal(200, savedActivity.Maximum); + Assert.Equal(100, savedActivity.Minimum); + Assert.Equal(25.5f, savedActivity.AverageTemperature); + Assert.True(savedActivity.HasAutoPause); + Assert.Equal(1, savedActivity.DataSourceId); + Assert.Equal(1, savedActivity.AthleteId); + } + } + + // Test for error cases, e.g., null values + [Fact] + public void Add_Activity_With_Null_Values_Fails() + { + var activity = new ActivityEntity + { + Type = null, + Date = default, + StartTime = default, + EndTime = default, + EffortFelt = 0, + Variability = 0, + Variance = 0, + StandardDeviation = 0, + Average = 0, + Maximum = 0, + Minimum = 0, + AverageTemperature = 0, + HasAutoPause = false, + DataSourceId = 0, + AthleteId = 0 + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + Assert.Throws(() => context.SaveChanges()); + } + } } diff --git a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs index 10193dc..701f27b 100644 --- a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs +++ b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs @@ -117,5 +117,71 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "john_doe"); + Assert.NotNull(savedAthlete); + Assert.Equal("john_doe", savedAthlete.Username); + Assert.Equal("Doe", savedAthlete.LastName); + Assert.Equal("John", savedAthlete.FirstName); + Assert.Equal("john.doe@example.com", savedAthlete.Email); + Assert.Equal("M", savedAthlete.Sexe); + Assert.Equal(180.0, savedAthlete.Length); + Assert.Equal(75.5f, savedAthlete.Weight); + Assert.Equal("password", savedAthlete.Password); + Assert.Equal(new DateOnly(1990, 1, 1), savedAthlete.DateOfBirth); + Assert.False(savedAthlete.IsCoach); + } + } + + [Fact] + public void Add_Athlete_With_Null_Values_Fails() + { + var athlete = new AthleteEntity + { + Username = null, + LastName = null, + FirstName = null, + Email = null, + Sexe = null, + Length = 0, + Weight = 0, + Password = null, + DateOfBirth = default, + IsCoach = false + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + Assert.Throws(() => context.SaveChanges()); + } + } } diff --git a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs index 1e0d1e0..925242c 100644 --- a/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs +++ b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs @@ -95,5 +95,50 @@ public class DataSourceEntityTests (DatabaseFixture fixture) : IClassFixture d.Type == "GPS"); + Assert.NotNull(savedDataSource); + Assert.Equal("GPS", savedDataSource.Type); + Assert.Equal("Garmin Forerunner 945", savedDataSource.Model); + Assert.Equal(0.1f, savedDataSource.Precision); + } + } + + [Fact] + public void Add_DataSource_With_Null_Values_Fails() + { + var dataSource = new DataSourceEntity + { + Type = null, + Model = null, + Precision = 0 + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.DataSourcesSet.Add(dataSource); + Assert.Throws(() => context.SaveChanges()); + } + } } diff --git a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs index 377d960..baeabc1 100644 --- a/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs +++ b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs @@ -169,5 +169,62 @@ public class HeartRateEntityTests (DatabaseFixture fixture) : IClassFixture h.Altitude == 100.0 && h.Temperature == 20.0f); + Assert.NotNull(savedHeartRate); + Assert.Equal(100.0, savedHeartRate.Altitude); + Assert.Equal(new TimeOnly(9, 30), savedHeartRate.Time); + Assert.Equal(20.0f, savedHeartRate.Temperature); + Assert.Equal(150, savedHeartRate.Bpm); + Assert.Equal(45.12345f, savedHeartRate.Longitude); + Assert.Equal(35.6789f, savedHeartRate.Latitude); + } + } } diff --git a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs index 1fbc3a6..9858911 100644 --- a/src/Tests/UnitTestsEntities/NotificationEntityTests.cs +++ b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs @@ -145,5 +145,70 @@ public class NotificationEntityTests (DatabaseFixture fixture) : IClassFixture n.Message == "Test notification" && n.Statut == false && n.Urgence == "High"); + Assert.NotNull(savedNotification); + Assert.Equal("Test notification", savedNotification.Message); + Assert.Equal(DateTime.Today, savedNotification.Date.Date); + Assert.False(savedNotification.Statut); + Assert.Equal("High", savedNotification.Urgence); + } + } + + [Fact] + public void Add_Notification_With_Null_Values_Fails() + { + var notification = new NotificationEntity + { + Message = null, + Date = default, + Statut = false, + Urgence = null, + Sender = null + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.NotificationsSet.Add(notification); + Assert.Throws(() => context.SaveChanges()); + } + } } diff --git a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs index d466d34..7c1c878 100644 --- a/src/Tests/UnitTestsEntities/StatisticEntityTests.cs +++ b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs @@ -148,5 +148,55 @@ public class StatisticEntityTests (DatabaseFixture fixture) : IClassFixture s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); + Assert.NotNull(savedStatistic); + Assert.Equal(75.0f, savedStatistic.Weight); + Assert.Equal(150.0, savedStatistic.AverageHeartRate); + Assert.Equal(180.0, savedStatistic.MaximumHeartRate); + Assert.Equal(500.0, savedStatistic.AverageCaloriesBurned); + Assert.Equal(new DateOnly(2024, 3, 15), savedStatistic.Date); + } + } } diff --git a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs index 7503bed..04e8589 100644 --- a/src/Tests/UnitTestsEntities/TrainingEntityTests.cs +++ b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs @@ -196,4 +196,90 @@ public class TrainingEntityTests (DatabaseFixture fixture) : IClassFixture t.Description == "Training DescriptionAll" && t.FeedBack == "Training feedbackAll"); + Assert.NotNull(savedTraining); + Assert.Equal(new DateOnly(2024, 3, 15), savedTraining.Date); + Assert.Equal("Training DescriptionAll", savedTraining.Description); + Assert.Equal(40.12345f, savedTraining.Latitude); + Assert.Equal(-74.56789f, savedTraining.Longitude); + Assert.Equal("Training feedbackAll", savedTraining.FeedBack); + } + } + + [Fact] + public void Add_Training_With_Null_Values_Fails() + { + var training = new TrainingEntity + { + Date = default, + Description = null, + Latitude = 45, + Longitude = 45, + FeedBack = null, + Coach = null, + Athletes = null + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.TrainingsSet.Add(training); + Assert.Throws(() => context.SaveChanges()); + } + } + }