# 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 ```bash dotnet test ``` ### Using Rider or Visual Studio Run tests through the built-in test explorer. ### With Coverage ```bash dotnet test --collect:"XPlat Code Coverage" ``` ## Test Organization Tests are organized by functionality: 1. **Constructor Tests**: Basic initialization 2. **Calculation Tests**: Price conversion and adjustments 3. **Time Tests**: Quarter index calculations 4. **Entity Tests**: Entity creation and management 5. **Parsing Tests**: JSON parsing and data mapping 6. **Integration Tests**: Full workflow tests ## Mocking Strategy The tests use Moq to mock all external dependencies: - `ILogger`: Logging functionality - `IMapper`: AutoMapper for DTO to model mapping - `IEntityManager`: Entity management in Home Assistant - `IHaContext`: Home Assistant context - `Entities` and `Services`: Home Assistant generated entities and services ## Constants Used in Production Code The tests validate these constants: - `MegawattToKilowattDivisor = 1000.0` - `VatRate = 1.21` (21% VAT) - `EuroToCentsMultiplier = 100.0` - `PriceDecimalPlaces = 2` ## Private Method Testing Some private methods are tested using reflection to ensure internal logic correctness: - `GetTimeQuarterIndex` - `PriceCorrection` - `AdjustedNordPoolPrice` - `ParseNordPoolResult` - `GetInitEntitiesList` - `GetCurrentPrice` - `EnsureNordPoolEntityExistsAsync` This approach ensures comprehensive coverage of the business logic while maintaining encapsulation.