-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add testing cases for
Notifier
and NotifierHandler
- Loading branch information
Showing
5 changed files
with
164 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import threading | ||
import time | ||
from concurrent.futures import ThreadPoolExecutor | ||
|
||
from module.conf import settings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from textwrap import dedent | ||
|
||
import pytest | ||
from aioresponses import aioresponses | ||
from module.notification.base import ( | ||
DEFAULT_LOG_TEMPLATE, | ||
DEFAULT_MESSAGE_TEMPLATE, | ||
NotifierAdapter, | ||
) | ||
|
||
|
||
def test_default_message_template(fake_notification): | ||
content = DEFAULT_MESSAGE_TEMPLATE.format(**fake_notification.dict()) | ||
expected = dedent( | ||
"""\ | ||
番剧名称:AutoBangumi Test | ||
季度:第1季 | ||
更新集数:第1集 | ||
https://mikanani.me | ||
""" | ||
) | ||
|
||
assert content == expected | ||
|
||
|
||
def test_default_log_template(): | ||
content = DEFAULT_LOG_TEMPLATE.format( | ||
dt="2021-08-15 21:58:44,123", | ||
levelname="INFO", | ||
msg="test message", | ||
) | ||
expected = dedent( | ||
"""\ | ||
日志时间:2021-08-15 21:58:44,123 | ||
日志等级:INFO | ||
日志消息:test message | ||
""" | ||
) | ||
|
||
assert content == expected | ||
|
||
|
||
def test_NotifierAdapter_non_implementation_raised(fake_notification): | ||
with pytest.raises(NotImplementedError) as exc: | ||
NotifierAdapter.send(fake_notification) | ||
|
||
assert exc.match("send method is not implemented yet.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import logging | ||
from unittest import mock | ||
|
||
import pytest | ||
from module.notification import Notifier, NotifierHandler | ||
|
||
|
||
class TestNotifierHandler: | ||
@classmethod | ||
def setup_class(cls): | ||
cls.handler = NotifierHandler( | ||
service_name="gotify", | ||
config=dict( | ||
token="YOUR_TOKEN", | ||
base_url="https://example.com", | ||
), | ||
) | ||
logging.basicConfig(level=logging.DEBUG) | ||
cls.logger = logging.getLogger("test.TestNotifierHandler") | ||
cls.logger.addHandler(cls.handler) | ||
cls.logger.setLevel(logging.DEBUG) | ||
|
||
def test_emit(self, caplog): | ||
with mock.patch("module.notification.Notifier.send") as m: | ||
m.return_value = True | ||
self.logger.warning("test warning") | ||
caplog.set_level(logging.DEBUG, logger=self.logger.name) | ||
print(f"capture log: {caplog.text}") | ||
assert "test" in caplog.text | ||
|
||
|
||
class TestNotifier: | ||
@classmethod | ||
def setup_class(cls): | ||
cls.notifier = Notifier( | ||
service_name="gotify", | ||
config=dict( | ||
token="YOUR_TOKEN", | ||
base_url="https://example.com", | ||
), | ||
) | ||
|
||
def test_init_without_config(self): | ||
with pytest.raises(ValueError) as exc: | ||
Notifier(service_name="gotify") | ||
|
||
assert exc.match("Invalid notifier config") | ||
|
||
def test__get_poster(self): | ||
with mock.patch("module.notification.Notifier._get_poster") as m: | ||
expected = "https://mikanani.me" | ||
m.return_value = expected | ||
|
||
res = self.notifier._get_poster(name="test") | ||
m.assert_called_once_with(name="test") | ||
assert res == expected | ||
|
||
def test__get_poster_failed(self): | ||
with mock.patch("module.notification.Notifier._get_poster") as m: | ||
m.return_value = "" | ||
|
||
res = self.notifier._get_poster(name="") | ||
m.assert_called_once_with(name="") | ||
assert res == "" | ||
|
||
def test_send(self, fake_notification): | ||
with mock.patch("module.notification.Notifier.send") as m: | ||
m.return_value = True | ||
assert self.notifier.send(notification=fake_notification) | ||
|
||
m.assert_called_once_with(notification=fake_notification) | ||
|
||
def test_send_with_side_effect(self, fake_notification): | ||
with mock.patch("module.notification.Notifier.send") as m: | ||
m.side_effect = Exception("Unexpected error.") | ||
with pytest.raises(Exception) as exc: | ||
self.notifier.send(notification=fake_notification) | ||
|
||
assert exc.match("Unexpected error.") |