-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtest_pyreason_state_management.py
More file actions
346 lines (259 loc) · 11.2 KB
/
test_pyreason_state_management.py
File metadata and controls
346 lines (259 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""
Unit tests for pyreason global state management and reset functions.
Tests the critical reset(), reset_rules(), reset_settings() functions and global variable handling.
"""
import pytest
from unittest.mock import patch, MagicMock
import pyreason as pr
import pyreason.pyreason as pr_mod
class TestResetFunction:
"""Test the main reset() function."""
def setup_method(self):
"""Clean state before each test."""
pr.reset()
pr.reset_settings()
@patch('pyreason.pyreason.__program')
def test_reset_calls_program_methods_when_program_exists(self, mock_program):
"""Test reset() calls program reset methods when program exists."""
# Mock program exists
mock_program_instance = MagicMock()
mock_program.return_value = mock_program_instance
# Make __program not None
with patch('pyreason.pyreason.__program', mock_program_instance):
pr.reset()
# Verify program reset methods were called
mock_program_instance.reset_facts.assert_called_once()
mock_program_instance.reset_graph.assert_called_once()
def test_reset_handles_none_program(self):
"""Test reset() handles None program gracefully."""
# Should not raise any exceptions
pr.reset()
def test_reset_calls_reset_rules(self):
"""Test that reset() calls reset_rules()."""
with patch('pyreason.pyreason.reset_rules') as mock_reset_rules:
pr.reset()
mock_reset_rules.assert_called_once()
def test_reset_clears_node_facts_global(self):
"""Test that reset() clears __node_facts global variable."""
# Note: We can't easily test the actual global variable state
# without complex mocking, but we can test the function doesn't crash
pr.reset()
def test_reset_clears_edge_facts_global(self):
"""Test that reset() clears __edge_facts global variable."""
pr.reset()
def test_reset_clears_graph_global(self):
"""Test that reset() clears __graph global variable."""
pr.reset()
class TestResetRulesFunction:
"""Test the reset_rules() function - includes critical annotation_functions bug fix."""
def setup_method(self):
"""Clean state before each test."""
pr.reset()
pr.reset_settings()
def test_reset_rules_basic_functionality(self):
"""Test reset_rules() basic operation."""
# Should not raise exceptions
pr.reset_rules()
@patch('pyreason.pyreason.__program')
def test_reset_rules_calls_program_reset_when_program_exists(self, mock_program):
"""Test reset_rules() calls program.reset_rules() when program exists."""
mock_program_instance = MagicMock()
with patch('pyreason.pyreason.__program', mock_program_instance):
pr.reset_rules()
mock_program_instance.reset_rules.assert_called_once()
def test_reset_rules_handles_none_program(self):
"""Test reset_rules() handles None program gracefully."""
# Should not raise any exceptions
pr.reset_rules()
def test_reset_rules_clears_annotation_functions(self):
# Add an annotation function
def test_annotation_func(annotations, weights):
return 0.5, 0.5
pr.add_annotation_function(test_annotation_func)
# Reset rules should clear annotation functions
pr.reset_rules()
def test_annotation_functions_isolation_between_resets(self):
"""
Test that annotation functions are properly isolated between reset_rules() calls.
This ensures the bug fix works correctly.
"""
# Add annotation function
def func1(annotations, weights):
return 0.1, 0.1
pr.add_annotation_function(func1)
# Reset
pr.reset_rules()
# Add a different annotation function
def func2(annotations, weights):
return 0.2, 0.2
pr.add_annotation_function(func2)
# Reset again
pr.reset_rules()
# Should work without Numba typing errors
def test_multiple_annotation_functions_reset(self):
"""Test reset_rules() with multiple annotation functions."""
# Add multiple annotation functions
def func1(annotations, weights):
return 0.1, 0.1
def func2(annotations, weights):
return 0.2, 0.2
def func3(annotations, weights):
return 0.3, 0.3
pr.add_annotation_function(func1)
pr.add_annotation_function(func2)
pr.add_annotation_function(func3)
# Reset should clear all
pr.reset_rules()
class TestResetSettingsFunction:
"""Test the reset_settings() function."""
def test_reset_settings_calls_settings_reset(self):
"""Test that reset_settings() calls settings.reset()."""
with patch.object(pr.settings, 'reset') as mock_reset:
pr.reset_settings()
mock_reset.assert_called_once()
def test_reset_settings_restores_defaults(self):
"""Test that reset_settings() actually restores default values."""
# Change settings
pr.settings.verbose = False
pr.settings.memory_profile = True
pr.settings.output_file_name = "custom"
# Reset
pr.reset_settings()
# Verify defaults restored
assert pr.settings.verbose is True
assert pr.settings.memory_profile is False
assert pr.settings.output_file_name == "pyreason_output"
class TestGlobalStateManagement:
"""Test global variable state management."""
def setup_method(self):
"""Clean state before each test."""
pr.reset()
pr.reset_settings()
def test_torch_integration_consistency(self):
"""Test that torch integration variables are consistent"""
# Just verify the current state is consistent
if hasattr(pr, 'LogicIntegratedClassifier'):
if pr.LogicIntegratedClassifier is None:
# If LogicIntegratedClassifier is None, ModelInterfaceOptions should also be None
assert pr.ModelInterfaceOptions is None
else:
# If LogicIntegratedClassifier exists, ModelInterfaceOptions should also exist
assert pr.ModelInterfaceOptions is not None
def test_state_isolation_between_operations(self):
"""Test that state is properly isolated between operations."""
# This test verifies that subsequent operations don't interfere
# with each other due to global state pollution
# First operation
pr.reset()
# Second operation
pr.reset_rules()
# Third operation
pr.reset_settings()
# Should all work without issues
def test_annotation_functions_state_consistency(self):
"""
Test annotation functions state consistency across operations.
This is a comprehensive test for the annotation_functions bug fix.
"""
# Test sequence that previously caused Numba typing errors
# 1. Start clean
pr.reset()
pr.reset_rules()
# 2. Add annotation function
def test_func(annotations, weights):
return 0.5, 0.5
pr.add_annotation_function(test_func)
# 3. Reset (should clear annotation functions)
pr.reset_rules()
# 4. Add different annotation function
def test_func2(annotations, weights):
return 0.8, 0.8
pr.add_annotation_function(test_func2)
# 5. Reset again
pr.reset_rules()
# This sequence should work without Numba typing errors
def test_reset_sequence_comprehensive(self):
"""Test comprehensive reset sequence."""
# Test the full reset sequence
pr.reset() # Reset main state
pr.reset_rules() # Reset rules and annotation functions
pr.reset_settings() # Reset settings
# Should be in clean state
def test_repeated_resets_are_safe(self):
"""Test that repeated resets are safe and don't cause issues."""
# Multiple resets should be safe
for _ in range(5):
pr.reset()
pr.reset_rules()
pr.reset_settings()
class TestStateConsistency:
"""Test state consistency across different operations."""
def setup_method(self):
"""Clean state before each test."""
pr.reset()
pr.reset_settings()
def test_settings_persist_across_reset(self):
"""Test that settings changes persist across reset() (but not reset_settings())."""
# Change a setting
pr.settings.verbose = True
# Call reset() (not reset_settings())
pr.reset()
# Setting should still be changed
assert pr.settings.verbose is True
# But reset_settings() should restore it
pr.reset_settings()
assert pr.settings.verbose is True
def test_rules_reset_independence(self):
"""Test that reset_rules() is independent of other reset operations."""
# Change settings
pr.settings.verbose = True
# Reset rules
pr.reset_rules()
# Settings should be unchanged
assert pr.settings.verbose is True
def test_full_cleanup_sequence(self):
"""Test the complete cleanup sequence for test isolation."""
# This is the sequence that should be used for test cleanup
# to ensure complete state isolation
pr.reset() # Clear main state
pr.reset_settings() # Reset settings to defaults
# Verify clean state
assert pr.settings.verbose is True
assert pr.settings.memory_profile is False
assert pr.settings.output_file_name == "pyreason_output"
def _get_closed_world():
"""Access the internal closed_world predicates set."""
return pr_mod.__dict__['__closed_world_predicates']
class Testclosed_worldPredicateApi:
"""Test add_closed_world_predicate() and its interaction with reset functions."""
def setup_method(self):
pr.reset()
pr.reset_rules()
def teardown_method(self):
pr.reset()
pr.reset_rules()
def test_add_closed_world_predicate_registers(self):
pr.add_closed_world_predicate('hackerControl')
assert 'hackerControl' in _get_closed_world()
def test_add_multiple_closed_world_predicates(self):
pr.add_closed_world_predicate('pred_a')
pr.add_closed_world_predicate('pred_b')
pr.add_closed_world_predicate('pred_c')
assert {'pred_a', 'pred_b', 'pred_c'} == _get_closed_world()
def test_add_duplicate_is_idempotent(self):
pr.add_closed_world_predicate('pred')
pr.add_closed_world_predicate('pred')
assert _get_closed_world() == {'pred'}
def test_reset_clears_closed_world_predicates(self):
pr.add_closed_world_predicate('pred_a')
pr.add_closed_world_predicate('pred_b')
pr.reset()
assert len(_get_closed_world()) == 0
def test_reset_rules_does_not_clear_closed_world_predicates(self):
pr.add_closed_world_predicate('pred')
pr.reset_rules()
assert 'pred' in _get_closed_world()
def test_reset_settings_does_not_clear_closed_world_predicates(self):
pr.add_closed_world_predicate('pred')
pr.reset_settings()
assert 'pred' in _get_closed_world()