Skip to content

test_parser_rolling_statistics_rule

Tests for ParserRollingStatisticsRule class

test_parse_dict_to_rule_data_logic()

Test if a correct dictionary is parsed into a RuleData object

Source code in tests/data/parsers/test_parser_rolling_statistics_rule.py
def test_parse_dict_to_rule_data_logic():
    """Test if a correct dictionary is parsed into a RuleData object"""
    # Arrange
    contents = {
        "name": "testname",
        "input_variable": "input",
        "operation": "MIN",
        "output_variable": "output",
        "time_scale": "day",
        "period": "2",
    }
    logger = Mock(ILogger)

    # Act
    data = ParserRollingStatisticsRule()
    parsed_dict = data.parse_dict(contents, logger)

    # Assert
    assert isinstance(parsed_dict, IRuleData)

test_parse_operation_percentile_valid_parameter()

Test if operation percentile is parsed correctly

Source code in tests/data/parsers/test_parser_rolling_statistics_rule.py
def test_parse_operation_percentile_valid_parameter():
    """Test if operation percentile is parsed correctly"""
    # Arrange
    contents = {
        "name": "testname",
        "input_variable": "input",
        "operation": "PERCENTILE(999)",
        "output_variable": "output",
        "time_scale": "year",
        "period": "2",
    }
    logger = Mock(ILogger)

    # Act
    data = ParserRollingStatisticsRule()
    with pytest.raises(ValueError) as exc_info:
        data.parse_dict(contents, logger)

    exception_raised = exc_info.value

    # Assert
    expected_message = "Operation percentile should be a number between 0 and 100."
    assert exception_raised.args[0] == expected_message

test_parse_operation_type()

Test if an incorrect dictionary is not parsed

Source code in tests/data/parsers/test_parser_rolling_statistics_rule.py
def test_parse_operation_type():
    """Test if an incorrect dictionary is not parsed"""
    # Arrange
    contents = {
        "name": "testname",
        "input_variable": "input",
        "operation": "Minimum",
        "output_variable": "output",
        "time_scale": "year",
        "period": "2",
    }
    logger = Mock(ILogger)

    # Act
    data = ParserRollingStatisticsRule()
    with pytest.raises(ValueError) as exc_info:
        data.parse_dict(contents, logger)

    exception_raised = exc_info.value

    # Assert
    expected_message = (
        "Operation 'Minimum' is not of a predefined type. Should be in:"
        + f"{[o.name for o in TimeOperationType]}."
    )
    assert exception_raised.args[0] == expected_message

test_parser_rolling_statistics_rule_creation_logic()

The RollingStatisticsRule should parse the provided dictionary to correctly initialize itself during creation

Source code in tests/data/parsers/test_parser_rolling_statistics_rule.py
def test_parser_rolling_statistics_rule_creation_logic():
    """The RollingStatisticsRule should parse the provided dictionary
    to correctly initialize itself during creation"""

    # Act
    data = ParserRollingStatisticsRule()

    # Assert

    assert isinstance(data, IParserRuleBase)
    assert data.rule_type_name == "rolling_statistics_rule"