-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from pkuehnel/develop
Develop
- Loading branch information
Showing
44 changed files
with
1,043 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
SmartTeslaAmpSetter.Tests/Services/ChargeTimeUpdateService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using SmartTeslaAmpSetter.Shared.Dtos.Settings; | ||
using SmartTeslaAmpSetter.Shared.TimeProviding; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace SmartTeslaAmpSetter.Tests.Services; | ||
|
||
public class ChargeTimeUpdateService : TestBase | ||
{ | ||
public ChargeTimeUpdateService(ITestOutputHelper outputHelper) | ||
: base(outputHelper) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData(3)] | ||
[InlineData(1)] | ||
public void Calculates_Correct_Charge_MaxSpeed_Charge_Time(int numberOfPhases) | ||
{ | ||
var car = new Car() | ||
{ | ||
Id = 1, | ||
CarState = new CarState() | ||
{ | ||
PluggedIn = true, | ||
SoC = 30, | ||
ChargerPhases = numberOfPhases | ||
}, | ||
CarConfiguration = new CarConfiguration() | ||
{ | ||
MinimumSoC = 45, | ||
UsableEnergy = 74, | ||
MaximumAmpere = 16, | ||
} | ||
}; | ||
|
||
|
||
var dateTime = new DateTime(2022, 4, 1, 14, 0, 0); | ||
Mock.Mock<IDateTimeProvider>().Setup(d => d.Now()).Returns(dateTime); | ||
var chargingService = Mock.Create<Server.Services.ChargeTimeUpdateService>(); | ||
|
||
chargingService.UpdateChargeTime(car); | ||
|
||
var lowerMinutes = 60 * (3 / numberOfPhases); | ||
|
||
#pragma warning disable CS8629 | ||
Assert.InRange((DateTime)car.CarState.ReachingMinSocAtFullSpeedCharge, dateTime.AddMinutes(lowerMinutes), dateTime.AddMinutes(lowerMinutes + 1)); | ||
#pragma warning restore CS8629 | ||
} | ||
|
||
[Fact] | ||
public void Handles_Plugged_Out_Car() | ||
{ | ||
var car = new Car() | ||
{ | ||
Id = 1, | ||
CarState = new CarState() | ||
{ | ||
PluggedIn = false, | ||
SoC = 30, | ||
ChargerPhases = 1 | ||
}, | ||
CarConfiguration = new CarConfiguration() | ||
{ | ||
MinimumSoC = 45, | ||
UsableEnergy = 74, | ||
MaximumAmpere = 16, | ||
} | ||
}; | ||
|
||
|
||
var dateTime = new DateTime(2022, 4, 1, 14, 0, 0); | ||
Mock.Mock<IDateTimeProvider>().Setup(d => d.Now()).Returns(dateTime); | ||
var chargingService = Mock.Create<Server.Services.ChargeTimeUpdateService>(); | ||
|
||
chargingService.UpdateChargeTime(car); | ||
|
||
Assert.Null(car.CarState.ReachingMinSocAtFullSpeedCharge); | ||
} | ||
|
||
[Fact] | ||
public void Handles_Reaced_Minimum_Soc() | ||
{ | ||
var car = new Car() | ||
{ | ||
Id = 1, | ||
CarState = new CarState() | ||
{ | ||
PluggedIn = true, | ||
SoC = 30, | ||
ChargerPhases = 1 | ||
}, | ||
CarConfiguration = new CarConfiguration() | ||
{ | ||
MinimumSoC = 30, | ||
UsableEnergy = 74, | ||
MaximumAmpere = 16, | ||
} | ||
}; | ||
|
||
|
||
var dateTime = new DateTime(2022, 4, 1, 14, 0, 0); | ||
Mock.Mock<IDateTimeProvider>().Setup(d => d.Now()).Returns(dateTime); | ||
var chargingService = Mock.Create<Server.Services.ChargeTimeUpdateService>(); | ||
|
||
chargingService.UpdateChargeTime(car); | ||
|
||
Assert.Equal(dateTime, car.CarState.ReachingMinSocAtFullSpeedCharge); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Autofac; | ||
using SmartTeslaAmpSetter.Shared.Dtos.Settings; | ||
using SmartTeslaAmpSetter.Shared.Enums; | ||
using SmartTeslaAmpSetter.Shared.TimeProviding; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace SmartTeslaAmpSetter.Tests.Services; | ||
|
||
public class ChargingService : TestBase | ||
{ | ||
public ChargingService(ITestOutputHelper outputHelper) | ||
: base(outputHelper) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData(ChargeMode.PvAndMinSoc, -32, 10, false)] | ||
[InlineData(ChargeMode.PvAndMinSoc, 2, -10, false)] | ||
[InlineData(ChargeMode.PvOnly, -32, 10, false)] | ||
[InlineData(ChargeMode.PvOnly, 2, -10, false)] | ||
[InlineData(ChargeMode.PvAndMinSoc, -32, 10, true)] | ||
[InlineData(ChargeMode.PvAndMinSoc, 2, -10, true)] | ||
[InlineData(ChargeMode.PvOnly, -32, 10, true)] | ||
[InlineData(ChargeMode.PvOnly, 2, -10, true)] | ||
public void Does_autoenable_fullspeed_charge_if_needed(ChargeMode chargeMode, int fullSpeedChargeMinutesAfterLatestTime, int moreSocThanMinSoc, bool autofullSpeedCharge) | ||
{ | ||
var chargingService = Mock.Create<Server.Services.ChargingService>(); | ||
var currentTimeProvider = Mock.Create<FakeDateTimeProvider>( | ||
new NamedParameter("dateTime", new DateTime(2022, 4, 1, 14, 0, 0))); | ||
var currentTime = currentTimeProvider.Now(); | ||
|
||
var timeSpanToLatestTimeToReachMinSoc = TimeSpan.FromMinutes(60); | ||
var timeSpanToReachMinSoCAtFullSpeedCharge = timeSpanToLatestTimeToReachMinSoc.Add(TimeSpan.FromMinutes(fullSpeedChargeMinutesAfterLatestTime)); | ||
|
||
var minSoc = 50; | ||
|
||
var car = CreateDemoCar(chargeMode, currentTime + timeSpanToLatestTimeToReachMinSoc, minSoc + moreSocThanMinSoc, minSoc, autofullSpeedCharge); | ||
|
||
|
||
car.CarState.ReachingMinSocAtFullSpeedCharge = currentTime + timeSpanToReachMinSoCAtFullSpeedCharge; | ||
|
||
chargingService.EnableFullSpeedChargeIfMinimumSocNotReachable(car); | ||
chargingService.DisableFullSpeedChargeIfMinimumSocReachedOrMinimumSocReachable(car); | ||
|
||
|
||
if (fullSpeedChargeMinutesAfterLatestTime > 0) | ||
{ | ||
Assert.True(car.CarState.AutoFullSpeedCharge); | ||
return; | ||
} | ||
|
||
if (moreSocThanMinSoc >= 0) | ||
{ | ||
Assert.False(car.CarState.AutoFullSpeedCharge); | ||
return; | ||
} | ||
|
||
switch (chargeMode) | ||
{ | ||
case ChargeMode.PvAndMinSoc: | ||
Assert.True(car.CarState.AutoFullSpeedCharge); | ||
break; | ||
|
||
case ChargeMode.PvOnly: | ||
Assert.False(car.CarState.AutoFullSpeedCharge); | ||
break; | ||
|
||
default: | ||
throw new NotImplementedException("This test does not handle this charge mode"); | ||
} | ||
|
||
} | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void Enable_Full_Speed_Charge_Can_Handle_Null_Values(bool autoFullSpeedCharge) | ||
{ | ||
var chargingService = Mock.Create<Server.Services.ChargingService>(); | ||
var currentTimeProvider = Mock.Create<FakeDateTimeProvider>( | ||
new NamedParameter("dateTime", new DateTime(2022, 4, 1, 14, 0, 0))); | ||
var currentTime = currentTimeProvider.Now(); | ||
|
||
var timeSpanToLatestTimeToReachMinSoc = TimeSpan.FromMinutes(60); | ||
|
||
var minSoc = 50; | ||
|
||
var car = CreateDemoCar(ChargeMode.PvAndMinSoc, currentTime + timeSpanToLatestTimeToReachMinSoc, minSoc + 10, minSoc, autoFullSpeedCharge); | ||
|
||
car.CarState.ReachingMinSocAtFullSpeedCharge = null; | ||
|
||
chargingService.EnableFullSpeedChargeIfMinimumSocNotReachable(car); | ||
|
||
Assert.Equal(autoFullSpeedCharge, car.CarState.AutoFullSpeedCharge); | ||
} | ||
|
||
[Theory] | ||
[InlineData(false)] | ||
[InlineData(true)] | ||
public void Disable_Full_Speed_Charge_Can_Handle_Null_Values(bool autoFullSpeedCharge) | ||
{ | ||
var chargingService = Mock.Create<Server.Services.ChargingService>(); | ||
var currentTimeProvider = Mock.Create<FakeDateTimeProvider>( | ||
new NamedParameter("dateTime", new DateTime(2022, 4, 1, 14, 0, 0))); | ||
var currentTime = currentTimeProvider.Now(); | ||
|
||
var timeSpanToLatestTimeToReachMinSoc = TimeSpan.FromMinutes(60); | ||
|
||
var minSoc = 55; | ||
|
||
var car = CreateDemoCar(ChargeMode.PvOnly, currentTime + timeSpanToLatestTimeToReachMinSoc, minSoc - 10, minSoc, autoFullSpeedCharge); | ||
|
||
car.CarState.ReachingMinSocAtFullSpeedCharge = null; | ||
|
||
chargingService.DisableFullSpeedChargeIfMinimumSocReachedOrMinimumSocReachable(car); | ||
|
||
Assert.False(car.CarState.AutoFullSpeedCharge); | ||
} | ||
|
||
[Fact] | ||
public void Gets_relevant_car_IDs() | ||
{ | ||
var geofence = "Home"; | ||
var cars = new List<Car>() | ||
{ | ||
new Car() | ||
{ | ||
Id = 1, | ||
CarState = new CarState() | ||
{ | ||
Geofence = geofence, | ||
PluggedIn = true, | ||
ClimateOn = false, | ||
ChargerActualCurrent = 3, | ||
SoC = 30, | ||
SocLimit = 60, | ||
}, | ||
}, | ||
new Car() | ||
{ | ||
Id = 2, | ||
CarState = new CarState() | ||
{ | ||
Geofence = null, | ||
PluggedIn = true, | ||
ClimateOn = false, | ||
ChargerActualCurrent = 3, | ||
SoC = 30, | ||
SocLimit = 60, | ||
}, | ||
}, | ||
}; | ||
Mock.Mock<ISettings>().Setup(s => s.Cars).Returns(cars); | ||
var chargingService = Mock.Create<Server.Services.ChargingService>(); | ||
|
||
var relevantIds = chargingService.GetRelevantCarIds(geofence); | ||
|
||
Assert.Contains(1, relevantIds); | ||
Assert.Single(relevantIds); | ||
} | ||
|
||
private Car CreateDemoCar(ChargeMode chargeMode, DateTime latestTimeToReachSoC, int soC, int minimumSoC, bool autoFullSpeedCharge) | ||
{ | ||
var car = new Car() | ||
{ | ||
CarState = new CarState() | ||
{ | ||
AutoFullSpeedCharge = autoFullSpeedCharge, | ||
SoC = soC, | ||
}, | ||
CarConfiguration = new CarConfiguration() | ||
{ | ||
LatestTimeToReachSoC = latestTimeToReachSoC, | ||
MinimumSoC = minimumSoC, | ||
ChargeMode = chargeMode, | ||
}, | ||
}; | ||
return car; | ||
} | ||
} |
Oops, something went wrong.