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.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 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 = "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(); context.HeartRatesSet.Remove(savedHeartRate); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { var deletedHeartRate = context.HeartRatesSet.FirstOrDefault(); Assert.Null(deletedHeartRate); } } }