Skip to content

test_combine_results_rule

Tests for RuleBase class

test_all_operations_combine_results_rule(operation, expected_result)

Test the outcome of each operand for the combine results rule

Source code in tests/business/entities/rules/test_combine_results_rule.py
@pytest.mark.parametrize(
    "operation, expected_result",
    [
        (MultiArrayOperationType.MIN, [4, 5, 3]),
        (MultiArrayOperationType.MAX, [20, 12, 24]),
        (MultiArrayOperationType.MULTIPLY, [1200, 420, 432]),
        (MultiArrayOperationType.AVERAGE, [13, 8, 11]),
        (MultiArrayOperationType.MEDIAN, [15, 7, 6]),
        (MultiArrayOperationType.ADD, [39, 24, 33]),
        (MultiArrayOperationType.SUBTRACT, [1, -10, -27]),
    ],
)
def test_all_operations_combine_results_rule(
    operation: MultiArrayOperationType, expected_result: List[float]
):
    """Test the outcome of each operand for the combine results rule"""
    # Arrange
    logger = Mock(ILogger)
    dict_vars = {
        "var1_name": _xr.DataArray([20, 7, 3]),
        "var2_name": _xr.DataArray([4, 5, 6]),
        "var3_name": _xr.DataArray([15, 12, 24]),
    }

    # Act
    rule = CombineResultsRule(
        "test_name",
        ["var1_name", "var2_name", "var3_name"],
        operation,
    )
    obtained_result = rule.execute(dict_vars, logger)

    # Assert
    _xr.testing.assert_equal(obtained_result, _xr.DataArray(expected_result))

test_all_operations_ignore_nan(operation, expected_result)

Test the outcome of each operand for the combine results rule

Source code in tests/business/entities/rules/test_combine_results_rule.py
@pytest.mark.parametrize(
    "operation, expected_result",
    [
        (MultiArrayOperationType.MIN, [4, 5, 3]),
        (MultiArrayOperationType.MAX, [20, 12, 24]),
        (MultiArrayOperationType.MULTIPLY, [_np.nan, 420, 432]),
        (MultiArrayOperationType.AVERAGE, [12, 8, 11]),
        (MultiArrayOperationType.MEDIAN, [12, 7, 6]),
        (MultiArrayOperationType.ADD, [24, 24, 33]),
        (MultiArrayOperationType.SUBTRACT, [16, -10, -27]),
    ],
)
def test_all_operations_ignore_nan(
    operation: MultiArrayOperationType, expected_result: List[float]
):
    """Test the outcome of each operand for the combine results rule"""
    # Arrange
    logger = Mock(ILogger)
    dict_vars = {
        "var1_name": _xr.DataArray([20, 7, 3]),
        "var2_name": _xr.DataArray([4, 5, 6]),
        "var3_name": _xr.DataArray([_np.nan, 12, 24]),
    }

    # Act
    rule = CombineResultsRule(
        "test_name", ["var1_name", "var2_name", "var3_name"], operation, ignore_nan=True
    )
    obtained_result = rule.execute(dict_vars, logger)

    # Assert
    _xr.testing.assert_equal(obtained_result, _xr.DataArray(expected_result))

test_all_operations_incl_nan(operation, expected_result)

Test the outcome of each operand for the combine results rule

Source code in tests/business/entities/rules/test_combine_results_rule.py
@pytest.mark.parametrize(
    "operation, expected_result",
    [
        (MultiArrayOperationType.MIN, [_np.nan, 5, 3]),
        (MultiArrayOperationType.MAX, [_np.nan, 12, 24]),
        (MultiArrayOperationType.MULTIPLY, [_np.nan, 420, 432]),
        (MultiArrayOperationType.AVERAGE, [_np.nan, 8, 11]),
        (MultiArrayOperationType.MEDIAN, [_np.nan, 7, 6]),
        (MultiArrayOperationType.ADD, [_np.nan, 24, 33]),
        (MultiArrayOperationType.SUBTRACT, [_np.nan, -10, -27]),
    ],
)
def test_all_operations_incl_nan(
    operation: MultiArrayOperationType, expected_result: List[float]
):
    """Test the outcome of each operand for the combine results rule"""
    # Arrange
    logger = Mock(ILogger)
    dict_vars = {
        "var1_name": _xr.DataArray([20, 7, 3]),
        "var2_name": _xr.DataArray([4, 5, 6]),
        "var3_name": _xr.DataArray([_np.nan, 12, 24]),
    }

    # Act
    rule = CombineResultsRule(
        "test_name",
        ["var1_name", "var2_name", "var3_name"],
        operation,
    )
    obtained_result = rule.execute(dict_vars, logger)

    # Assert
    _xr.testing.assert_equal(obtained_result, _xr.DataArray(expected_result))

