Skip to content

test_parser_time_aggregation_rule

Tests for ParserTimeAggregationRule 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_time_aggregation_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": "year",
    }
    logger = Mock(ILogger)

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

    # Assert
    assert isinstance(parsed_dict, IRuleData)

test_parse_operation_percentile_has_parameter()

Test if operation percentile is parsed correctly

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

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

    exception_raised = exc_info.value

    # Assert
    expected_message = (
        "Operation percentile is missing valid value like 'percentile(10)'"
    )
    assert exception_raised.args[0] == expected_message

test_parse_operation_percentile_valid_parameter()

Test if operation percentile is parsed correctly

Source code in tests/data/parsers/test_parser_time_aggregation_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",
    }
    logger = Mock(ILogger)

    # Act
    data = ParserTimeAggregationRule()
    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_time_aggregation_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",
    }
    logger = Mock(ILogger)

    # Act
    data = ParserTimeAggregationRule()
    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_parse_wrong_dict_to_rule_data_logic()

Test if an incorrect dictionary is not parsed

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

    # Act
    data = ParserTimeAggregationRule()

    with pytest.raises(AttributeError) as exc_info:
        data.parse_dict(contents, logger)

    exception_raised = exc_info.value

    # Assert
    expected_message = "Missing element operation"
    assert exception_raised.args[0] == expected_message

test_parser_time_aggregation_rule_creation_logic()

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

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

    # Act
    data = ParserTimeAggregationRule()

    # Assert

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