|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import datetime |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ttsim.interface_dag_elements.dates import ( |
| 8 | + evaluation_date_from_evaluation_date_str, |
| 9 | + evaluation_date_use_other_info, |
| 10 | + policy_date, |
| 11 | +) |
| 12 | +from ttsim.interface_dag_elements.interface_node_objects import ( |
| 13 | + InputDependentInterfaceFunction, |
| 14 | + InterfaceFunction, |
| 15 | + InterfaceInput, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +# ============================================================================= |
| 20 | +# policy_date tests |
| 21 | +# ============================================================================= |
| 22 | +def test_policy_date_is_interface_function(): |
| 23 | + assert isinstance(policy_date, InterfaceFunction) |
| 24 | + |
| 25 | + |
| 26 | +def test_policy_date_from_valid_string(): |
| 27 | + result = policy_date("2024-06-15") |
| 28 | + assert result == datetime.date(2024, 6, 15) |
| 29 | + |
| 30 | + |
| 31 | +def test_policy_date_from_leap_year_date(): |
| 32 | + result = policy_date("2020-02-29") |
| 33 | + assert result == datetime.date(2020, 2, 29) |
| 34 | + |
| 35 | + |
| 36 | +def test_policy_date_in_top_level_namespace(): |
| 37 | + assert policy_date.in_top_level_namespace is True |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize( |
| 41 | + "date_str", |
| 42 | + [ |
| 43 | + "2024-01-01", |
| 44 | + "1990-12-31", |
| 45 | + "2000-06-15", |
| 46 | + ], |
| 47 | +) |
| 48 | +def test_policy_date_various_valid_dates(date_str): |
| 49 | + result = policy_date(date_str) |
| 50 | + year, month, day = map(int, date_str.split("-")) |
| 51 | + assert result == datetime.date(year, month, day) |
| 52 | + |
| 53 | + |
| 54 | +def test_policy_date_invalid_date_raises(): |
| 55 | + with pytest.raises(ValueError): |
| 56 | + policy_date("2024-02-30") # Invalid date |
| 57 | + |
| 58 | + |
| 59 | +# ============================================================================= |
| 60 | +# evaluation_date_use_other_info tests |
| 61 | +# ============================================================================= |
| 62 | +def test_evaluation_date_use_other_info_is_input_dependent(): |
| 63 | + assert isinstance(evaluation_date_use_other_info, InputDependentInterfaceFunction) |
| 64 | + |
| 65 | + |
| 66 | +def test_evaluation_date_use_other_info_returns_none(): |
| 67 | + result = evaluation_date_use_other_info(backend="numpy") |
| 68 | + assert result is None |
| 69 | + |
| 70 | + |
| 71 | +def test_evaluation_date_use_other_info_with_jax_backend(): |
| 72 | + result = evaluation_date_use_other_info(backend="jax") |
| 73 | + assert result is None |
| 74 | + |
| 75 | + |
| 76 | +def test_evaluation_date_use_other_info_has_correct_leaf_name(): |
| 77 | + assert evaluation_date_use_other_info.leaf_name == "evaluation_date" |
| 78 | + |
| 79 | + |
| 80 | +def test_evaluation_date_use_other_info_in_top_level_namespace(): |
| 81 | + assert evaluation_date_use_other_info.in_top_level_namespace is True |
| 82 | + |
| 83 | + |
| 84 | +def test_evaluation_date_use_other_info_include_condition(): |
| 85 | + """This function should be included when evaluation_date_str is NOT present.""" |
| 86 | + assert evaluation_date_use_other_info.include_if_no_input_present == [ |
| 87 | + "evaluation_date_str" |
| 88 | + ] |
| 89 | + assert not evaluation_date_use_other_info.include_if_all_inputs_present |
| 90 | + assert not evaluation_date_use_other_info.include_if_any_input_present |
| 91 | + |
| 92 | + |
| 93 | +def test_evaluation_date_use_other_info_condition_satisfied_when_no_eval_date_str(): |
| 94 | + """Condition is satisfied when evaluation_date_str is not in input names.""" |
| 95 | + result = evaluation_date_use_other_info.include_condition_satisfied( |
| 96 | + ["backend", "policy_date_str"] |
| 97 | + ) |
| 98 | + assert result is True |
| 99 | + |
| 100 | + |
| 101 | +def test_evaluation_date_use_other_info_condition_not_satisfied_when_eval_date_str(): |
| 102 | + """Condition is not satisfied when evaluation_date_str is in input names.""" |
| 103 | + result = evaluation_date_use_other_info.include_condition_satisfied( |
| 104 | + ["backend", "evaluation_date_str"] |
| 105 | + ) |
| 106 | + assert result is False |
| 107 | + |
| 108 | + |
| 109 | +# ============================================================================= |
| 110 | +# evaluation_date_from_evaluation_date_str tests |
| 111 | +# ============================================================================= |
| 112 | +def test_evaluation_date_from_str_is_input_dependent(): |
| 113 | + assert isinstance( |
| 114 | + evaluation_date_from_evaluation_date_str, InputDependentInterfaceFunction |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def test_evaluation_date_from_str_returns_date(): |
| 119 | + result = evaluation_date_from_evaluation_date_str("2024-06-15") |
| 120 | + assert result == datetime.date(2024, 6, 15) |
| 121 | + |
| 122 | + |
| 123 | +def test_evaluation_date_from_str_has_correct_leaf_name(): |
| 124 | + assert evaluation_date_from_evaluation_date_str.leaf_name == "evaluation_date" |
| 125 | + |
| 126 | + |
| 127 | +def test_evaluation_date_from_str_in_top_level_namespace(): |
| 128 | + assert evaluation_date_from_evaluation_date_str.in_top_level_namespace is True |
| 129 | + |
| 130 | + |
| 131 | +def test_evaluation_date_from_str_include_condition(): |
| 132 | + """This function should be included when evaluation_date_str IS present.""" |
| 133 | + assert evaluation_date_from_evaluation_date_str.include_if_all_inputs_present == [ |
| 134 | + "evaluation_date_str" |
| 135 | + ] |
| 136 | + assert not evaluation_date_from_evaluation_date_str.include_if_no_input_present |
| 137 | + assert not evaluation_date_from_evaluation_date_str.include_if_any_input_present |
| 138 | + |
| 139 | + |
| 140 | +def test_evaluation_date_from_str_condition_satisfied_when_eval_date_str_present(): |
| 141 | + """Condition is satisfied when evaluation_date_str is in input names.""" |
| 142 | + result = evaluation_date_from_evaluation_date_str.include_condition_satisfied( |
| 143 | + ["backend", "evaluation_date_str"] |
| 144 | + ) |
| 145 | + assert result is True |
| 146 | + |
| 147 | + |
| 148 | +def test_evaluation_date_from_str_condition_not_satisfied_when_no_eval_date_str(): |
| 149 | + """Condition is not satisfied when evaluation_date_str is not in input names.""" |
| 150 | + result = evaluation_date_from_evaluation_date_str.include_condition_satisfied( |
| 151 | + ["backend", "policy_date_str"] |
| 152 | + ) |
| 153 | + assert result is False |
| 154 | + |
| 155 | + |
| 156 | +# ============================================================================= |
| 157 | +# Input definitions tests |
| 158 | +# ============================================================================= |
| 159 | +def test_policy_date_str_is_interface_input(): |
| 160 | + from ttsim.interface_dag_elements.dates import policy_date_str |
| 161 | + |
| 162 | + assert isinstance(policy_date_str, InterfaceInput) |
| 163 | + assert policy_date_str.in_top_level_namespace is True |
| 164 | + |
| 165 | + |
| 166 | +def test_evaluation_date_str_is_interface_input(): |
| 167 | + from ttsim.interface_dag_elements.dates import evaluation_date_str |
| 168 | + |
| 169 | + assert isinstance(evaluation_date_str, InterfaceInput) |
| 170 | + assert evaluation_date_str.in_top_level_namespace is True |
| 171 | + |
| 172 | + |
| 173 | +# ============================================================================= |
| 174 | +# Mutual exclusivity tests |
| 175 | +# ============================================================================= |
| 176 | +def test_evaluation_date_functions_are_mutually_exclusive(): |
| 177 | + """Only one evaluation_date function can be included at a time.""" |
| 178 | + # When evaluation_date_str is present |
| 179 | + inputs_with_eval_str = ["backend", "evaluation_date_str", "policy_date_str"] |
| 180 | + |
| 181 | + use_other_satisfied = evaluation_date_use_other_info.include_condition_satisfied( |
| 182 | + inputs_with_eval_str |
| 183 | + ) |
| 184 | + from_str_satisfied = ( |
| 185 | + evaluation_date_from_evaluation_date_str.include_condition_satisfied( |
| 186 | + inputs_with_eval_str |
| 187 | + ) |
| 188 | + ) |
| 189 | + |
| 190 | + # Only from_str should be satisfied |
| 191 | + assert from_str_satisfied is True |
| 192 | + assert use_other_satisfied is False |
| 193 | + |
| 194 | + # When evaluation_date_str is NOT present |
| 195 | + inputs_without_eval_str = ["backend", "policy_date_str"] |
| 196 | + |
| 197 | + use_other_satisfied = evaluation_date_use_other_info.include_condition_satisfied( |
| 198 | + inputs_without_eval_str |
| 199 | + ) |
| 200 | + from_str_satisfied = ( |
| 201 | + evaluation_date_from_evaluation_date_str.include_condition_satisfied( |
| 202 | + inputs_without_eval_str |
| 203 | + ) |
| 204 | + ) |
| 205 | + |
| 206 | + # Only use_other should be satisfied |
| 207 | + assert use_other_satisfied is True |
| 208 | + assert from_str_satisfied is False |
0 commit comments