Add shared test data builders and Qodana configuration
- Extract per-monitor *TestData helper classes and rework tests to use them - Add boiler run-time tracker abstraction (implementation pending) - Drop unused NordPool init/update-result types and stale PLAN.md - Add qodana.yaml with the inspection set to keep the analysis focused
This commit is contained in:
@@ -0,0 +1,244 @@
|
||||
using System.Globalization;
|
||||
using System.Text.Json;
|
||||
using BatteryMonitor.Models;
|
||||
using Moq;
|
||||
using NetDaemon.HassModel;
|
||||
using NetDaemon.HassModel.Entities;
|
||||
|
||||
namespace BatteryMonitor.Manager.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Shared, deterministic builders and datasets used by the BatteryMonitor tests.
|
||||
/// Keeping entity/state construction and generated inputs here means the test
|
||||
/// classes only describe the scenario under test.
|
||||
/// </summary>
|
||||
internal static class BatteryMonitorTestData
|
||||
{
|
||||
public const string DefaultFriendlyName = "Battery Device";
|
||||
|
||||
/// <summary>
|
||||
/// Describes one entity exposed through <see cref="IHaContext.GetAllEntities"/>.
|
||||
/// </summary>
|
||||
public sealed record EntitySpec
|
||||
{
|
||||
public required string EntityId { get; init; }
|
||||
|
||||
public string? State { get; init; } = "50";
|
||||
|
||||
public string? DeviceClass { get; init; } = BatteryMonitorConsts.BatteryDeviceClass;
|
||||
|
||||
public string? FriendlyName { get; init; } = DefaultFriendlyName;
|
||||
|
||||
/// <summary>When false the context returns no <see cref="EntityState"/> for the entity.</summary>
|
||||
public bool HasState { get; init; } = true;
|
||||
}
|
||||
|
||||
/// <summary>A battery device-class sensor.</summary>
|
||||
public static EntitySpec Battery(string entityId, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>A sensor with an arbitrary (or missing) device class.</summary>
|
||||
public static EntitySpec Sensor(string entityId, string? deviceClass, string? state, string? friendlyName = DefaultFriendlyName)
|
||||
{
|
||||
return new EntitySpec
|
||||
{
|
||||
EntityId = entityId,
|
||||
DeviceClass = deviceClass,
|
||||
State = state,
|
||||
FriendlyName = friendlyName
|
||||
};
|
||||
}
|
||||
|
||||
public static EntityState CreateEntityState(EntitySpec spec)
|
||||
{
|
||||
var attributes = new Dictionary<string, object?>(StringComparer.Ordinal);
|
||||
|
||||
if (spec.DeviceClass is not null)
|
||||
{
|
||||
attributes["device_class"] = spec.DeviceClass;
|
||||
}
|
||||
|
||||
if (spec.FriendlyName is not null)
|
||||
{
|
||||
attributes["friendly_name"] = spec.FriendlyName;
|
||||
}
|
||||
|
||||
return new EntityState
|
||||
{
|
||||
State = spec.State,
|
||||
AttributesJson = JsonSerializer.SerializeToElement(attributes)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wires <see cref="IHaContext.GetState"/> and <see cref="IHaContext.GetAllEntities"/>
|
||||
/// on the mock and returns the created entities in declaration order.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<Entity> ConfigureHaContext(Mock<IHaContext> haContextMock, params EntitySpec[] entities)
|
||||
{
|
||||
var entityList = new List<Entity>(entities.Length);
|
||||
|
||||
foreach (var spec in entities)
|
||||
{
|
||||
if (spec.HasState)
|
||||
{
|
||||
haContextMock.Setup(h => h.GetState(spec.EntityId)).Returns(CreateEntityState(spec));
|
||||
}
|
||||
|
||||
entityList.Add(new Entity(haContextMock.Object, spec.EntityId));
|
||||
}
|
||||
|
||||
haContextMock.Setup(h => h.GetAllEntities()).Returns(entityList);
|
||||
|
||||
return entityList;
|
||||
}
|
||||
|
||||
public static BatterySensorInfo CreateSensorInfo(string entityId, double batteryLevel, string? friendlyName = null)
|
||||
{
|
||||
return new BatterySensorInfo
|
||||
{
|
||||
EntityId = entityId,
|
||||
FriendlyName = friendlyName ?? entityId,
|
||||
BatteryLevel = batteryLevel
|
||||
};
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return sensors
|
||||
.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public static List<BatterySensorInfo> CreateSensorInfos(params (string EntityId, string FriendlyName, double BatteryLevel)[] sensors)
|
||||
{
|
||||
return sensors
|
||||
.Select(s => CreateSensorInfo(s.EntityId, s.BatteryLevel, s.FriendlyName))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates battery sensors with every integer level in <c>0..count-1</c>.
|
||||
/// </summary>
|
||||
public static EntitySpec[] CreateGeneratedBatteryEntities(int count)
|
||||
{
|
||||
return Enumerable.Range(0, count)
|
||||
.Select(i => Battery(
|
||||
$"sensor.generated_battery_{i:D3}",
|
||||
i.ToString(CultureInfo.InvariantCulture),
|
||||
$"Generated Battery {i:D3}"))
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates notification devices with deterministic, non-monotonic levels so
|
||||
/// ordering behaviour is exercised over more than a handful of rows.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<BatterySensorInfo> CreateGeneratedSensorInfos(int count)
|
||||
{
|
||||
return Enumerable.Range(0, count)
|
||||
.Select(i => CreateSensorInfo(
|
||||
$"sensor.generated_{i:D3}",
|
||||
(i * 37) % 101,
|
||||
$"Generated Device {i:D3}"))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// (threshold, state, expected) rows straddling the strict <c>level < threshold</c>
|
||||
/// boundary, including zero/negative thresholds and int min/max thresholds.
|
||||
/// </summary>
|
||||
public static TheoryData<int, string, bool> ThresholdBoundaryRows { get; } = new()
|
||||
{
|
||||
{ 5, "4", true },
|
||||
{ 5, "5", false },
|
||||
{ 5, "6", false },
|
||||
{ 5, "0", true },
|
||||
{ 5, "-3", true },
|
||||
{ 5, "4.9", true },
|
||||
{ 5, "5.0", false },
|
||||
{ 5, "100", false },
|
||||
{ 1, "0.5", true },
|
||||
{ 1, "1", false },
|
||||
{ 20, "19", true },
|
||||
{ 20, "19.99", true },
|
||||
{ 20, "20", false },
|
||||
{ 0, "-1", true },
|
||||
{ 0, "0", false },
|
||||
{ 0, "1", false },
|
||||
{ -5, "-6", true },
|
||||
{ -5, "-5", false },
|
||||
{ -5, "-4", false },
|
||||
{ 100, "99.9", true },
|
||||
{ 100, "100", false },
|
||||
{ 101, "100", true },
|
||||
{ int.MaxValue, "100", true },
|
||||
{ int.MinValue, "-2147483648", false },
|
||||
{ int.MinValue, "-2147483649", true }
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// States that must never produce a <see cref="BatterySensorInfo"/>, including
|
||||
/// whitespace/malformed strings and a null state.
|
||||
/// </summary>
|
||||
public static TheoryData<string?> UnusableStateRows { get; } = new()
|
||||
{
|
||||
"unavailable",
|
||||
"unknown",
|
||||
"UNAVAILABLE",
|
||||
"UNKNOWN",
|
||||
"",
|
||||
" ",
|
||||
"\t",
|
||||
"not_a_number",
|
||||
"3%",
|
||||
"--1",
|
||||
"low",
|
||||
"1 2",
|
||||
(string?)null
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Cross-product of every excluded keyword (lower and upper case) plus
|
||||
/// non-excluded look-alikes that must not be filtered.
|
||||
/// </summary>
|
||||
public static TheoryData<string, bool> ExcludedEntityIdRows()
|
||||
{
|
||||
var data = new TheoryData<string, bool>();
|
||||
|
||||
foreach (var keyword in BatteryMonitorConsts.ExcludedKeywords)
|
||||
{
|
||||
data.Add($"sensor.my_{keyword}_battery_level", true);
|
||||
data.Add($"SENSOR.MY_{keyword.ToUpperInvariant()}_BATTERY_LEVEL", true);
|
||||
}
|
||||
|
||||
data.Add("sensor.iphone", true);
|
||||
data.Add("sensor.x_phone_x", true);
|
||||
data.Add("sensor.bathroom_battery", false);
|
||||
data.Add("sensor.boiler_temp_battery", false);
|
||||
data.Add("sensor.aquarium_leak_sensor_battery", false);
|
||||
data.Add("sensor.temperature", false);
|
||||
data.Add("", false);
|
||||
data.Add(" ", false);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>Device counts used to exercise notification payload generation.</summary>
|
||||
public static TheoryData<int> NotifyDeviceCounts { get; } = new()
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
10,
|
||||
50
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user