Skip to content

Commit 0366d07

Browse files
committed
improve test coverage
1 parent 6e5b145 commit 0366d07

File tree

2 files changed

+213
-61
lines changed

2 files changed

+213
-61
lines changed

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/exporter/aws/metrics/test_console_emf_exporter.py

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,17 @@ def test_initialization_with_parameters(self):
4343
"""Test exporter initialization with optional parameters."""
4444
# Test with preferred_temporality
4545
preferred_temporality = {Counter: AggregationTemporality.CUMULATIVE}
46-
exporter = ConsoleEmfExporter(
47-
namespace="TestNamespace",
48-
preferred_temporality=preferred_temporality
49-
)
46+
exporter = ConsoleEmfExporter(namespace="TestNamespace", preferred_temporality=preferred_temporality)
5047
self.assertEqual(exporter.namespace, "TestNamespace")
5148
self.assertEqual(exporter._preferred_temporality[Counter], AggregationTemporality.CUMULATIVE)
5249

5350
# Test with preferred_aggregation
5451
preferred_aggregation = {Counter: "TestAggregation"}
55-
exporter = ConsoleEmfExporter(
56-
preferred_aggregation=preferred_aggregation
57-
)
52+
exporter = ConsoleEmfExporter(preferred_aggregation=preferred_aggregation)
5853
self.assertEqual(exporter._preferred_aggregation[Counter], "TestAggregation")
5954

6055
# Test with additional kwargs
61-
exporter = ConsoleEmfExporter(
62-
namespace="TestNamespace",
63-
extra_param="ignored" # Should be ignored
64-
)
56+
exporter = ConsoleEmfExporter(namespace="TestNamespace", extra_param="ignored") # Should be ignored
6557
self.assertEqual(exporter.namespace, "TestNamespace")
6658

6759
def test_export_log_event_success(self):
@@ -93,7 +85,7 @@ def test_export_log_event_empty_message(self):
9385
# Should not print anything for empty message
9486
captured_output = mock_stdout.getvalue().strip()
9587
self.assertEqual(captured_output, "")
96-
88+
9789
# Should log a warning
9890
mock_logger.warning.assert_called_once()
9991

@@ -108,7 +100,7 @@ def test_export_log_event_missing_message(self):
108100
# Should not print anything when message is missing
109101
captured_output = mock_stdout.getvalue().strip()
110102
self.assertEqual(captured_output, "")
111-
103+
112104
# Should log a warning
113105
mock_logger.warning.assert_called_once()
114106

@@ -123,7 +115,7 @@ def test_export_log_event_with_none_message(self):
123115
# Should not print anything when message is None
124116
captured_output = mock_stdout.getvalue().strip()
125117
self.assertEqual(captured_output, "")
126-
118+
127119
# Should log a warning
128120
mock_logger.warning.assert_called_once()
129121

@@ -147,20 +139,20 @@ def test_export_log_event_various_message_types(self):
147139
# Test with JSON string
148140
json_message = '{"key": "value"}'
149141
log_event = {"message": json_message, "timestamp": 1640995200000}
150-
142+
151143
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
152144
self.exporter._export(log_event)
153-
145+
154146
captured_output = mock_stdout.getvalue().strip()
155147
self.assertEqual(captured_output, json_message)
156148

157149
# Test with plain string
158150
plain_message = "Simple log message"
159151
log_event = {"message": plain_message, "timestamp": 1640995200000}
160-
152+
161153
with patch("sys.stdout", new_callable=StringIO) as mock_stdout:
162154
self.exporter._export(log_event)
163-
155+
164156
captured_output = mock_stdout.getvalue().strip()
165157
self.assertEqual(captured_output, plain_message)
166158

