diff --git a/bumblebee_status/modules/contrib/gitlab.py b/bumblebee_status/modules/contrib/gitlab.py index 126650d..24f2eb8 100644 --- a/bumblebee_status/modules/contrib/gitlab.py +++ b/bumblebee_status/modules/contrib/gitlab.py @@ -20,15 +20,15 @@ Parameters: * gitlab.actions: Comma separated actions to be parsed (e.g.: gitlab.actions=assigned,approval_required) """ -import json -import requests import shutil -import util -import core.module -import core.widget +import requests + import core.decorators import core.input +import core.module +import core.widget +import util class Module(core.module.Module): @@ -74,5 +74,14 @@ class Module(core.module.Module): except Exception as e: self.__label = "n/a" + def state(self, widget): + state = [] + + try: + if int(self.__label) > 0: + state.append("warning") + except ValueError: + pass + return state # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/contrib/test_gitlab.py b/tests/modules/contrib/test_gitlab.py index d3a3b9a..016a2ab 100644 --- a/tests/modules/contrib/test_gitlab.py +++ b/tests/modules/contrib/test_gitlab.py @@ -1,13 +1,12 @@ -import pytest from unittest import TestCase, mock +import pytest +from requests import Session + import core.config import core.widget import modules.contrib.gitlab -from requests import Session -from requests.models import Response - pytest.importorskip("requests") @@ -15,6 +14,7 @@ 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: [ @@ -25,6 +25,7 @@ def mock_todo_api_response(): res.status_code = 200 return res + class TestGitlabUnit(TestCase): def test_load_module(self): __import__("modules.contrib.gitlab") @@ -40,3 +41,30 @@ class TestGitlabUnit(TestCase): 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) == []