From 710a2cef98689500149dabfde62524247b4a76cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Wed, 2 Sep 2020 22:10:09 -0300 Subject: [PATCH] Create uptime and public ip tests --- tests/modules/contrib/test_publicip.py | 43 ++++++++++++++++++++++++-- tests/modules/contrib/test_uptime.py | 25 +++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/tests/modules/contrib/test_publicip.py b/tests/modules/contrib/test_publicip.py index 6c5a31a..891cfc8 100644 --- a/tests/modules/contrib/test_publicip.py +++ b/tests/modules/contrib/test_publicip.py @@ -1,5 +1,44 @@ import pytest +from unittest import TestCase, mock -def test_load_module(): - __import__("modules.contrib.publicip") +import core.config +import core.widget +import modules.contrib.publicip + +def build_module(): + config = core.config.Config([]) + return modules.contrib.publicip.Module(config=config, theme=None) + +def widget(module): + return module.widgets()[0] + +class PublicIPTest(TestCase): + def test_load_module(self): + __import__("modules.contrib.publicip") + + @mock.patch('util.location.public_ip') + def test_public_ip(self, public_ip_mock): + public_ip_mock.return_value = '5.12.220.2' + + module = build_module() + module.update() + + assert widget(module).full_text() == '5.12.220.2' + + @mock.patch('util.location.public_ip') + def test_public_ip_with_exception(self, public_ip_mock): + public_ip_mock.side_effect = Exception + + module = build_module() + module.update() + + assert widget(module).full_text() == 'n/a' + + @mock.patch('util.location.public_ip') + def test_interval_seconds(self, public_ip_mock): + public_ip_mock.side_effect = Exception + + module = build_module() + + assert module.parameter('interval') == 3600 diff --git a/tests/modules/contrib/test_uptime.py b/tests/modules/contrib/test_uptime.py index 67c791f..18a59d4 100644 --- a/tests/modules/contrib/test_uptime.py +++ b/tests/modules/contrib/test_uptime.py @@ -1,7 +1,26 @@ import pytest +from unittest import TestCase, mock -pytest.importorskip("datetime") +import core.config +import core.widget +import modules.contrib.uptime -def test_load_module(): - __import__("modules.contrib.uptime") +def build_module(): + config = core.config.Config([]) + return modules.contrib.uptime.Module(config=config, theme=None) + +def widget(module): + return module.widgets()[0] + +class UptimeTest(TestCase): + def test_load_module(self): + __import__("modules.contrib.uptime") + + @mock.patch('builtins.open', new_callable=mock.mock_open, read_data='300000 10.45') + def test_uptime(self, uptime_mock): + module = build_module() + module.update() + + uptime_mock.assert_called_with('/proc/uptime', 'r') + assert widget(module).full_text() == '3 days, 11:20:00'