Skip to content

Commit

Permalink
Improve UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Mar 3, 2020
1 parent 02945ef commit 908e39f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
21 changes: 11 additions & 10 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,16 @@ def time_diff(self, before):
except:
bp.fatal('Missing dependencies: You need to install python-dateutil', 'https://dateutil.readthedocs.io/en/stable/#installation')

def main(vcs, bp):
def main(registry, conf, bp):
vcs = []
for s in registry:
key = s.name.lower()
if key in conf.user_config:
try:
vcs.append(s(conf.get_config(key)))
except NotiError as e:
bp.fatal(f"[{e.vcs}] {e.message}", e.help_link)

if len(vcs) == 0:
bp.fatal('You have to configure either gitlab or github')

Expand All @@ -538,13 +547,5 @@ def main(vcs, bp):
if __name__ == "__main__":
conf = NotiConfig()
registry = [Gitlab, Github]
vcs = []
for s in registry:
key = s.name.lower()
if key in conf.user_config:
try:
vcs.append(s(conf.get_config(key)))
except NotiError as e:
bp.fatal(f"[{e.vcs}] {e.message}", e.help_link)

main(vcs, bp)
main(registry, conf, bp)
37 changes: 26 additions & 11 deletions tests/unit/test_noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,53 @@
import pytest

from noti import main

from noti import NotiError
class TestNoti:

@pytest.fixture
def bp(self):
return MagicMock()

@pytest.fixture
def vcs(self):
vcs = MagicMock()
vcs.name = '_name_'
vcs_instance = MagicMock()
vcs.return_value.name = '_name_'

return vcs

@pytest.fixture
def conf(self):
conf = MagicMock()
conf.user_config = {
'_name_': {}
}

return conf
def test_no_vcs(self, bp):
main([], bp)
main([], {}, bp)

bp.fatal.assert_called

def test_no_connection(self, bp):
vcs = MagicMock()
vcs.name = '_name_'
vcs.get_mrs.side_effect = ConnectionError()
def test_no_connection(self, bp, vcs, conf):
vcs.return_value.get_mrs.side_effect = ConnectionError()

main([vcs], bp)
main([vcs], conf, bp)

conf.get_config.assert_called_with('_name_')
bp.add_error.assert_called_with('_name_: failed to connect to the server')

def test_happy_path(self, bp):
vcs = MagicMock()
def test_happy_path(self, bp, vcs, conf):
mrs = {
'_repo_': [
'_mr_',
'_mr_'
]
}
vcs.get_mrs.return_value = mrs
vcs.return_value.get_mrs.return_value = mrs

main([vcs], bp)
main([vcs], conf, bp)

bp.generate_title.assert_called_with(mrs)
bp.add.assert_called_with('_repo_')
Expand Down

0 comments on commit 908e39f

Please sign in to comment.