File: test_utility_additional.py

package info (click to toggle)
python-bsblan 2.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 784 kB
  • sloc: python: 2,890; makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,977 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""Additional tests for the utility module."""

# pylint: disable=redefined-outer-name
# pylint: disable=protected-access

from __future__ import annotations

import logging
from typing import Any

import pytest

from bsblan.utility import APIValidator


@pytest.fixture
def mock_request_data_invalid() -> dict[str, Any]:
    """Fixture for invalid mock request data."""
    return {
        "700": {
            "name": "Operating mode heating circuit 1",
            "value": "---",  # Invalid value
            "unit": "",
            "desc": "",
        },
        "710": {
            "name": "Comfort setpoint heating circuit 1",
            # Missing value field
            "unit": "°C",
            "desc": "",
        },
    }


@pytest.fixture
def fresh_api_validator() -> APIValidator:
    """Fixture for a fresh APIValidator instance with empty validated_sections."""
    api_config = {
        "heating": {
            "700": "hvac_mode",
            "710": "target_temperature",
        },
        "staticValues": {
            "714": "min_temp",
            "716": "max_temp",
        },
        "hot_water": {
            "8830": "dhw_actual_value_top_temperature",
            "8820": "state_dhw_pump",
        },
    }
    validated_sections: set[str] = set()
    return APIValidator(api_config=api_config, validated_sections=validated_sections)


def test_validate_section_with_invalid_values(
    fresh_api_validator: APIValidator,
    mock_request_data_invalid: dict[str, Any],
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test validating a section with invalid data values."""
    with caplog.at_level(logging.INFO):
        # The validation will run but log errors for invalid data
        fresh_api_validator.validate_section("heating", mock_request_data_invalid)

        # Check that appropriate error messages were logged
        assert "returned invalid value" in caplog.text


def test_api_validator_with_non_existing_section(
    fresh_api_validator: APIValidator,
) -> None:
    """Test APIValidator with a non-existing section."""
    # Test with a section that doesn't exist in the api_config
    non_existent_section_params = fresh_api_validator.get_section_params(
        "non_existent_section"
    )
    assert non_existent_section_params == {}

    # Check that the non-existent section is not validated
    assert not fresh_api_validator.is_section_validated("non_existent_section")


def test_api_validator_with_empty_request_logs(
    fresh_api_validator: APIValidator,
    caplog: pytest.LogCaptureFixture,
) -> None:
    """Test that APIValidator logs appropriate messages with empty request data."""
    # Test with empty request data
    caplog.clear()
    with caplog.at_level(logging.INFO):
        fresh_api_validator.validate_section("heating", {})

        # Check that appropriate warning messages were logged
        assert "Parameter" in caplog.text
        assert "not found in device response" in caplog.text