|  | 
|  | 1 | +import json | 
|  | 2 | +import sys | 
|  | 3 | +from io import StringIO | 
|  | 4 | + | 
|  | 5 | +import yaml | 
| 1 | 6 | from django.core.management import call_command | 
| 2 | 7 | from django.test import TestCase, override_settings | 
| 3 | 8 | 
 | 
| 4 |  | -from scheduler.tests import test_settings  # noqa | 
| 5 |  | -from scheduler.types import SchedulerConfiguration | 
|  | 9 | +from scheduler import settings | 
|  | 10 | +from scheduler.helpers.queues import get_queue | 
|  | 11 | +from scheduler.worker.worker import get_queues | 
| 6 | 12 | 
 | 
| 7 | 13 | 
 | 
|  | 14 | +@override_settings(SCHEDULER_QUEUES=dict(default={"HOST": "localhost", "PORT": 6379, "DB": 0})) | 
| 8 | 15 | class SchedulerStatsTest(TestCase): | 
| 9 |  | -    @override_settings(SCHEDULER_CONFIG=SchedulerConfiguration(SCHEDULER_INTERVAL=1)) | 
| 10 |  | -    def test_scheduler_stats__does_not_fail(self): | 
|  | 16 | +    EXPECTED_OUTPUT = { | 
|  | 17 | +        "queues": [ | 
|  | 18 | +            { | 
|  | 19 | +                "canceled_jobs": 0, | 
|  | 20 | +                "failed_jobs": 0, | 
|  | 21 | +                "finished_jobs": 0, | 
|  | 22 | +                "name": "default", | 
|  | 23 | +                "oldest_job_timestamp": None, | 
|  | 24 | +                "queued_jobs": 0, | 
|  | 25 | +                "scheduled_jobs": 0, | 
|  | 26 | +                "scheduler_pid": None, | 
|  | 27 | +                "started_jobs": 0, | 
|  | 28 | +                "workers": 0, | 
|  | 29 | +            } | 
|  | 30 | +        ] | 
|  | 31 | +    } | 
|  | 32 | +    OLD_QUEUES = None | 
|  | 33 | + | 
|  | 34 | +    def setUp(self): | 
|  | 35 | +        super(SchedulerStatsTest, self).setUp() | 
|  | 36 | +        SchedulerStatsTest.OLD_QUEUES = settings._QUEUES | 
|  | 37 | +        settings._QUEUES = dict() | 
|  | 38 | +        settings.conf_settings() | 
|  | 39 | +        get_queue("default").connection.flushall() | 
|  | 40 | + | 
|  | 41 | +    def tearDown(self): | 
|  | 42 | +        super(SchedulerStatsTest, self).tearDown() | 
|  | 43 | +        settings._QUEUES = SchedulerStatsTest.OLD_QUEUES | 
|  | 44 | + | 
|  | 45 | +    def test_scheduler_stats__json_output(self): | 
|  | 46 | +        test_stdout = StringIO() | 
|  | 47 | +        sys.stdout = test_stdout | 
|  | 48 | +        # act | 
| 11 | 49 |         call_command("scheduler_stats", "-j") | 
|  | 50 | +        # assert | 
|  | 51 | +        res = test_stdout.getvalue() | 
|  | 52 | +        self.assertEqual(json.loads(res), SchedulerStatsTest.EXPECTED_OUTPUT) | 
|  | 53 | + | 
|  | 54 | +    def test_scheduler_stats__yaml_output(self): | 
|  | 55 | +        # arrange | 
|  | 56 | +        test_stdout = StringIO() | 
|  | 57 | +        sys.stdout = test_stdout | 
|  | 58 | +        # act | 
| 12 | 59 |         call_command("scheduler_stats", "-y") | 
| 13 |  | -        call_command("scheduler_stats") | 
|  | 60 | +        # assert | 
|  | 61 | +        res = test_stdout.getvalue() | 
|  | 62 | +        self.assertEqual(yaml.load(res, yaml.SafeLoader), SchedulerStatsTest.EXPECTED_OUTPUT) | 
|  | 63 | + | 
|  | 64 | +    def test_scheduler_stats__plain_text_output(self): | 
|  | 65 | +        test_stdout = StringIO() | 
|  | 66 | +        sys.stdout = test_stdout | 
|  | 67 | +        # act | 
|  | 68 | +        call_command("scheduler_stats", "--no-color") | 
|  | 69 | +        # assert | 
|  | 70 | +        res = test_stdout.getvalue() | 
|  | 71 | +        self.assertEqual( | 
|  | 72 | +            res, | 
|  | 73 | +            """ | 
|  | 74 | +Django-Scheduler CLI Dashboard | 
|  | 75 | +
 | 
|  | 76 | +-------------------------------------------------------------------------------- | 
|  | 77 | +| Name             |    Queued |    Active |  Finished |  Canceled |   Workers | | 
|  | 78 | +-------------------------------------------------------------------------------- | 
|  | 79 | +| default          |         0 |         0 |         0 |         0 |         0 | | 
|  | 80 | +-------------------------------------------------------------------------------- | 
|  | 81 | +""", | 
|  | 82 | +        ) | 
|  | 83 | + | 
|  | 84 | +    def test_scheduler_stats__bad_args(self): | 
|  | 85 | +        # arrange | 
|  | 86 | +        sys.stderr = StringIO() | 
|  | 87 | +        sys.stdout = StringIO() | 
|  | 88 | +        # act | 
|  | 89 | +        call_command("scheduler_stats", "-y", "-j") | 
|  | 90 | +        # assert | 
|  | 91 | +        res = sys.stdout.getvalue() | 
|  | 92 | +        self.assertEqual(res, """""") | 
|  | 93 | +        err = sys.stderr.getvalue() | 
|  | 94 | +        self.assertEqual(err, """Aborting. Cannot output as both json and yaml\n""") | 
0 commit comments