Skip to content

Commit

Permalink
Improve UT
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Mar 5, 2020
1 parent 90c4b9c commit e2cb003
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
7 changes: 1 addition & 6 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,6 @@ def title(self, title):
def add(self, item):
self._items.append(item)

def clear(self):
self.title('')
self._items = []

def print(self):
print(self._title)

Expand All @@ -404,9 +400,8 @@ def print(self):
for config in self._configs:
print(config)

# print_error will override title and body with error messages.
def add_error(self, title):
self._configs.insert(0, f"{title}| color=red")
self._configs.insert(0, f"{title} | color=red")

@classmethod
def fatal(cls, message, help_link=None):
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/test_bitbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from datetime import datetime
from datetime import timedelta
from io import StringIO
from unittest.mock import patch
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -108,6 +107,14 @@ def test_print_with_items(self, bp):
def test_fatal(self, bp):
with pytest.raises(SystemExit):
bp.fatal('hello', 'world')

def test_add_error(self, bp):
bp.title('MYTITLE')
bp.add_error('_error_')

out = proxy_print(bp)

assert out == 'MYTITLE\n---\n_error_ | color=red\nConfigure noti | bash="vi $HOME/.noticonfig.json"\n'

def test_generate_title_no_mr(self, bp):
bp.generate_title({})
Expand Down
1 change: 0 additions & 1 deletion tests/unit/test_github.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from unittest.mock import MagicMock
from unittest.mock import patch

import pytest

from noti import Github
Expand Down
53 changes: 43 additions & 10 deletions tests/unit/test_noti_config.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,73 @@
import json
import tempfile
from pathlib import Path
from unittest.mock import patch
import pytest

from noti import NotiConfig
from noti import NotiError

def create_tempfile(content):
fp = tempfile.NamedTemporaryFile('w')
fp.write(content)
fp.flush()
conf = NotiConfig(Path(fp.name))
fp.close()

return conf

class TestNotiConfig:

@patch('noti.Path.home')
def test_init_config(self, home):
home.return_value = tempfile.tempdir
path = Path(home.return_value, ".noticonfig.json")

assert not path.exists()

conf = NotiConfig()
content = path.read_text()
path.unlink()
default_config = json.loads(content)

assert default_config == conf.DEFAULT_CONFIG
assert not path.exists()



def test_user_config(self):
fp = tempfile.NamedTemporaryFile('w')
fp.write('''
conf = create_tempfile('''
{
"gitlab": {}
}
''')
fp.flush()
conf = NotiConfig(Path(fp.name))
fp.close()

assert 'gitlab' in conf.user_config
assert 'global' not in conf.user_config

def test_get_config(self):
fp = tempfile.NamedTemporaryFile('w')
fp.write('''
conf = create_tempfile('''
{
"github": {
"hello": "world"
}
}
''')
fp.flush()
conf = NotiConfig(Path(fp.name))
fp.close()

config = conf.get_config('github')

assert config.get('mr_limit') == 5
assert config.get('hello') == 'world'

def test_bitbar_config(self):
conf = create_tempfile('''
{
"bitbar": {
"running": "_running_"
}
}
''')

bitbar = conf.bitbar_config
assert bitbar.get('running') == '_running_'
assert bitbar.get('failed') == NotiConfig.DEFAULT_CONFIG.get('bitbar').get('failed')

0 comments on commit e2cb003

Please sign in to comment.