From ecc5b377fefd8b09b4217c5f9f8b4c304d38019e Mon Sep 17 00:00:00 2001 From: Petr Heinz Date: Wed, 19 Jun 2024 14:12:45 +0200 Subject: [PATCH] fix @mock.patch in Python 3.11 --- tests/test_flusher.py | 10 ++++++---- tests/test_handler.py | 26 ++++++++++++++------------ tests/test_uploader.py | 4 +++- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tests/test_flusher.py b/tests/test_flusher.py index 894057a..e4b524c 100644 --- a/tests/test_flusher.py +++ b/tests/test_flusher.py @@ -6,6 +6,8 @@ import threading import unittest +from unittest.mock import patch + from logtail.compat import queue from logtail.flusher import RETRY_SCHEDULE from logtail.flusher import FlushWorker @@ -51,7 +53,7 @@ def uploader(frame): self.assertEqual(self.calls, 1) - @mock.patch('logtail.flusher._calculate_time_remaining') + @patch('logtail.flusher._calculate_time_remaining') def test_flushes_after_interval(self, calculate_time_remaining): self.buffer_capacity = 10 num_items = 2 @@ -83,8 +85,8 @@ def timeout(last_flush, interval): self.assertEqual(self.upload_calls, 1) self.assertEqual(self.timeout_calls, 2) - @mock.patch('logtail.flusher._calculate_time_remaining') - @mock.patch('logtail.flusher._initial_time_remaining') + @patch('logtail.flusher._calculate_time_remaining') + @patch('logtail.flusher._initial_time_remaining') def test_does_nothing_without_any_items(self, initial_time_remaining, calculate_time_remaining): calculate_time_remaining.side_effect = lambda a,b: 0.0 initial_time_remaining.side_effect = lambda a: 0.0001 @@ -96,7 +98,7 @@ def test_does_nothing_without_any_items(self, initial_time_remaining, calculate_ fw.step() self.assertFalse(uploader.called) - @mock.patch('logtail.flusher.time.sleep') + @patch('logtail.flusher.time.sleep') def test_retries_according_to_schedule(self, mock_sleep): first_frame = list(range(self.buffer_capacity)) diff --git a/tests/test_handler.py b/tests/test_handler.py index 10474ba..ece326f 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -6,20 +6,22 @@ import unittest import logging -from logtail import LogtailHandler, context +from unittest.mock import patch +from logtail import LogtailHandler, context +from logtail.handler import FlushWorker class TestLogtailHandler(unittest.TestCase): source_token = 'dummy_source_token' host = 'dummy_host' - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_handler_creates_uploader_from_args(self, MockWorker): handler = LogtailHandler(source_token=self.source_token, host=self.host) self.assertEqual(handler.uploader.source_token, self.source_token) self.assertEqual(handler.uploader.host, self.host) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_handler_creates_pipe_from_args(self, MockWorker): buffer_capacity = 9 flush_interval = 1 @@ -30,7 +32,7 @@ def test_handler_creates_pipe_from_args(self, MockWorker): ) self.assertTrue(handler.pipe.empty()) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_handler_creates_and_starts_worker_from_args_after_first_log(self, MockWorker): buffer_capacity = 9 flush_interval = 9 @@ -53,7 +55,7 @@ def test_handler_creates_and_starts_worker_from_args_after_first_log(self, MockW ) self.assertEqual(handler.flush_thread.start.call_count, 1) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_emit_starts_thread_if_not_alive(self, MockWorker): handler = LogtailHandler(source_token=self.source_token) @@ -69,7 +71,7 @@ def test_emit_starts_thread_if_not_alive(self, MockWorker): self.assertEqual(handler.flush_thread.start.call_count, 2) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_emit_drops_records_if_configured(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -89,7 +91,7 @@ def test_emit_drops_records_if_configured(self, MockWorker): self.assertTrue(handler.pipe.empty()) self.assertEqual(handler.dropcount, 1) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_emit_does_not_drop_records_if_configured(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -120,7 +122,7 @@ def consumer(q): self.assertEqual(handler.dropcount, 0) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_error_suppression(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -141,7 +143,7 @@ def test_error_suppression(self, MockWorker): handler.raise_exceptions = False logger.critical('hello') - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_can_send_unserializable_extra_data(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -160,7 +162,7 @@ def test_can_send_unserializable_extra_data(self, MockWorker): self.assertRegex(log_entry['data']['unserializable'], r'^$') self.assertTrue(handler.pipe.empty()) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_can_send_unserializable_context(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -180,7 +182,7 @@ def test_can_send_unserializable_context(self, MockWorker): self.assertRegex(log_entry['context']['data']['unserializable'], r'^$') self.assertTrue(handler.pipe.empty()) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_can_send_circular_dependency_in_extra_data(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( @@ -202,7 +204,7 @@ def test_can_send_circular_dependency_in_extra_data(self, MockWorker): self.assertTrue(handler.pipe.empty()) - @mock.patch('logtail.handler.FlushWorker') + @patch('logtail.handler.FlushWorker') def test_can_send_circular_dependency_in_context(self, MockWorker): buffer_capacity = 1 handler = LogtailHandler( diff --git a/tests/test_uploader.py b/tests/test_uploader.py index f09c853..6163714 100644 --- a/tests/test_uploader.py +++ b/tests/test_uploader.py @@ -4,6 +4,8 @@ import mock import unittest +from unittest.mock import patch + from logtail.uploader import Uploader @@ -12,7 +14,7 @@ class TestUploader(unittest.TestCase): source_token = 'dummy_source_token' frame = [1, 2, 3] - @mock.patch('logtail.uploader.requests.Session.post') + @patch('logtail.uploader.requests.Session.post') def test_call(self, post): def mock_post(endpoint, data=None, headers=None): # Check that the data is sent to ther correct endpoint