|
| 1 | +from zephyr_ml.core import GuideHandler, guide |
| 2 | + |
| 3 | + |
| 4 | +class DummyObject: |
| 5 | + def __init__(self): |
| 6 | + producers_and_getters = [ |
| 7 | + ([self.step0_key, self.step0_set], [self.step0_getter]), |
| 8 | + ([self.step1_key, self.step1_set], [self.step1_getter]), |
| 9 | + ([self.step2_key, self.step2_set], [self.step2_getter]) |
| 10 | + ] |
| 11 | + set_methods = { |
| 12 | + self.step0_set.__name__, |
| 13 | + self.step1_set.__name__, |
| 14 | + self.step2_set.__name__ |
| 15 | + } |
| 16 | + self._guide_handler = GuideHandler(producers_and_getters, set_methods) |
| 17 | + |
| 18 | + @guide |
| 19 | + def step0_key(self): |
| 20 | + return "step0_key_result" |
| 21 | + |
| 22 | + @guide |
| 23 | + def step0_set(self): |
| 24 | + return "step0_set_result" |
| 25 | + |
| 26 | + @guide |
| 27 | + def step0_getter(self): |
| 28 | + return "step0_get_result" |
| 29 | + |
| 30 | + @guide |
| 31 | + def step1_key(self): |
| 32 | + return "step1_key_result" |
| 33 | + |
| 34 | + @guide |
| 35 | + def step1_set(self): |
| 36 | + return "step1_set_result" |
| 37 | + |
| 38 | + @guide |
| 39 | + def step1_getter(self): |
| 40 | + return "step1_get_result" |
| 41 | + |
| 42 | + @guide |
| 43 | + def step2_key(self): |
| 44 | + return "step2_key_result" |
| 45 | + |
| 46 | + @guide |
| 47 | + def step2_set(self): |
| 48 | + return "step2_set_result" |
| 49 | + |
| 50 | + @guide |
| 51 | + def step2_getter(self): |
| 52 | + return "step2_get_result" |
| 53 | + |
| 54 | + |
| 55 | +def test_forward_key_steps(): |
| 56 | + """Test performing key steps in forward order""" |
| 57 | + obj = DummyObject() |
| 58 | + |
| 59 | + # First step should work without warnings |
| 60 | + assert obj.step0_key() == "step0_key_result" |
| 61 | + |
| 62 | + # Second step should work without warnings since previous step is up to |
| 63 | + # date |
| 64 | + assert obj.step1_key() == "step1_key_result" |
| 65 | + |
| 66 | + # Third step should work without warnings since previous step is up to date |
| 67 | + assert obj.step2_key() == "step2_key_result" |
| 68 | + |
| 69 | + |
| 70 | +def test_set_methods_can_skip(caplog): |
| 71 | + """Test that set methods can skip steps""" |
| 72 | + obj = DummyObject() |
| 73 | + |
| 74 | + # Set methods should work in any order and start new iterations |
| 75 | + assert obj.step2_set() == "step2_set_result" # Skip to step 2 |
| 76 | + assert "[GUIDE] STALE WARNING" in caplog.text |
| 77 | + |
| 78 | + assert obj.step0_set() == "step0_set_result" # Go back to step 0 |
| 79 | + assert "[GUIDE] STALE WARNING" in caplog.text |
| 80 | + assert obj.step1_set() == "step1_set_result" # Do step 1 |
| 81 | + assert "[GUIDE] STALE WARNING" in caplog.text |
| 82 | + |
| 83 | + |
| 84 | +def test_key_methods_require_previous_step(caplog): |
| 85 | + """Test that key methods require the previous step to be up to date""" |
| 86 | + obj = DummyObject() |
| 87 | + |
| 88 | + # Try to do step 1 without doing step 0 first |
| 89 | + obj.step1_key() |
| 90 | + assert "[GUIDE] INCONSISTENCY WARNING" in caplog.text |
| 91 | + |
| 92 | + # Do step 0, then step 1 should work |
| 93 | + obj.step0_key() |
| 94 | + assert obj.step1_key() == "step1_key_result" |
| 95 | + |
| 96 | + |
| 97 | +def test_stale_data_warning(caplog): |
| 98 | + """Test warning when data becomes stale""" |
| 99 | + obj = DummyObject() |
| 100 | + |
| 101 | + # Complete steps 0 and 1 |
| 102 | + obj.step0_key() |
| 103 | + obj.step1_key() |
| 104 | + |
| 105 | + # Go back to step 0 with set method (allowed, but warns about stale data) |
| 106 | + obj.step0_set() |
| 107 | + assert "[GUIDE] STALE WARNING" in caplog.text |
| 108 | + |
| 109 | + |
| 110 | +def test_getter_with_stale_data(caplog): |
| 111 | + """Test getting data that may be stale""" |
| 112 | + obj = DummyObject() |
| 113 | + |
| 114 | + # Complete steps 0 and 1 |
| 115 | + obj.step0_key() |
| 116 | + obj.step1_key() |
| 117 | + |
| 118 | + # Go back to step 0 with set method |
| 119 | + obj.step0_set() |
| 120 | + |
| 121 | + # Try to get data from step 1, should warn about stale data |
| 122 | + obj.step1_getter() |
| 123 | + assert "[GUIDE] STALE WARNING" in caplog.text |
| 124 | + |
| 125 | + |
| 126 | +def test_getter_with_missing_key(caplog): |
| 127 | + """Test getting data when the key method hasn't been run""" |
| 128 | + obj = DummyObject() |
| 129 | + |
| 130 | + # Try to get data without running key method first |
| 131 | + obj.step1_getter() |
| 132 | + assert "[GUIDE] INCONSISTENCY WARNING" in caplog.text |
| 133 | + |
| 134 | + |
| 135 | +def test_key_method_after_stale_data(caplog): |
| 136 | + """Test that key methods cannot be run when previous step is stale""" |
| 137 | + obj = DummyObject() |
| 138 | + |
| 139 | + # Complete steps 0 and 1 |
| 140 | + obj.step0_key() |
| 141 | + obj.step1_key() |
| 142 | + |
| 143 | + # Go back to step 0 with set method |
| 144 | + obj.step1_set() |
| 145 | + obj.step1_key() |
| 146 | + assert "[GUIDE] INCONSISTENCY WARNING" in caplog.text |
| 147 | + |
| 148 | + |
| 149 | +def test_multiple_iterations(): |
| 150 | + """Test multiple iterations through the steps""" |
| 151 | + obj = DummyObject() |
| 152 | + |
| 153 | + # First iteration with key methods |
| 154 | + assert obj.step0_key() == "step0_key_result" |
| 155 | + assert obj.step1_key() == "step1_key_result" |
| 156 | + |
| 157 | + # Second iteration starting with set method |
| 158 | + assert obj.step0_set() == "step0_set_result" |
| 159 | + # Can't do step 1 with key method after set without redoing step 0 key |
| 160 | + assert obj.step1_key() == "step1_key_result" |
| 161 | + |
| 162 | + |
| 163 | +def test_guide_decorator(): |
| 164 | + """Test that the guide decorator properly wraps methods""" |
| 165 | + obj = DummyObject() |
| 166 | + |
| 167 | + # Check that the decorator preserves function metadata |
| 168 | + assert obj.step0_key.__name__ == "step0_key" |
| 169 | + assert obj.step0_getter.__name__ == "step0_getter" |
| 170 | + |
| 171 | + # Check that the decorator routes through the guide handler |
| 172 | + assert obj.step0_key() == "step0_key_result" |
| 173 | + assert obj.step0_getter() == "step0_get_result" |
0 commit comments