1+ # Generated by Claude Sonnet 4
2+ import logging
3+ import time
4+ from unittest .mock import patch
5+
6+ import pytest
7+ from django .test import TestCase , override_settings
8+
9+ from ansible_base .lib .utils .performance import (
10+ enable_performance_logging ,
11+ measure_post_migrate_signal ,
12+ measure_time ,
13+ patch_post_migrate_handlers ,
14+ )
15+
16+
17+ class TestPerformanceMeasurement (TestCase ):
18+ """Test the performance measurement utilities."""
19+
20+ def test_measure_time_context_manager (self ):
21+ """Test that measure_time context manager logs execution time."""
22+ with self .assertLogs ('ansible_base.performance' , level = 'INFO' ) as cm :
23+ with measure_time ("test_operation" ):
24+ time .sleep (0.01 ) # Small delay to ensure measurable time
25+
26+ self .assertEqual (len (cm .output ), 1 )
27+ self .assertIn ("[PERFORMANCE] test_operation:" , cm .output [0 ])
28+ # Should show some positive time duration
29+ self .assertRegex (cm .output [0 ], r'\d+\.\d{4}s' )
30+
31+ def test_measure_post_migrate_signal_decorator (self ):
32+ """Test that the post_migrate signal decorator works correctly."""
33+
34+ @measure_post_migrate_signal
35+ def dummy_post_migrate_handler (sender , ** kwargs ):
36+ time .sleep (0.01 ) # Small delay
37+ return "test_result"
38+
39+ with self .assertLogs ('ansible_base.performance' , level = 'INFO' ) as cm :
40+ result = dummy_post_migrate_handler (sender = None )
41+
42+ self .assertEqual (result , "test_result" )
43+ self .assertEqual (len (cm .output ), 1 )
44+ self .assertIn ("post_migrate: test_app.tests.lib.utils.test_performance.dummy_post_migrate_handler" , cm .output [0 ])
45+
46+ def test_enable_performance_logging (self ):
47+ """Test that enable_performance_logging sets up the logger correctly."""
48+ # Get the logger before enabling
49+ logger = logging .getLogger ('ansible_base.performance' )
50+ original_level = logger .level
51+ original_handlers = list (logger .handlers )
52+
53+ try :
54+ enable_performance_logging ()
55+
56+ # Check that the logger is set to INFO level
57+ self .assertEqual (logger .level , logging .INFO )
58+
59+ # Check that a handler was added if none existed
60+ if not original_handlers :
61+ self .assertTrue (len (logger .handlers ) > 0 )
62+
63+ finally :
64+ # Restore original state
65+ logger .setLevel (original_level )
66+ logger .handlers = original_handlers
67+
68+ @patch ('ansible_base.rbac.management.create_dab_permissions' )
69+ @patch ('ansible_base.rbac.triggers.post_migration_rbac_setup' )
70+ @patch ('ansible_base.resource_registry.apps.initialize_resources' )
71+ @patch ('ansible_base.resource_registry.apps.connect_resource_signals' )
72+ def test_patch_post_migrate_handlers (self , mock_connect , mock_init , mock_rbac , mock_perms ):
73+ """Test that patch_post_migrate_handlers patches the handlers."""
74+ # Mock the original functions
75+ def original_func ():
76+ return "original"
77+
78+ mock_connect .return_value = original_func
79+ mock_init .return_value = original_func
80+ mock_rbac .return_value = original_func
81+ mock_perms .return_value = original_func
82+
83+ with self .assertLogs ('ansible_base.performance' , level = 'INFO' ) as cm :
84+ patch_post_migrate_handlers ()
85+
86+ # Check that the patching was logged
87+ patch_logged = any ("Post-migrate signal handlers patched" in output for output in cm .output )
88+ self .assertTrue (patch_logged , "Patching should have been logged" )
89+
90+ @override_settings (ANSIBLE_BASE_MEASURE_POST_MIGRATE_PERFORMANCE = True )
91+ def test_performance_measurement_setting (self ):
92+ """Test that the setting enables performance measurement."""
93+ from django .conf import settings
94+ self .assertTrue (getattr (settings , 'ANSIBLE_BASE_MEASURE_POST_MIGRATE_PERFORMANCE' , False ))
95+
96+
97+ class TestPerformanceMeasurementIntegration (TestCase ):
98+ """Integration tests for performance measurement with Django signals."""
99+
100+ @override_settings (ANSIBLE_BASE_MEASURE_POST_MIGRATE_PERFORMANCE = True )
101+ def test_performance_measurement_with_setting_enabled (self ):
102+ """Test that performance measurement works when the setting is enabled."""
103+ # This test verifies the integration works, but doesn't run actual migrations
104+ # as that would be too complex for a unit test
105+ from django .conf import settings
106+ self .assertTrue (getattr (settings , 'ANSIBLE_BASE_MEASURE_POST_MIGRATE_PERFORMANCE' , False ))
0 commit comments