From 205fb3882ae814835c524869ef84b95c134885b7 Mon Sep 17 00:00:00 2001 From: "kevin.monteiro" Date: Fri, 15 Mar 2024 00:53:23 +0100 Subject: [PATCH] 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 + + + + + + + + + + + + + +