Coverage for ivatar/test_telemetry_integration.py: 99%
69 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 00:07 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-04 00:07 +0000
1"""
2Tests for OpenTelemetry integration and graceful degradation.
3"""
5import unittest
6from unittest.mock import patch
8from django.test import TestCase, RequestFactory
10from ivatar.telemetry_utils import (
11 get_telemetry_decorators,
12 get_telemetry_metrics,
13 is_telemetry_available,
14)
17class TelemetryIntegrationTestCase(TestCase):
18 """Test OpenTelemetry integration and graceful degradation"""
20 def setUp(self):
21 self.factory = RequestFactory()
23 def test_telemetry_utils_import(self):
24 """Test that telemetry utils can be imported safely"""
25 # This should work regardless of whether OpenTelemetry is installed
26 trace_avatar, trace_file, trace_auth = get_telemetry_decorators()
27 metrics = get_telemetry_metrics()
28 available = is_telemetry_available()
30 # All should be callable/usable
31 self.assertTrue(callable(trace_avatar))
32 self.assertTrue(callable(trace_file))
33 self.assertTrue(callable(trace_auth))
34 self.assertTrue(hasattr(metrics, "record_avatar_generated"))
35 self.assertIsInstance(available, bool)
37 def test_decorators_work_as_no_op(self):
38 """Test that decorators work even when OpenTelemetry is not available"""
39 trace_avatar, trace_file, trace_auth = get_telemetry_decorators()
41 @trace_avatar("test_operation")
42 def test_function():
43 return "success"
45 @trace_file("test_upload")
46 def test_upload():
47 return "uploaded"
49 @trace_auth("test_login")
50 def test_login():
51 return "logged_in"
53 # Functions should work normally
54 self.assertEqual(test_function(), "success")
55 self.assertEqual(test_upload(), "uploaded")
56 self.assertEqual(test_login(), "logged_in")
58 def test_metrics_work_as_no_op(self):
59 """Test that metrics work even when OpenTelemetry is not available"""
60 metrics = get_telemetry_metrics()
62 # These should not raise exceptions
63 metrics.record_avatar_generated(size="80", format_type="png", source="test")
64 metrics.record_cache_hit(size="80", format_type="png")
65 metrics.record_cache_miss(size="80", format_type="png")
66 metrics.record_external_request("test_service", 200)
67 metrics.record_file_upload(1024, "image/png", True)
69 def test_telemetry_available_true(self):
70 """Test behavior when telemetry is available"""
71 # This test assumes OpenTelemetry is available
72 available = is_telemetry_available()
73 # The actual value depends on whether OpenTelemetry is installed
74 self.assertIsInstance(available, bool)
76 def test_views_import_telemetry_safely(self):
77 """Test that views can import telemetry utilities safely"""
78 # This should not raise ImportError
79 from ivatar.views import avatar_metrics
80 from ivatar.ivataraccount.views import avatar_metrics as account_metrics
82 # Both should have the required methods
83 self.assertTrue(hasattr(avatar_metrics, "record_avatar_generated"))
84 self.assertTrue(hasattr(account_metrics, "record_file_upload"))
87class MockTelemetryTestCase(TestCase):
88 """Test with mocked OpenTelemetry to verify actual instrumentation calls"""
90 def setUp(self):
91 self.factory = RequestFactory()
93 @patch("ivatar.telemetry_utils.avatar_metrics")
94 def test_avatar_generation_metrics(self, mock_metrics):
95 """Test that avatar generation records metrics"""
96 # Test that metrics would be called (we can't easily test the full flow)
97 mock_metrics.record_avatar_generated.assert_not_called() # Not called yet
99 # Call the metric recording directly to test the interface
100 mock_metrics.record_avatar_generated(
101 size="80", format_type="png", source="test"
102 )
103 mock_metrics.record_avatar_generated.assert_called_once_with(
104 size="80", format_type="png", source="test"
105 )
107 @patch("ivatar.telemetry_utils.avatar_metrics")
108 def test_file_upload_metrics(self, mock_metrics):
109 """Test that file uploads record metrics"""
110 # Test the metric recording interface
111 mock_metrics.record_file_upload(1024, "image/png", True)
112 mock_metrics.record_file_upload.assert_called_once_with(1024, "image/png", True)
114 @patch("ivatar.telemetry_utils.avatar_metrics")
115 def test_external_request_metrics(self, mock_metrics):
116 """Test that external requests record metrics"""
117 # Test the metric recording interface
118 mock_metrics.record_external_request("gravatar", 200)
119 mock_metrics.record_external_request.assert_called_once_with("gravatar", 200)
121 @patch("ivatar.telemetry_utils.avatar_metrics")
122 def test_cache_metrics(self, mock_metrics):
123 """Test that cache operations record metrics"""
124 # Test the metric recording interface
125 mock_metrics.record_cache_hit(size="80", format_type="png")
126 mock_metrics.record_cache_miss(size="80", format_type="png")
128 mock_metrics.record_cache_hit.assert_called_once_with(
129 size="80", format_type="png"
130 )
131 mock_metrics.record_cache_miss.assert_called_once_with(
132 size="80", format_type="png"
133 )
136if __name__ == "__main__":
137 unittest.main()