Skip to content

Commit

Permalink
fix @mock.patch in Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
PetrHeinz committed Jun 19, 2024
1 parent fa56374 commit ecc5b37
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
10 changes: 6 additions & 4 deletions tests/test_flusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))

Expand Down
26 changes: 14 additions & 12 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -160,7 +162,7 @@ def test_can_send_unserializable_extra_data(self, MockWorker):
self.assertRegex(log_entry['data']['unserializable'], r'^<tests\.test_handler\.UnserializableObject object at 0x[0-f]+>$')
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(
Expand All @@ -180,7 +182,7 @@ def test_can_send_unserializable_context(self, MockWorker):
self.assertRegex(log_entry['context']['data']['unserializable'], r'^<tests\.test_handler\.UnserializableObject object at 0x[0-f]+>$')
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(
Expand All @@ -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(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import mock
import unittest

from unittest.mock import patch

from logtail.uploader import Uploader


Expand All @@ -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
Expand Down

0 comments on commit ecc5b37

Please sign in to comment.