File: test_commands.py

package info (click to toggle)
python-django-structlog 9.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,004 kB
  • sloc: python: 3,509; sh: 206; javascript: 79; makefile: 19
file content (105 lines) | stat: -rw-r--r-- 4,613 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import logging
from typing import Any

import structlog
from django.core.management import BaseCommand, call_command
from django.test import TestCase
from django_extensions.management.utils import (  # type: ignore[import-untyped]
    signalcommand,
)


class TestCommands(TestCase):
    def test_command(self) -> None:
        class Command(BaseCommand):

            @signalcommand  # type: ignore[misc]
            def handle(self, *args: Any, **options: Any) -> Any:
                structlog.getLogger("command").info("command_event")

        with (
            self.assertLogs("command", logging.INFO) as command_log_results,
            self.assertLogs(
                "django_structlog.commands", logging.INFO
            ) as django_structlog_commands_log_results,
        ):
            call_command(Command())

        self.assertEqual(1, len(command_log_results.records))
        record: Any
        record = command_log_results.records[0]
        self.assertEqual("command_event", record.msg["event"])
        self.assertIn("command_id", record.msg)

        self.assertEqual(2, len(django_structlog_commands_log_results.records))
        record = django_structlog_commands_log_results.records[0]
        self.assertEqual("command_started", record.msg["event"])
        self.assertIn("command_id", record.msg)
        record = django_structlog_commands_log_results.records[1]
        self.assertEqual("command_finished", record.msg["event"])
        self.assertIn("command_id", record.msg)

    def test_nested_command(self) -> None:
        class Command(BaseCommand):
            @signalcommand  # type: ignore[misc]
            def handle(self, *args: Any, **options: Any) -> None:
                logger = structlog.getLogger("command")
                logger.info("command_event_1")
                call_command(NestedCommand())
                logger.info("command_event_2")

        class NestedCommand(BaseCommand):
            @signalcommand  # type: ignore[misc]
            def handle(self, *args: Any, **options: Any) -> None:
                structlog.getLogger("nested_command").info("nested_command_event")

        with (
            self.assertLogs("command", logging.INFO) as command_log_results,
            self.assertLogs("nested_command", logging.INFO),
            self.assertLogs(
                "django_structlog.commands", logging.INFO
            ) as django_structlog_commands_log_results,
        ):
            call_command(Command())

        self.assertEqual(2, len(command_log_results.records))
        command_event_1: Any = command_log_results.records[0]
        self.assertEqual("command_event_1", command_event_1.msg["event"])
        self.assertIn("command_id", command_event_1.msg)
        command_event_2: Any = command_log_results.records[1]
        self.assertEqual("command_event_2", command_event_2.msg["event"])
        self.assertIn("command_id", command_event_2.msg)
        self.assertEqual(
            command_event_1.msg["command_id"], command_event_2.msg["command_id"]
        )

        self.assertEqual(4, len(django_structlog_commands_log_results.records))
        command_started_1: Any = django_structlog_commands_log_results.records[0]
        self.assertEqual("command_started", command_started_1.msg["event"])
        self.assertIn("command_id", command_started_1.msg)

        command_started_2: Any = django_structlog_commands_log_results.records[1]
        self.assertEqual("command_started", command_started_2.msg["event"])
        self.assertIn("command_id", command_started_2.msg)
        self.assertIn("parent_command_id", command_started_2.msg)
        self.assertEqual(
            command_started_1.msg["command_id"],
            command_started_2.msg["parent_command_id"],
        )

        command_finished_1: Any = django_structlog_commands_log_results.records[2]
        self.assertEqual("command_finished", command_finished_1.msg["event"])
        self.assertIn("command_id", command_finished_1.msg)
        self.assertIn("parent_command_id", command_finished_1.msg)
        self.assertEqual(
            command_started_1.msg["command_id"],
            command_finished_1.msg["parent_command_id"],
        )

        command_finished_2: Any = django_structlog_commands_log_results.records[3]
        self.assertEqual("command_finished", command_finished_2.msg["event"])
        self.assertIn("command_id", command_finished_2.msg)
        self.assertNotIn("parent_command_id", command_finished_2.msg)
        self.assertEqual(
            command_event_1.msg["command_id"], command_finished_2.msg["command_id"]
        )