|
1 | 1 | import os |
2 | 2 | import unittest |
3 | | - |
4 | | -from unittest.mock import patch, call |
| 3 | +from datetime import datetime, timedelta |
| 4 | +from unittest.mock import call, patch |
5 | 5 |
|
6 | 6 | from botocore.exceptions import ClientError as BotocoreClientError |
7 | 7 | from datadog.api.exceptions import ClientError |
8 | | -from datetime import datetime, timedelta |
9 | 8 |
|
10 | | -from datadog_lambda.metric import lambda_metric, flush_stats |
11 | | -from datadog_lambda.api import decrypt_kms_api_key, KMS_ENCRYPTION_CONTEXT_KEY |
12 | | -from datadog_lambda.thread_stats_writer import ThreadStatsWriter |
| 9 | +from datadog_lambda.api import KMS_ENCRYPTION_CONTEXT_KEY, decrypt_kms_api_key |
| 10 | +from datadog_lambda.metric import flush_stats, lambda_metric |
13 | 11 | from datadog_lambda.tags import dd_lambda_layer_tag |
| 12 | +from datadog_lambda.thread_stats_writer import ThreadStatsWriter |
14 | 13 |
|
15 | 14 |
|
16 | 15 | class TestLambdaMetric(unittest.TestCase): |
@@ -53,22 +52,31 @@ def test_lambda_metric_timestamp_with_extension(self): |
53 | 52 | timestamp = int((datetime.now() - delta).timestamp()) |
54 | 53 |
|
55 | 54 | lambda_metric("test_timestamp", 1, timestamp) |
56 | | - self.mock_metric_lambda_stats.distribution.assert_not_called() |
57 | | - self.mock_metric_extension_thread_stats.distribution.assert_called_with( |
58 | | - "test_timestamp", 1, timestamp=timestamp, tags=[dd_lambda_layer_tag] |
| 55 | + self.mock_metric_lambda_stats.distribution.assert_has_calls( |
| 56 | + [call("test_timestamp", 1, timestamp=timestamp, tags=[dd_lambda_layer_tag])] |
59 | 57 | ) |
| 58 | + self.mock_metric_extension_thread_stats.distribution.assert_not_called() |
60 | 59 |
|
61 | 60 | @patch("datadog_lambda.metric.should_use_extension", True) |
62 | 61 | def test_lambda_metric_datetime_with_extension(self): |
63 | 62 | patcher = patch("datadog_lambda.metric.extension_thread_stats") |
64 | 63 | self.mock_metric_extension_thread_stats = patcher.start() |
65 | 64 | self.addCleanup(patcher.stop) |
66 | 65 |
|
67 | | - delta = timedelta(hours=5) |
| 66 | + delta = timedelta(minutes=1) |
68 | 67 | timestamp = datetime.now() - delta |
69 | 68 |
|
70 | | - lambda_metric("test_timestamp", 1, timestamp) |
71 | | - self.mock_metric_lambda_stats.distribution.assert_not_called() |
| 69 | + lambda_metric("test_datetime_timestamp", 0, timestamp) |
| 70 | + self.mock_metric_lambda_stats.distribution.assert_has_calls( |
| 71 | + [ |
| 72 | + call( |
| 73 | + "test_datetime_timestamp", |
| 74 | + 0, |
| 75 | + timestamp=int(timestamp.timestamp()), |
| 76 | + tags=[dd_lambda_layer_tag], |
| 77 | + ) |
| 78 | + ] |
| 79 | + ) |
72 | 80 | self.mock_metric_extension_thread_stats.distribution.assert_not_called() |
73 | 81 |
|
74 | 82 | @patch("datadog_lambda.metric.should_use_extension", True) |
|
0 commit comments