test_create_combine_results_rule_with_all_fields()

Test creating a combine results rule with all fields

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_create_combine_results_rule_with_all_fields():
    """Test creating a combine results rule with all fields"""

    # Arrange & Act
    rule = CombineResultsRule(
        "test_rule_name", ["foo", "hello"], MultiArrayOperationType.MULTIPLY
    )

    rule.description = "test description"

    # Assert
    assert isinstance(rule, CombineResultsRule)
    assert rule.name == "test_rule_name"
    assert rule.description == "test description"
    assert rule.input_variable_names == ["foo", "hello"]
    assert rule.operation_type == MultiArrayOperationType.MULTIPLY
    assert rule.output_variable_name == "output"

test_create_combine_results_rule_with_defaults()

Test creating a combine results rule with defaults

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_create_combine_results_rule_with_defaults():
    """Test creating a combine results rule with defaults"""

    # Arrange & Act
    rule = CombineResultsRule(
        "test_rule_name", ["foo", "hello"], MultiArrayOperationType.MULTIPLY
    )
    # Assert
    assert isinstance(rule, CombineResultsRule)
    assert rule.name == "test_rule_name"
    assert rule.description == ""
    assert rule.input_variable_names == ["foo", "hello"]
    assert rule.operation_type == MultiArrayOperationType.MULTIPLY
    assert rule.output_variable_name == "output"

test_dims_present_in_result()

Test that the dims metadata of the result is equal to the one of the first xarray used.

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_dims_present_in_result():
    """Test that the dims metadata of the result is equal to the one of the first xarray used."""
    # Arrange
    logger = Mock(ILogger)
    raw_data_1 = _np.ones((10, 20))
    raw_data_2 = 2 * _np.ones((10, 20))
    raw_data = [raw_data_1, raw_data_2]
    xarray_data = [
        _xr.DataArray(data=arr, dims=["test_dimension_1", "test_dimension_2"])
        for arr in raw_data
    ]
    dict_data = {"var1_name": xarray_data[0], "var2_name": xarray_data[1]}

    # Act
    rule = CombineResultsRule(
        "test_name", ["var1_name", "var2_name"], MultiArrayOperationType.ADD
    )
    obtained_result = rule.execute(dict_data, logger)

    # Assert
    # _xr.testing.assert_equal(obtained_result.dims, xarray_data[0].dims)
    assert obtained_result.dims == xarray_data[0].dims

test_execute_error_combine_results_rule_different_lengths()

Test setting input_variable_names of a RuleBase

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_execute_error_combine_results_rule_different_lengths():
    """Test setting input_variable_names of a RuleBase"""

    # Arrange & Act
    rule = CombineResultsRule(
        "test", ["foo_data", "hello_data"], MultiArrayOperationType.MULTIPLY
    )
    value_array = {
        "foo_data": _xr.DataArray([1, 2, 3]),
        "hello_data": _xr.DataArray([4, 3, 2, 1]),
    }

    # Assert
    with pytest.raises(ValueError) as exc_info:
        rule.execute(value_array, logger=Mock(ILogger))

    exception_raised = exc_info.value
    assert exception_raised.args[0] == "The arrays must have the same dimensions."

test_execute_error_combine_results_rule_different_shapes()

Test setting input_variable_names of a RuleBase

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_execute_error_combine_results_rule_different_shapes():
    """Test setting input_variable_names of a RuleBase"""

    # Arrange & Act
    rule = CombineResultsRule(
        "test", ["foo_data", "hello_data"], MultiArrayOperationType.MULTIPLY, False
    )
    value_array = {
        "foo_data": _xr.DataArray([[1, 2], [3, 4]]),
        "hello_data": _xr.DataArray([4, 3, 2, 1]),
    }

    # Assert
    with pytest.raises(ValueError) as exc_info:
        rule.execute(value_array, logger=Mock(ILogger))

    exception_raised = exc_info.value
    assert exception_raised.args[0] == "The arrays must have the same dimensions."

test_no_validate_error_with_correct_rule()

Test a correct combine results rule validates without error

Source code in tests/business/entities/rules/test_combine_results_rule.py
def test_no_validate_error_with_correct_rule():
    """Test a correct combine results rule validates without error"""

    # Arrange
    logger = Mock(ILogger)
    rule = CombineResultsRule(
        "test_rule_name", ["foo", "hello"], MultiArrayOperationType.MULTIPLY
    )

    # Act
    valid = rule.validate(logger)

    # Assert
    assert isinstance(rule, CombineResultsRule)
    assert valid