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 +``` diff --git a/src/HeartTrack.sln b/src/HeartTrack.sln index 6b62741..8947e4c 100644 --- a/src/HeartTrack.sln +++ b/src/HeartTrack.sln @@ -3,52 +3,49 @@ 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 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 +87,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 +103,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 @@ -135,6 +116,9 @@ Global {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 EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection GlobalSection(NestedProjects) = preSolution {477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {2D166FAD-4934-474B-96A8-6C0635156EC2} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} @@ -144,4 +128,7 @@ Global {31FA8E5E-D642-4C43-A2B2-02B9832B2CEC} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} {73EA27F2-9F0C-443F-A5EE-2960C983A422} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0F3487F4-66CA-4034-AC66-1BC899C9B523} + EndGlobalSection EndGlobal 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 bdd147f..1a5589f 100644 --- a/src/StubbedContextLib/StubbedContextLib.csproj +++ b/src/StubbedContextLib/StubbedContextLib.csproj @@ -7,10 +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 new file mode 100644 index 0000000..f505bd7 --- /dev/null +++ b/src/Tests/UnitTestsEntities/ActivityEntityTests.cs @@ -0,0 +1,223 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; +using Microsoft.Data.Sqlite; + +public class ActivityEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedActivity = context.ActivitiesSet.First(a => a.Type == "Running"); + Assert.NotNull(savedActivity); + Assert.Equal("Running", savedActivity.Type ); + } + } + + [Fact] + public void Update_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 + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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" && a.Maximum == 200); + Assert.NotNull(updatedActivity); + Assert.Equal("Walking", updatedActivity.Type ); + Assert.Equal(7, updatedActivity.EffortFelt ); + } + } + + [Fact] + public void Delete_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 + }; + + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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" && a.EffortFelt == 7); + Assert.Null(deletedActivity); + } + } + + [Fact] + public void Add_Activity_With_All_Properties_Success() + { + // Arrange + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.SaveChanges(); + } + + // Assert + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedActivity = context.ActivitiesSet.First(a => 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 new file mode 100644 index 0000000..701f27b --- /dev/null +++ b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs @@ -0,0 +1,187 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedAthlete = context.AthletesSet.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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedAthlete = context.AthletesSet.First(a => a.Username == "jane_smith"); + savedAthlete.Username = "jane_doe"; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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] + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedAthlete = context.AthletesSet.First(a => a.Username == "test_user"); + context.AthletesSet.Remove(savedAthlete); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var deletedAthlete = context.AthletesSet.FirstOrDefault(a => a.Username == "test_user"); + Assert.Null(deletedAthlete); + } + } + + [Fact] + public void Add_Athlete_With_All_Properties_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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedAthlete = context.AthletesSet.First(a => 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 new file mode 100644 index 0000000..925242c --- /dev/null +++ b/src/Tests/UnitTestsEntities/DataSourceEntityTests.cs @@ -0,0 +1,144 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class DataSourceEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [Fact] + public void Add_DataSource_Success() + { + var dataSource = new DataSourceEntity + { + Type = "GPS", + Model = "Garmin Forerunner 945", + Precision = 0.1f + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.DataSourcesSet.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedDataSource = context.DataSourcesSet.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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.DataSourcesSet.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedDataSource = context.DataSourcesSet.First(d => d.Type == "Heart Rate Monitor"); + savedDataSource.Model = "Polar H9"; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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] + public void Delete_DataSource_Success() + { + var dataSource = new DataSourceEntity + { + Type = "Smartwatch", + Model = "Apple Watch Series 6", + Precision = 0.05f + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.DataSourcesSet.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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" && d.Model == "Apple Watch Series 6"); + Assert.Null(deletedDataSource); + } + } + + [Fact] + public void Add_DataSource_With_All_Properties_Success() + { + var dataSource = new DataSourceEntity + { + Type = "GPS", + Model = "Garmin Forerunner 945", + Precision = 0.1f + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.DataSourcesSet.Add(dataSource); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedDataSource = context.DataSourcesSet.First(d => 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/DatabaseFixture.cs b/src/Tests/UnitTestsEntities/DatabaseFixture.cs new file mode 100644 index 0000000..eaae26d --- /dev/null +++ b/src/Tests/UnitTestsEntities/DatabaseFixture.cs @@ -0,0 +1,27 @@ +using DbContextLib; +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/FriendshipEntityTests.cs b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs new file mode 100644 index 0000000..8e2b815 --- /dev/null +++ b/src/Tests/UnitTestsEntities/FriendshipEntityTests.cs @@ -0,0 +1,206 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class FriendshipEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [Fact] + public void Add_Friendship_Success() + { + var follower = new AthleteEntity + { + Username = "follower_user1", + 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_user1", + 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, + Followers = (ICollection)follower + }; + + var friendship = new FriendshipEntity + { + Follower = follower, + Following = following, + StartDate = DateTime.Now + }; + //Cast impossible entre Entities.AthleteEntity & System.Collections.Generic.ICollection ; Idem pour Update et Delete mais pas modif vu que ca ne fonctionne pas + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(follower); + context.AthletesSet.Add(following); + var follow = context.AthletesSet.FirstOrDefault(a => a.Username == "following_user1"); + follow.Followings = (ICollection)following; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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] + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(follower); + context.AthletesSet.Add(following); + context.AthletesSet.Add(thirdAthlete); + context.Friendships.Add(friendship); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedFriendship = context.Friendships.FirstOrDefault(); + savedFriendship.Follower = thirdAthlete; // Update the follower + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var updatedFriendship = context.Friendships.FirstOrDefault(); + Assert.NotNull(updatedFriendship); + Assert.Equal(thirdAthlete.IdAthlete, updatedFriendship.FollowerId); + }*/ + } + + [Fact] + public void Delete_Friendship_Success() + { + // Act + 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 + }; + + + /* + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + 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 TrainingStubbedContext(fixture._options)) + { + var savedFriendship = context.Friendships.FirstOrDefault(); + context.Friendships.Remove(savedFriendship); + context.SaveChanges(); + } + + 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 new file mode 100644 index 0000000..baeabc1 --- /dev/null +++ b/src/Tests/UnitTestsEntities/HeartRateEntityTests.cs @@ -0,0 +1,230 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class HeartRateEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedHeartRate = context.HeartRatesSet.FirstOrDefault(h => h.Altitude == 100.0 && h.Temperature == 20.0f); + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedHeartRate = context.HeartRatesSet.First(); + savedHeartRate.Bpm = 160; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var updatedHeartRate = context.HeartRatesSet.First(); + Assert.NotNull(updatedHeartRate); + Assert.Equal(160, updatedHeartRate.Bpm); + } + } + + [Fact] + public void Delete_HeartRate_Success() + { + var activity = new ActivityEntity + { + Type = "Run", + 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 = 105.0, + Time = new TimeOnly(9, 30), + Temperature = 20.0f, + Bpm = 150, + Longitude = 45.12345f, + Latitude = 35.6789f, + Activity = activity + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); + context.SaveChanges(); + } + // Ne veut pas passer, si quelqu'un sait pourquoi ... Erreur : System.ArgumentNullException : Value cannot be null. (Parameter 'entity') + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedHeartRate = context.HeartRatesSet.FirstOrDefault(h => 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(h => h.Altitude == 105.0); + Assert.Null(deletedHeartRate); + } + } + + [Fact] + public void Add_HeartRate_With_All_Properties_Success() + { + // Arrange + 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 + }; + + // Act + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.ActivitiesSet.Add(activity); + context.HeartRatesSet.Add(heartRateEntry); + context.SaveChanges(); + } + + // Assert + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedHeartRate = context.HeartRatesSet.FirstOrDefault(h => 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 new file mode 100644 index 0000000..9858911 --- /dev/null +++ b/src/Tests/UnitTestsEntities/NotificationEntityTests.cs @@ -0,0 +1,214 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class NotificationEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedNotification = context.NotificationsSet.FirstOrDefault(n => n.Message == "Test notification" && n.Statut == false && n.Urgence == "High"); + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedNotification = context.NotificationsSet.First(); + savedNotification.Message = "Updated message"; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var updatedNotification = context.NotificationsSet.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 Suppression", + Date = DateTime.Now, + Statut = false, + Urgence = "High", + Sender = sender + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedNotification = context.NotificationsSet.FirstOrDefault(n => n.Message == "Test notification Suppression"); + context.NotificationsSet.Remove(savedNotification); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var deletedNotification = context.NotificationsSet.FirstOrDefault(n => n.Message == "Test notification Suppression"); + Assert.Null(deletedNotification); + } + } + + [Fact] + public void Add_Notification_With_All_Properties_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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(sender); + context.NotificationsSet.Add(notification); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedNotification = context.NotificationsSet.FirstOrDefault(n => 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 new file mode 100644 index 0000000..7c1c878 --- /dev/null +++ b/src/Tests/UnitTestsEntities/StatisticEntityTests.cs @@ -0,0 +1,202 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class StatisticEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedStatistic = context.StatisticsSet.FirstOrDefault(s => s.AverageHeartRate == 150.0 && s.MaximumHeartRate == 180.0); + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedStatistic = context.StatisticsSet.First(); + savedStatistic.Weight = 80.0f; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var updatedStatistic = context.StatisticsSet.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 = 85.0f, + AverageHeartRate = 150.0, + MaximumHeartRate = 180.0, + AverageCaloriesBurned = 500.0, + Date = new DateOnly(2024, 3, 15), + Athlete = athlete + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + 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 == 85.0 ); + Assert.Null(deletedStatistic); + } + } + + [Fact] + public void Add_Statistic_With_All_Properties_Success() + { + // Arrange + 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 + }; + + // Act + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(athlete); + context.StatisticsSet.Add(statistic); + context.SaveChanges(); + } + + // Assert + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedStatistic = context.StatisticsSet.FirstOrDefault(s => 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 new file mode 100644 index 0000000..04e8589 --- /dev/null +++ b/src/Tests/UnitTestsEntities/TrainingEntityTests.cs @@ -0,0 +1,285 @@ +namespace UnitTestsEntities; +using Xunit; +using System.Linq; +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; + +public class TrainingEntityTests (DatabaseFixture fixture) : IClassFixture +{ + [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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedTraining = context.TrainingsSet.FirstOrDefault(t => t.Description == "Training Description" && t.FeedBack == "Training feedback"); + 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 TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedTraining = context.TrainingsSet.First(); + savedTraining.Description = "Updated training description"; + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var updatedTraining = context.TrainingsSet.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 suppression", + Latitude = 40.12345f, + Longitude = -74.56789f, + FeedBack = "Training feedback suppression", + Coach = coach, + Athletes = { athlete } + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedTraining = context.TrainingsSet.FirstOrDefault(t => 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(t => t.Description == "Training description suppression" && t.FeedBack == "Training feedback suppression"); + Assert.Null(deletedTraining); + } + } + + [Fact] + public void Add_Training_With_All_Properties_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 DescriptionAll", + Latitude = 40.12345f, + Longitude = -74.56789f, + FeedBack = "Training feedbackAll", + Coach = coach, + Athletes = { athlete } + }; + + using (var context = new TrainingStubbedContext(fixture._options)) + { + context.Database.EnsureCreated(); + context.AthletesSet.Add(coach); + context.AthletesSet.Add(athlete); + context.TrainingsSet.Add(training); + context.SaveChanges(); + } + + using (var context = new TrainingStubbedContext(fixture._options)) + { + var savedTraining = context.TrainingsSet.FirstOrDefault(t => 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()); + } + } + +} 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..80251f0 100644 --- a/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj +++ b/src/Tests/UnitTestsEntities/UnitTestsEntities.csproj @@ -1,15 +1,20 @@ - + net8.0 enable enable - - false + $(MSBuildProjectDirectory) + false true + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -22,4 +27,11 @@ + + + + + + + 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 + + + + + + + + + + + + + +