Convert test data sources to typed TheoryData
Replace untyped MemberData sources with TheoryData<...>, use collection expressions for array and list literals, drop redundant casts and null suppressions, and give log-assertion helpers an IsEnabled setup so guarded log calls stay verifiable.
This commit is contained in:
@@ -27,7 +27,7 @@ public class NordPoolEntityServiceTests
|
||||
public void GetInitEntitiesList_ShouldReturnAllEntities()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
var result = NordPoolEntityService.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
@@ -45,7 +45,7 @@ public class NordPoolEntityServiceTests
|
||||
public void GetInitEntitiesList_ShouldHaveCorrectEntityProperties()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
var result = NordPoolEntityService.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
var entity = result.First(e => e.EntityId == "sensor.nordpool_current_price");
|
||||
@@ -58,7 +58,7 @@ public class NordPoolEntityServiceTests
|
||||
public void GetInitEntitiesList_CurrentPriceEntity_ShouldHaveMonetaryDeviceClassConfig()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
var result = NordPoolEntityService.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
var currentPriceEntity = result.First(e => e.EntityId == "sensor.nordpool_current_price");
|
||||
@@ -72,7 +72,7 @@ public class NordPoolEntityServiceTests
|
||||
public void GetInitEntitiesList_ShouldContainExpectedDomains()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
var result = NordPoolEntityService.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Should().OnlyContain(e => e.Domain == "sensor" || e.Domain == "button");
|
||||
@@ -82,7 +82,7 @@ public class NordPoolEntityServiceTests
|
||||
public void GetInitEntitiesList_AllEntities_ShouldHaveUniqueEntityIds()
|
||||
{
|
||||
// Act
|
||||
var result = _sut.GetInitEntitiesList("EUR/kWh");
|
||||
var result = NordPoolEntityService.GetInitEntitiesList("EUR/kWh");
|
||||
|
||||
// Assert
|
||||
result.Select(e => e.EntityId).Should().OnlyHaveUniqueItems();
|
||||
@@ -115,12 +115,9 @@ public class NordPoolEntityServiceTests
|
||||
.Returns(() =>
|
||||
{
|
||||
callCount++;
|
||||
if (callCount == 2)
|
||||
{
|
||||
throw new InvalidOperationException("Entity creation failed");
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
return callCount == 2
|
||||
? throw new InvalidOperationException("Entity creation failed")
|
||||
: Task.CompletedTask;
|
||||
});
|
||||
|
||||
// Act
|
||||
@@ -435,13 +432,13 @@ public class NordPoolEntityServiceTests
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
public static IEnumerable<object?[]> MergedPriceScenarios()
|
||||
public static TheoryData<string, NordPoolPrice?, NordPoolPrice?, int> MergedPriceScenarios()
|
||||
{
|
||||
var data = new TheoryData<string, NordPoolPrice?, NordPoolPrice?, int>();
|
||||
var todayStart = new DateTime(2026, 3, 31, 6, 0, 0, DateTimeKind.Utc);
|
||||
var tomorrowStart = new DateTime(2026, 4, 1, 6, 0, 0, DateTimeKind.Utc);
|
||||
|
||||
yield return
|
||||
[
|
||||
data.Add(
|
||||
"today and tomorrow with null-start entry filtered",
|
||||
new NordPoolPrice
|
||||
{
|
||||
@@ -456,27 +453,26 @@ public class NordPoolEntityServiceTests
|
||||
Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }]
|
||||
},
|
||||
2
|
||||
];
|
||||
);
|
||||
|
||||
yield return
|
||||
[
|
||||
data.Add(
|
||||
"only today",
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = todayStart, End = todayStart.AddMinutes(15), Price = 17.91 }] },
|
||||
new NordPoolPrice { Prices = [] },
|
||||
1
|
||||
];
|
||||
);
|
||||
|
||||
yield return
|
||||
[
|
||||
data.Add(
|
||||
"only tomorrow",
|
||||
new NordPoolPrice { Prices = [] },
|
||||
new NordPoolPrice { Prices = [new NordPoolDayPrice { Start = tomorrowStart, End = tomorrowStart.AddMinutes(15), Price = 18.71 }] },
|
||||
1
|
||||
];
|
||||
);
|
||||
|
||||
yield return ["empty lists", new NordPoolPrice { Prices = [] }, new NordPoolPrice { Prices = [] }, 0];
|
||||
yield return ["null lists", new NordPoolPrice { Prices = null }, new NordPoolPrice { Prices = null }, 0];
|
||||
yield return ["null price objects", (NordPoolPrice?)null, (NordPoolPrice?)null, 0];
|
||||
data.Add("empty lists", new NordPoolPrice { Prices = [] }, new NordPoolPrice { Prices = [] }, 0);
|
||||
data.Add("null lists", new NordPoolPrice { Prices = null }, new NordPoolPrice { Prices = null }, 0);
|
||||
data.Add("null price objects", null, null, 0);
|
||||
return data;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -832,15 +828,17 @@ public class NordPoolEntityServiceTests
|
||||
|
||||
#region Cancellation
|
||||
|
||||
public static IEnumerable<object[]> CancelledOperations()
|
||||
public static TheoryData<string, Func<NordPoolEntityService, Task>> CancelledOperations()
|
||||
{
|
||||
var data = new TheoryData<string, Func<NordPoolEntityService, Task>>();
|
||||
var cancelled = new CancellationToken(canceled: true);
|
||||
var context = CreateDefaultContext();
|
||||
|
||||
yield return ["create entities", (Func<NordPoolEntityService, Task>)(sut => sut.CreateEntitiesAsync("EUR/kWh", cancelled))];
|
||||
yield return ["reset entities", (Func<NordPoolEntityService, Task>)(sut => sut.ResetEntitiesAsync(cancelled))];
|
||||
yield return ["update entities", (Func<NordPoolEntityService, Task>)(sut => sut.UpdateEntitiesAsync(context, cancelled))];
|
||||
yield return ["cleanup entities", (Func<NordPoolEntityService, Task>)(sut => sut.CleanupEntitiesAsync(cancelled))];
|
||||
data.Add("create entities", sut => sut.CreateEntitiesAsync("EUR/kWh", cancelled));
|
||||
data.Add("reset entities", sut => sut.ResetEntitiesAsync(cancelled));
|
||||
data.Add("update entities", sut => sut.UpdateEntitiesAsync(context, cancelled));
|
||||
data.Add("cleanup entities", sut => sut.CleanupEntitiesAsync(cancelled));
|
||||
return data;
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
||||
Reference in New Issue
Block a user