cd5f9d0352
Made-with: Cursor
NordPool.Manager.Tests
This project contains comprehensive unit tests for the NordPoolInit class.
Test Coverage
Price Calculation Tests
- PriceCorrection_ShouldCalculateCorrectPrice: Tests the price conversion from MW/h to cents/kWh with various inputs
- PriceCorrection_ShouldApplyVatAndConversion: Validates the full calculation chain (MW to kW, VAT application, Euro to cents conversion)
- AdjustedNordPoolPrice_ShouldApplyPriceCorrectionToAllPrices: Ensures price correction is applied to all price entries
- AdjustedNordPoolPrice_WithNullPriceValue_ShouldSkipThatPrice: Tests handling of null prices in the list
- AdjustedNordPoolPrice_WithNullPricesList_ShouldReturnEmptyPricesList: Tests handling of null price lists
- AdjustedNordPoolPrice_ShouldPreserveDateTimes: Ensures start/end times are preserved during price adjustment
Time Quarter Index Tests
- GetTimeQuarterIndex_ShouldReturnCorrectIndex: Tests quarter calculation for various times
- GetTimeQuarterIndex_ShouldHandleAllHoursCorrectly: Validates all hours including edge cases (midnight, 23:00, etc.)
Entity Management Tests
- GetInitEntitiesList_ShouldReturnFourEntities: Validates that all required entities are created
- GetInitEntitiesList_ShouldHaveCorrectEntityProperties: Ensures entities have correct domain, identifier, and names
- EnsureNordPoolEntityExistsAsync_WhenEntityDoesNotExist_ShouldCreateEntity: Tests entity creation when entity is missing
- EnsureNordPoolEntityExistsAsync_WhenEntityExists_ShouldNotCreateEntity: Ensures no duplicate entities are created
Data Parsing Tests
- ParseNordPoolResult_WithNullElement_ShouldReturnEmptyNordPoolPrice: Tests null handling
- ParseNordPoolResult_WithValidElement_ShouldMapCorrectly: Validates JSON deserialization and mapping
Integration Tests
- InitializeAsync_ShouldCallAllNecessaryMethods: Tests the full initialization flow
- GetCurrentPrice_WithValidPriceAtIndex_ShouldReturnCorrectPrice: Tests current price retrieval logic
Test Framework and Libraries
- xUnit: Primary testing framework
- Moq: Mocking framework for dependencies
- FluentAssertions: Fluent assertion library for readable tests
- AutoMapper: Used in the actual implementation, mocked in tests
Running the Tests
Using .NET CLI
dotnet test
Using Rider or Visual Studio
Run tests through the built-in test explorer.
With Coverage
dotnet test --collect:"XPlat Code Coverage"
Test Organization
Tests are organized by functionality:
- Constructor Tests: Basic initialization
- Calculation Tests: Price conversion and adjustments
- Time Tests: Quarter index calculations
- Entity Tests: Entity creation and management
- Parsing Tests: JSON parsing and data mapping
- Integration Tests: Full workflow tests
Mocking Strategy
The tests use Moq to mock all external dependencies:
ILogger<NordPoolInit>: Logging functionalityIMapper: AutoMapper for DTO to model mappingIEntityManager: Entity management in Home AssistantIHaContext: Home Assistant contextEntitiesandServices: Home Assistant generated entities and services
Constants Used in Production Code
The tests validate these constants:
MegawattToKilowattDivisor = 1000.0VatRate = 1.21(21% VAT)EuroToCentsMultiplier = 100.0PriceDecimalPlaces = 2
Private Method Testing
Some private methods are tested using reflection to ensure internal logic correctness:
GetTimeQuarterIndexPriceCorrectionAdjustedNordPoolPriceParseNordPoolResultGetInitEntitiesListGetCurrentPriceEnsureNordPoolEntityExistsAsync
This approach ensures comprehensive coverage of the business logic while maintaining encapsulation.