|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for the OnlyIfPrevious implementation.""" |
| 5 | + |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Callable, Generic, TypeVar |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from frequenz.channels.experimental import OnlyIfPrevious |
| 12 | + |
| 13 | +_T = TypeVar("_T") |
| 14 | + |
| 15 | + |
| 16 | +@dataclass(frozen=True, kw_only=True) |
| 17 | +class PredicateTestCase(Generic[_T]): |
| 18 | + """Test case for testing OnlyIfPrevious behavior with different predicates.""" |
| 19 | + |
| 20 | + title: str |
| 21 | + messages: list[_T] |
| 22 | + expected_results: list[bool] |
| 23 | + predicate: Callable[[_T, _T], bool] |
| 24 | + first_is_true: bool |
| 25 | + |
| 26 | + |
| 27 | +def always_true(old: object, new: object) -> bool: # pylint: disable=unused-argument |
| 28 | + """Return always True.""" |
| 29 | + return True |
| 30 | + |
| 31 | + |
| 32 | +def always_false(old: object, new: object) -> bool: # pylint: disable=unused-argument |
| 33 | + """Return always False.""" |
| 34 | + return False |
| 35 | + |
| 36 | + |
| 37 | +def is_greater(old: int, new: int) -> bool: |
| 38 | + """Return weather the new value is greater than the old one.""" |
| 39 | + return new > old |
| 40 | + |
| 41 | + |
| 42 | +def is_not_same_instance(old: object, new: object) -> bool: |
| 43 | + """Return weather the new value is not the same instance as the old one.""" |
| 44 | + return old is not new |
| 45 | + |
| 46 | + |
| 47 | +PREDICATE_TEST_CASES = [ |
| 48 | + # Basic cases with different predicates |
| 49 | + PredicateTestCase( |
| 50 | + title="Always true predicate", |
| 51 | + messages=[1, 2, 3], |
| 52 | + expected_results=[True, True, True], |
| 53 | + predicate=always_true, |
| 54 | + first_is_true=True, |
| 55 | + ), |
| 56 | + PredicateTestCase( |
| 57 | + title="Always false predicate with first_is_true=False", |
| 58 | + messages=[1, 2, 3], |
| 59 | + expected_results=[False, False, False], |
| 60 | + predicate=always_false, |
| 61 | + first_is_true=False, |
| 62 | + ), |
| 63 | + PredicateTestCase( |
| 64 | + title="Greater than predicate", |
| 65 | + messages=[1, 2, 0, 0, 1, 2], |
| 66 | + expected_results=[False, True, False, False, True, True], |
| 67 | + predicate=is_greater, |
| 68 | + first_is_true=False, |
| 69 | + ), |
| 70 | + # Edge cases |
| 71 | + PredicateTestCase( |
| 72 | + title="Empty sequence", |
| 73 | + messages=[], |
| 74 | + expected_results=[], |
| 75 | + predicate=always_true, |
| 76 | + first_is_true=True, |
| 77 | + ), |
| 78 | + PredicateTestCase( |
| 79 | + title="Single value with first_is_true=True", |
| 80 | + messages=[1], |
| 81 | + expected_results=[True], |
| 82 | + predicate=always_false, |
| 83 | + first_is_true=True, |
| 84 | + ), |
| 85 | + PredicateTestCase( |
| 86 | + title="Single value with first_is_true=False", |
| 87 | + messages=[1], |
| 88 | + expected_results=[False], |
| 89 | + predicate=always_true, |
| 90 | + first_is_true=False, |
| 91 | + ), |
| 92 | + # Instance comparison |
| 93 | + PredicateTestCase( |
| 94 | + title="Same instances", |
| 95 | + messages=[1, 1], |
| 96 | + expected_results=[True, False], |
| 97 | + predicate=is_not_same_instance, |
| 98 | + first_is_true=True, |
| 99 | + ), |
| 100 | + PredicateTestCase( |
| 101 | + title="Different instances of same values", |
| 102 | + messages=[[1], [1]], |
| 103 | + expected_results=[True, True], |
| 104 | + predicate=is_not_same_instance, |
| 105 | + first_is_true=True, |
| 106 | + ), |
| 107 | +] |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + "test_case", |
| 112 | + PREDICATE_TEST_CASES, |
| 113 | + ids=lambda test_case: test_case.title, |
| 114 | +) |
| 115 | +def test_only_if_previous(test_case: PredicateTestCase[_T]) -> None: |
| 116 | + """Test the OnlyIfPrevious with different predicates and sequences. |
| 117 | +
|
| 118 | + Args: |
| 119 | + test_case: The test case containing the input values and expected results. |
| 120 | + """ |
| 121 | + only_if_previous = OnlyIfPrevious( |
| 122 | + test_case.predicate, |
| 123 | + first_is_true=test_case.first_is_true, |
| 124 | + ) |
| 125 | + results = [only_if_previous(msg) for msg in test_case.messages] |
| 126 | + assert results == test_case.expected_results |
| 127 | + |
| 128 | + |
| 129 | +def test_only_if_previous_state_independence() -> None: |
| 130 | + """Test that multiple OnlyIfPrevious instances maintain independent state.""" |
| 131 | + only_if_previous1 = OnlyIfPrevious(is_greater) |
| 132 | + only_if_previous2 = OnlyIfPrevious(is_greater) |
| 133 | + |
| 134 | + # First message should be accepted (first_is_true default is True) |
| 135 | + assert only_if_previous1(1) is True |
| 136 | + assert only_if_previous2(10) is True |
| 137 | + |
| 138 | + # Second messages should be evaluated independently |
| 139 | + assert only_if_previous1(0) is False # 0 is not greater than 1 |
| 140 | + assert only_if_previous2(20) is True # 20 is greater than 10 |
| 141 | + |
| 142 | + |
| 143 | +def test_only_if_previous_str_representation() -> None: |
| 144 | + """Test the string representation of OnlyIfPrevious.""" |
| 145 | + only_if_previous = OnlyIfPrevious(is_greater) |
| 146 | + assert str(only_if_previous) == "OnlyIfPrevious:is_greater" |
| 147 | + assert ( |
| 148 | + repr(only_if_previous) == f"<OnlyIfPrevious: {is_greater!r} first_is_true=True>" |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def test_only_if_previous_sentinel_str() -> None: |
| 153 | + """Test the string representation of the sentinel value.""" |
| 154 | + only_if_previous = OnlyIfPrevious(always_true) |
| 155 | + |
| 156 | + # Access the private attribute for testing purposes |
| 157 | + # pylint: disable=protected-access |
| 158 | + assert str(only_if_previous._last_message) == "<no value received yet>" |
0 commit comments