@@ -171,10 +163,10 @@ def test_force_flush(self):
171163
result = self.exporter.force_flush()
172164
self.assertTrue(result)
173165
mock_logger.debug.assert_called_once()
174-
166+
175167
# Reset mock for next call
176168
mock_logger.reset_mock()
177-
169+
178170
# Test with custom timeout
179171
result = self.exporter.force_flush(timeout_millis=5000)
180172
self.assertTrue(result)
@@ -186,23 +178,19 @@ def test_shutdown(self):
186178
# Test with no timeout
187179
result = self.exporter.shutdown()
188180
self.assertTrue(result)
189-
mock_logger.debug.assert_called_once_with(
190-
"ConsoleEmfExporter shutdown called with timeout_millis=%s", None
191-
)
192-
181+
mock_logger.debug.assert_called_once_with("ConsoleEmfExporter shutdown called with timeout_millis=%s", None)
182+
193183
# Reset mock for next call
194184
mock_logger.reset_mock()
195-
185+
196186
# Test with timeout
197187
result = self.exporter.shutdown(timeout_millis=3000)
198188
self.assertTrue(result)
199-
mock_logger.debug.assert_called_once_with(
200-
"ConsoleEmfExporter shutdown called with timeout_millis=%s", 3000
201-
)
202-
189+
mock_logger.debug.assert_called_once_with("ConsoleEmfExporter shutdown called with timeout_millis=%s", 3000)
190+
203191
# Reset mock for next call
204192
mock_logger.reset_mock()
205-
193+
206194
# Test with additional kwargs
207195
result = self.exporter.shutdown(timeout_millis=3000, extra_arg="ignored")
208196
self.assertTrue(result)
@@ -215,38 +203,41 @@ def test_integration_with_metrics_data(self):
215203
mock_resource_metrics = MagicMock()
216204
mock_scope_metrics = MagicMock()
217205
mock_metric = MagicMock()
218-
206+
219207
# Set up the mock hierarchy
220208
mock_metrics_data.resource_metrics = [mock_resource_metrics]
221209
mock_resource_metrics.scope_metrics = [mock_scope_metrics]
222210
mock_scope_metrics.metrics = [mock_metric]
223-
211+
224212
# Mock the metric to have no data_points to avoid complex setup
225213
mock_metric.data = None
226-
214+
227215
with patch("sys.stdout", new_callable=StringIO):
228216
result = self.exporter.export(mock_metrics_data)
229-
217+
230218
# Should succeed even with no actual metrics
231219
from opentelemetry.sdk.metrics.export import MetricExportResult
220+
232221
self.assertEqual(result, MetricExportResult.SUCCESS)
233222

234223
def test_integration_export_success(self):
235224
"""Test export method returns success."""
236225
# Create empty MetricsData
237226
mock_metrics_data = MagicMock(spec=MetricsData)
238227
mock_metrics_data.resource_metrics = []
239-
228+
240229
from opentelemetry.sdk.metrics.export import MetricExportResult
230+
241231
result = self.exporter.export(mock_metrics_data)
242232
self.assertEqual(result, MetricExportResult.SUCCESS)
243233

244234
def test_integration_export_with_timeout(self):
245235
"""Test export method with timeout parameter."""
246236
mock_metrics_data = MagicMock(spec=MetricsData)
247237
mock_metrics_data.resource_metrics = []
248-
238+
249239
from opentelemetry.sdk.metrics.export import MetricExportResult
240+
250241
result = self.exporter.export(mock_metrics_data, timeout_millis=5000)
251242
self.assertEqual(result, MetricExportResult.SUCCESS)
252243

@@ -255,28 +246,29 @@ def test_export_failure_handling(self):
255246
# Create a mock that raises an exception
256247
mock_metrics_data = MagicMock(spec=MetricsData)
257248
mock_metrics_data.resource_metrics = [MagicMock()]
258-
249+
259250
# Make the resource_metrics access raise an exception
260251
type(mock_metrics_data).resource_metrics = property(
261252
lambda self: (_ for _ in ()).throw(Exception("Test exception"))
262253
)
263-
254+
264255
# Patch the logger in the base_emf_exporter since that's where the error logging happens
265256
with patch("amazon.opentelemetry.distro.exporter.aws.metrics.base_emf_exporter.logger") as mock_logger:
266257
from opentelemetry.sdk.metrics.export import MetricExportResult
258+
267259
result = self.exporter.export(mock_metrics_data)
268-
260+
269261
self.assertEqual(result, MetricExportResult.FAILURE)
270262
mock_logger.error.assert_called_once()
271263
self.assertIn("Failed to export metrics", mock_logger.error.call_args[0][0])
272264

273265
def test_flush_output_verification(self):
274266
"""Test that print is called with flush=True."""
275267
log_event = {"message": "test message", "timestamp": 1640995200000}
276-
268+
277269
with patch("builtins.print") as mock_print:
278270
self.exporter._export(log_event)
279-
271+
280272
# Verify print was called with flush=True
281273
mock_print.assert_called_once_with("test message", flush=True)
282274

@@ -286,17 +278,17 @@ def test_logger_levels(self):
286278
with patch("amazon.opentelemetry.distro.exporter.aws.metrics.console_emf_exporter.logger") as mock_logger:
287279
self.exporter.force_flush()
288280
mock_logger.debug.assert_called_once()
289-
290-
# Test debug logging in shutdown
281+
282+
# Test debug logging in shutdown
291283
with patch("amazon.opentelemetry.distro.exporter.aws.metrics.console_emf_exporter.logger") as mock_logger:
292284
self.exporter.shutdown()
293285
mock_logger.debug.assert_called_once()
294-
286+
295287
# Test warning logging for empty message
296288
with patch("amazon.opentelemetry.distro.exporter.aws.metrics.console_emf_exporter.logger") as mock_logger:
297289
self.exporter._export({"message": "", "timestamp": 123})
298290
mock_logger.warning.assert_called_once()
299-
291+
300292
# Test error logging for exception
301293
with patch("builtins.print", side_effect=Exception("Test")):
302294
with patch("amazon.opentelemetry.distro.exporter.aws.metrics.console_emf_exporter.logger") as mock_logger:

0 commit comments

Comments
 (0)