File: test_gitlab.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (70 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download
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
from unittest import TestCase, mock

import pytest
from requests import Session

import core.config
import core.widget
import modules.contrib.gitlab

pytest.importorskip("requests")


def build_gitlab_module(actions=""):
    config = core.config.Config(["-p", "gitlab.actions={}".format(actions)])
    return modules.contrib.gitlab.Module(config=config, theme=None)


def mock_todo_api_response():
    res = mock.Mock()
    res.json = lambda: [
        {"action_name": "assigned"},
        {"action_name": "assigned"},
        {"action_name": "approval_required"},
    ]
    res.status_code = 200
    return res


class TestGitlabUnit(TestCase):
    def test_load_module(self):
        __import__("modules.contrib.gitlab")

    @mock.patch.object(Session, "get", return_value=mock_todo_api_response())
    def test_unfiltered(self, _):
        module = build_gitlab_module()
        module.update()
        assert module.widgets()[0].full_text() == "3"

    @mock.patch.object(Session, "get", return_value=mock_todo_api_response())
    def test_filtered(self, _):
        module = build_gitlab_module(actions="approval_required")
        module.update()
        assert module.widgets()[0].full_text() == "1"

    @mock.patch.object(Session, "get", return_value=mock_todo_api_response())
    def test_state_warning(self, _):
        module = build_gitlab_module(actions="approval_required")
        module.update()

        assert module.state(None) == ["warning"]

    @mock.patch.object(Session, "get", return_value=mock_todo_api_response())
    def test_state_normal(self, _):
        module = build_gitlab_module(actions="empty_filter")
        module.update()

        assert module.state(None) == []

    @mock.patch.object(Session, "get", return_value=mock_todo_api_response())
    def test_state_normal_before_update(self, _):
        module = build_gitlab_module(actions="approval_required")

        assert module.state(None) == []

    @mock.patch.object(Session, "get", side_effect=Exception("Something went wrong"))
    def test_state_normal_if_na(self, _):
        module = build_gitlab_module(actions="approval_required")
        module.update()

        assert module.state(None) == []