Skip to content

Commit

Permalink
Adjust tests for taskName attribute added in Python 3.12
Browse files Browse the repository at this point in the history
gh-91513: Added taskName attribute to logging module for use with
asyncio tasks.
python/cpython#91513 [1]

credits to https://github.com/hrnciar

[1] python/cpython#91513
  • Loading branch information
jcapiitao committed Jul 18, 2023
1 parent e8b080d commit 1c56a3b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
16 changes: 11 additions & 5 deletions daiquiri/tests/test_daiquiri.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import io
import json
import logging
import sys
import typing
import unittest
import warnings

Expand Down Expand Up @@ -39,7 +41,10 @@ def test_setup_json_formatter(self) -> None:
)
)
daiquiri.getLogger(__name__).warning("foobar")
self.assertEqual({"message": "foobar"}, json.loads(stream.getvalue()))
expected: dict[str, typing.Any] = {"message": "foobar"}
if sys.version_info >= (3, 12):
expected.update({"taskName": None})
self.assertEqual(expected, json.loads(stream.getvalue()))

def test_setup_json_formatter_with_extras(self) -> None:
stream = io.StringIO()
Expand All @@ -51,9 +56,10 @@ def test_setup_json_formatter_with_extras(self) -> None:
)
)
daiquiri.getLogger(__name__).warning("foobar", foo="bar")
self.assertEqual(
{"message": "foobar", "foo": "bar"}, json.loads(stream.getvalue())
)
expected: dict[str, typing.Any] = {"message": "foobar", "foo": "bar"}
if sys.version_info >= (3, 12):
expected.update({"taskName": None})
self.assertEqual(expected, json.loads(stream.getvalue()))

def test_get_logger_set_level(self) -> None:
logger = daiquiri.getLogger(__name__)
Expand All @@ -66,7 +72,7 @@ def test_capture_warnings(self) -> None:
line = stream.getvalue()
self.assertIn("WARNING py.warnings: ", line)
self.assertIn(
"daiquiri/tests/test_daiquiri.py:65: "
"daiquiri/tests/test_daiquiri.py:71: "
'UserWarning: omg!\n warnings.warn("omg!")\n',
line,
)
Expand Down
68 changes: 32 additions & 36 deletions daiquiri/tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# under the License.
import json
import logging
import sys
import syslog
import typing
import unittest
Expand Down Expand Up @@ -100,6 +101,34 @@ def test_datadog(self) -> None:
logger = daiquiri.getLogger()
logger.error("foo", bar=1)
logger.info("bar")
expected_error_1 = {
"status": "error",
"message": "foo",
"bar": 1,
"logger": {"name": "root"},
"timestamp": mock.ANY,
}
expected_info_1 = {
"status": "info",
"message": "bar",
"logger": {"name": "root"},
"timestamp": mock.ANY,
}
expected_error_2 = {
"status": "error",
"message": "backtrace",
"logger": {"name": "saymyname"},
"timestamp": mock.ANY,
"error": {
"kind": "ZeroDivisionError",
"stack": None,
"message": mock.ANY,
},
}
if sys.version_info >= (3, 12):
expected_error_1.update({"taskName": None})
expected_info_1.update({"taskName": None})
expected_error_2.update({"taskName": None})
try:
1 / 0
except ZeroDivisionError:
Expand All @@ -108,41 +137,8 @@ def test_datadog(self) -> None:
socket_instance.connect.assert_called_once_with(("127.0.0.1", 10518))
socket_instance.sendall.assert_has_calls(
[
mock.call(
DatadogMatcher(
{
"status": "error",
"message": "foo",
"bar": 1,
"logger": {"name": "root"},
"timestamp": mock.ANY,
}
)
),
mock.call(
DatadogMatcher(
{
"status": "info",
"message": "bar",
"logger": {"name": "root"},
"timestamp": mock.ANY,
}
)
),
mock.call(
DatadogMatcher(
{
"status": "error",
"message": "backtrace",
"logger": {"name": "saymyname"},
"timestamp": mock.ANY,
"error": {
"kind": "ZeroDivisionError",
"stack": None,
"message": mock.ANY,
},
}
)
),
mock.call(DatadogMatcher(expected_error_1)),
mock.call(DatadogMatcher(expected_info_1)),
mock.call(DatadogMatcher(expected_error_2)),
]
)

0 comments on commit 1c56a3b

Please sign in to comment.