Skip to content

test_step_function_rule

Tests for Step Function Rule class

fixture_example_rule()

Inititaion of StepFunctionRule to be reused in the following tests

Source code in tests/business/entities/rules/test_step_function_rule.py
@pytest.fixture(name="example_rule")
def fixture_example_rule():
    """Inititaion of StepFunctionRule to be reused in the following tests"""
    return StepFunctionRule(
        "step_function_rule_name",
        "input_variable_name",
        [0, 1, 2, 5, 10],
        [10, 11, 12, 15, 20],
    )

fixture_example_rule_combined()

Inititation of StepFunctionRule to be reused in the following tests, with differences in increasing an decreasing of the steps.

Source code in tests/business/entities/rules/test_step_function_rule.py
@pytest.fixture(name="example_rule_combined")
def fixture_example_rule_combined():
    """Inititation of StepFunctionRule to be reused in the following tests,
    with differences in increasing an decreasing of the steps."""
    return StepFunctionRule(
        "step_function_rule_name",
        "input_variable_name",
        [0, 1, 2, 5, 10],
        [22, 15, 10, 12, 20],
    )

test_create_step_function(example_rule)

Test creating a new (valid) Step Fuction rule

Source code in tests/business/entities/rules/test_step_function_rule.py
def test_create_step_function(example_rule):
    """
    Test creating a new (valid) Step Fuction rule
    """
    # Arrange
    logger = Mock(ILogger)

    # Assert
    assert example_rule.name == "step_function_rule_name"
    assert example_rule.input_variable_names[0] == "input_variable_name"
    assert (example_rule.limits == [0, 1, 2, 5, 10]).all()
    assert isinstance(example_rule, StepFunctionRule)
    assert example_rule.validate(logger)

test_execute_values_at_limits(example_rule, input_value, expected_output_value)

Test the function execution with input values exactly at the interval limits.

Source code in tests/business/entities/rules/test_step_function_rule.py
@pytest.mark.parametrize(
    "input_value, expected_output_value",
    [
        (0, (10, [0, 0])),
        (1, (11, [0, 0])),
        (2, (12, [0, 0])),
        (5, (15, [0, 0])),
        (10, (20, [0, 0])),
    ],
)
def test_execute_values_at_limits(
    example_rule, input_value: int, expected_output_value: int
):
    """
    Test the function execution with input values exactly at the interval limits.
    """
    # Arrange
    logger = Mock(ILogger)

    # Assert
    assert example_rule.execute(input_value, logger) == expected_output_value
    logger.log_warning.assert_not_called()

test_execute_values_between_limits(example_rule, input_value, expected_output_value)

Test the function execution with input values between the interval limits.

Source code in tests/business/entities/rules/test_step_function_rule.py
@pytest.mark.parametrize(
    "input_value, expected_output_value",
    [
        (0.5, (10, [0, 0])),
        (1.5, (11, [0, 0])),
        (2.5, (12, [0, 0])),
        (5.5, (15, [0, 0])),
    ],
)
def test_execute_values_between_limits(
    example_rule, input_value: int, expected_output_value: int
):
    """
    Test the function execution with input values between the interval limits.
    """
    # Arrange
    logger = Mock(ILogger)

    # Assert
    assert example_rule.execute(input_value, logger) == expected_output_value
    logger.log_warning.assert_not_called()

test_execute_values_combined_dec_inc(example_rule_combined, input_value, expected_output_value)

Test the function execution with input values between the interval limits.

Source code in tests/business/entities/rules/test_step_function_rule.py
@pytest.mark.parametrize(
    "input_value, expected_output_value",
    [
        (-1, (22, [1, 0])),
        (0.5, (22, [0, 0])),
        (1.5, (15, [0, 0])),
        (2.5, (10, [0, 0])),
        (5.5, (12, [0, 0])),
        (10.5, (20, [0, 1])),
    ],
)
def test_execute_values_combined_dec_inc(
    example_rule_combined,
    input_value: int,
    expected_output_value: int,
):
    """
    Test the function execution with input values between the interval limits.
    """
    # Arrange
    logger = Mock(ILogger)

    # Assert
    assert example_rule_combined.execute(input_value, logger) == expected_output_value

test_limits_and_responses_have_different_lengths(example_rule)

Test the function execution when limits and responses have different lengths

Source code in tests/business/entities/rules/test_step_function_rule.py
def test_limits_and_responses_have_different_lengths(example_rule):
    """
    Test the function execution when limits and responses have different lengths
    """
    # Arrange
    logger = Mock(ILogger)

    # Act
    example_rule._limits = _np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

    # Assert
    assert not example_rule.validate(logger)
    logger.log_error.assert_called_with(
        "The number of limits and of responses must be equal."
    )

test_limits_must_be_unique(example_rule)

The ParserStepFunctionRule cannot sort limits if they are not unique. An error message should be sent.

Source code in tests/business/entities/rules/test_step_function_rule.py
def test_limits_must_be_unique(example_rule):
    """The ParserStepFunctionRule cannot sort
    limits if they are not unique. An error message should be sent."""
    # Arrange
    logger = Mock(ILogger)

    # Act
    example_rule._limits = _np.array([0, 1, 2, 5, 1])

    # Assert
    assert not example_rule.validate(logger)
    logger.log_error.assert_called_with("Limits must be unique.")

test_limits_should_be_ordered(example_rule)

The ParserStepFunctionRule calculate responses if the limits are not sorted.

Source code in tests/business/entities/rules/test_step_function_rule.py
def test_limits_should_be_ordered(example_rule):
    """The ParserStepFunctionRule calculate responses if the limits
    are not sorted."""
    # Arrange
    logger = Mock(ILogger)

    # Act
    example_rule._limits = _np.array([0, 1, 2, 5, 4])

    # Assert
    assert not example_rule.validate(logger)
    logger.log_error.assert_called_with("The limits should be given in a sorted order.")