
    nic&                         S r SSKrSSKJrJr  SSKr " S S5      r " S S5      r " S S	5      r	 " S
 S5      r
 " S S5      rg)z
Unit tests for pyreason global state management and reset functions.
Tests the critical reset(), reset_rules(), reset_settings() functions and global variable handling.
    N)patch	MagicMockc                   X    \ rS rSrSrS r\" S5      S 5       rS rS r	S r
S	 rS
 rSrg)TestResetFunction   zTest the main reset() function.c                 X    [         R                  " 5         [         R                  " 5         gzClean state before each test.Nprresetreset_settingsselfs    h/home/james-whalen/.local/lib/python3.13/site-packages/tests/api_tests/test_pyreason_state_management.pysetup_methodTestResetFunction.setup_method       


    pyreason.pyreason.__programc                    [        5       nX!l        [        SU5         [        R                  " 5         UR
                  R                  5         UR                  R                  5         SSS5        g! , (       d  f       g= f)z=Test reset() calls program reset methods when program exists.r   N)r   return_valuer   r   r   reset_factsassert_called_oncereset_graphr   mock_programmock_program_instances      r   4test_reset_calls_program_methods_when_program_existsFTestResetFunction.test_reset_calls_program_methods_when_program_exists   s\     !*$9! 02GHHHJ "--@@B!--@@B IHHs   A
A00
A>c                 .    [         R                  " 5         g)z-Test reset() handles None program gracefully.Nr   r   r   s    r   test_reset_handles_none_program1TestResetFunction.test_reset_handles_none_program"   s     	
r   c                     [        S5       n[        R                  " 5         UR                  5         SSS5        g! , (       d  f       g= f)z&Test that reset() calls reset_rules().zpyreason.pyreason.reset_rulesN)r   r   r   r   )r   mock_reset_ruless     r   test_reset_calls_reset_rules.TestResetFunction.test_reset_calls_reset_rules'   s/    237GHHJ//1 433s	   &;
A	c                 .    [         R                  " 5         g)z6Test that reset() clears __node_facts global variable.Nr!   r   s    r   #test_reset_clears_node_facts_global5TestResetFunction.test_reset_clears_node_facts_global-   s     	
r   c                 .    [         R                  " 5         g)z6Test that reset() clears __edge_facts global variable.Nr!   r   s    r   #test_reset_clears_edge_facts_global5TestResetFunction.test_reset_clears_edge_facts_global3       

r   c                 .    [         R                  " 5         g)z1Test that reset() clears __graph global variable.Nr!   r   s    r   test_reset_clears_graph_global0TestResetFunction.test_reset_clears_graph_global7   r.   r    N)__name__
__module____qualname____firstlineno____doc__r   r   r   r"   r&   r)   r,   r0   __static_attributes__r2   r   r   r   r      s?    )
 ()C *C
2r   r   c                   X    \ rS rSrSrS rS r\" S5      S 5       rS r	S r
S	 rS
 rSrg)TestResetRulesFunction<   zQTest the reset_rules() function - includes critical annotation_functions bug fix.c                 X    [         R                  " 5         [         R                  " 5         gr	   r
   r   s    r   r   #TestResetRulesFunction.setup_method?        	

r   c                 .    [         R                  " 5         g)z#Test reset_rules() basic operation.Nr   reset_rulesr   s    r   $test_reset_rules_basic_functionality;TestResetRulesFunction.test_reset_rules_basic_functionalityE        	r   r   c                     [        5       n[        SU5         [        R                  " 5         UR                  R	                  5         SSS5        g! , (       d  f       g= f)zCTest reset_rules() calls program.reset_rules() when program exists.r   N)r   r   r   rA   r   r   s      r   8test_reset_rules_calls_program_reset_when_program_existsOTestResetRulesFunction.test_reset_rules_calls_program_reset_when_program_existsJ   sA     !*02GHNN!--@@B IHHs   0A
Ac                 .    [         R                  " 5         g)z3Test reset_rules() handles None program gracefully.Nr@   r   s    r   %test_reset_rules_handles_none_program<TestResetRulesFunction.test_reset_rules_handles_none_programS   rD   r   c                 `    S n[         R                  " U5        [         R                  " 5         g )Nc                     gN)      ?rN   r2   annotationsweightss     r   test_annotation_funcaTestResetRulesFunction.test_reset_rules_clears_annotation_functions.<locals>.test_annotation_funcZ       r   r   add_annotation_functionrA   )r   rR   s     r   ,test_reset_rules_clears_annotation_functionsCTestResetRulesFunction.test_reset_rules_clears_annotation_functionsX   s"    	
""#78 	r   c                     S n[         R                  " U5        [         R                  " 5         S n[         R                  " U5        [         R                  " 5         g)z}
Test that annotation functions are properly isolated between reset_rules() calls.
This ensures the bug fix works correctly.
c                     gN)皙?r\   r2   rO   s     r   func1XTestResetRulesFunction.test_annotation_functions_isolation_between_resets.<locals>.func1h   rT   r   c                     gN)皙?ra   r2   rO   s     r   func2XTestResetRulesFunction.test_annotation_functions_isolation_between_resets.<locals>.func2q   rT   r   NrU   )r   r]   rb   s      r   2test_annotation_functions_isolation_between_resetsITestResetRulesFunction.test_annotation_functions_isolation_between_resetsa   sD    	 	""5) 		 	""5) 	r   c                     S nS nS n[         R                  " U5        [         R                  " U5        [         R                  " U5        [         R                  " 5         g)z6Test reset_rules() with multiple annotation functions.c                     gr[   r2   rO   s     r   r]   NTestResetRulesFunction.test_multiple_annotation_functions_reset.<locals>.func1~   rT   r   c                     gr`   r2   rO   s     r   rb   NTestResetRulesFunction.test_multiple_annotation_functions_reset.<locals>.func2   rT   r   c                     g)N)333333?rl   r2   rO   s     r   func3NTestResetRulesFunction.test_multiple_annotation_functions_reset.<locals>.func3   rT   r   NrU   )r   r]   rb   rm   s       r   (test_multiple_annotation_functions_reset?TestResetRulesFunction.test_multiple_annotation_functions_reset{   sI    			 	""5)
""5)
""5) 	r   r2   N)r3   r4   r5   r6   r7   r   rB   r   rF   rI   rW   rd   ro   r8   r2   r   r   r:   r:   <   s?    [
 ()C *C
4r   r:   c                   $    \ rS rSrSrS rS rSrg)TestResetSettingsFunction   z#Test the reset_settings() function.c                     [         R                  " [        R                  S5       n[        R                  " 5         UR                  5         SSS5        g! , (       d  f       g= f)z2Test that reset_settings() calls settings.reset().r   N)r   objectr   settingsr   r   )r   
mock_resets     r   (test_reset_settings_calls_settings_resetBTestResetSettingsFunction.test_reset_settings_calls_settings_reset   s:    \\"++w/:))+ 0//s   &A
A#c                 h   S[         R                  l        S[         R                  l        S[         R                  l        [         R
                  " 5         [         R                  R                  SL d   e[         R                  R                  SL d   e[         R                  R                  S:X  d   eg)z<Test that reset_settings() actually restores default values.FTcustompyreason_outputN)r   rv   verbosememory_profileoutput_file_namer   r   s    r   %test_reset_settings_restores_defaults?TestResetSettingsFunction.test_reset_settings_restores_defaults   s     $%)"'/$ 	 {{""d***{{))U222{{++/@@@@r   r2   N)r3   r4   r5   r6   r7   rx   r   r8   r2   r   r   rr   rr      s    -,Ar   rr   c                   <    \ rS rSrSrS rS rS rS rS r	S r
S	rg
)TestGlobalStateManagement   z&Test global variable state management.c                 X    [         R                  " 5         [         R                  " 5         gr	   r
   r   s    r   r   &TestGlobalStateManagement.setup_method   r   r   c                     [        [        S5      (       a9  [        R                  c  [        R                  b   eg[        R                  c   egg)z4Test that torch integration variables are consistentLogicIntegratedClassifierN)hasattrr   r   ModelInterfaceOptionsr   s    r   "test_torch_integration_consistency<TestGlobalStateManagement.test_torch_integration_consistency   sI     2233++3//777 //;;; 4r   c                     [         R                  " 5         [         R                  " 5         [         R                  " 5         g)z8Test that state is properly isolated between operations.Nr   r   rA   r   r   s    r   'test_state_isolation_between_operationsATestGlobalStateManagement.test_state_isolation_between_operations   s'     	
 	 	r   c                    [         R                  " 5         [         R                  " 5         S n[         R                  " U5        [         R                  " 5         S n[         R                  " U5        [         R                  " 5         g)z
Test annotation functions state consistency across operations.
This is a comprehensive test for the annotation_functions bug fix.
c                     grM   r2   rO   s     r   	test_funcXTestGlobalStateManagement.test_annotation_functions_state_consistency.<locals>.test_func   rT   r   c                     g)N)皙?r   r2   rO   s     r   
test_func2YTestGlobalStateManagement.test_annotation_functions_state_consistency.<locals>.test_func2   rT   r   N)r   r   rA   rV   )r   r   r   s      r   +test_annotation_functions_state_consistencyETestGlobalStateManagement.test_annotation_functions_state_consistency   sY     	

	 	""9- 		 	"":. 	r   c                     [         R                  " 5         [         R                  " 5         [         R                  " 5         g)z"Test comprehensive reset sequence.Nr   r   s    r   !test_reset_sequence_comprehensive;TestGlobalStateManagement.test_reset_sequence_comprehensive   s#     	


r   c                     [        S5       HB  n[        R                  " 5         [        R                  " 5         [        R                  " 5         MD     g)z:Test that repeated resets are safe and don't cause issues.   N)ranger   r   rA   r   )r   _s     r   test_repeated_resets_are_safe7TestGlobalStateManagement.test_repeated_resets_are_safe   s1     qAHHJNN r   r2   N)r3   r4   r5   r6   r7   r   r   r   r   r   r   r8   r2   r   r   r   r      s#    0
	< > r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	TestStateConsistency   z3Test state consistency across different operations.c                 X    [         R                  " 5         [         R                  " 5         gr	   r
   r   s    r   r   !TestStateConsistency.setup_method   r>   r   c                     S[         R                  l        [         R                  " 5         [         R                  R                  SL d   e[         R                  " 5         [         R                  R                  SL d   eg)zMTest that settings changes persist across reset() (but not reset_settings()).TN)r   rv   r}   r   r   r   s    r   "test_settings_persist_across_reset7TestStateConsistency.test_settings_persist_across_reset  s[     # 	
 {{""d*** 	{{""d***r   c                     S[         R                  l        [         R                  " 5         [         R                  R                  SL d   eg)zATest that reset_rules() is independent of other reset operations.TN)r   rv   r}   rA   r   s    r   test_rules_reset_independence2TestStateConsistency.test_rules_reset_independence  s6     # 	 {{""d***r   c                    [         R                  " 5         [         R                  " 5         [         R                  R                  SL d   e[         R                  R
                  SL d   e[         R                  R                  S:X  d   eg)z6Test the complete cleanup sequence for test isolation.TFr|   N)r   r   r   rv   r}   r~   r   r   s    r   test_full_cleanup_sequence/TestStateConsistency.test_full_cleanup_sequence  sd    
 	

 {{""d***{{))U222{{++/@@@@r   r2   N)
r3   r4   r5   r6   r7   r   r   r   r   r8   r2   r   r   r   r      s    =+	+Ar   r   )r7   pytestunittest.mockr   r   pyreasonr   r   r:   rr   r   r   r2   r   r   <module>r      sR   
  * . .bP PfA A2Q  Q h-A -Ar   