From f22095e978f7147baf9199b7809fe4ef814a4fe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Mon, 31 Aug 2020 17:48:19 -0300 Subject: [PATCH 1/3] Add datetime module tests --- tests/modules/core/test_datetime.py | 56 +++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/tests/modules/core/test_datetime.py b/tests/modules/core/test_datetime.py index b8384d9..f99d552 100644 --- a/tests/modules/core/test_datetime.py +++ b/tests/modules/core/test_datetime.py @@ -1,7 +1,59 @@ import pytest +from unittest import mock, TestCase + +import datetime +from freezegun import freeze_time + +import core.config +import core.input +import core.widget +import modules.core.datetime pytest.importorskip("datetime") -def test_load_module(): - __import__("modules.core.datetime") +def build_module(args = []): + config = core.config.Config(args) + return modules.core.datetime.Module(config=config, theme=None) + +def build_widget(): + return core.widget.Widget() + +class DatetimeTest(TestCase): + def setup_class(self): + locale_patcher = mock.patch('locale.getdefaultlocale') + locale_mock = locale_patcher.start() + locale_mock.return_value = ('en-US', 'UTF-8') + + self.widget = build_widget() + + def test_load_module(self): + __import__("modules.core.datetime") + + @freeze_time('2020-10-15') + def test_default_format(self): + module = build_module() + assert module.full_text(self.widget) == '10/15/2020 12:00:00 AM' + + @freeze_time('2020-10-20') + def test_custom_format(self): + module = build_module(['-p', 'datetime.format=%Y.%m.%d']) + assert module.full_text(self.widget) == '2020.10.20' + + @freeze_time('2020-01-10 10:20:30') + @mock.patch('locale.getdefaultlocale') + def test_invalid_locale(self, locale_mock): + locale_mock.return_value = ('in-IN', 'UTF-0') + + module = build_module() + assert module.full_text(self.widget) == '01/10/2020 10:20:30 AM' + + @mock.patch('core.input.register') + def test_register_left_mouse_action(self, input_register_mock): + module = build_module() + + input_register_mock.assert_called_with( + module, + button=core.input.LEFT_MOUSE, + cmd='calendar' + ) From 0776592da66da7067237bab0cfb60bf234e0d3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Tue, 1 Sep 2020 20:12:31 -0300 Subject: [PATCH 2/3] Add freezegun to Travis dependencies --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 400bd81..78aa402 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ before_install: - sudo apt-get -qq update install: - sudo apt-get install python-dbus - - pip install -U coverage==4.3 pytest pytest-mock + - pip install -U coverage==4.3 pytest pytest-mock freezegun - pip install codeclimate-test-reporter - pip install i3-py Pillow Babel DateTime python-dateutil - pip install docker feedparser i3ipc From b1501a815940b7bfa7129594a7aa6aa606cd445d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Tue, 1 Sep 2020 20:29:04 -0300 Subject: [PATCH 3/3] Remove useless import --- tests/modules/core/test_datetime.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/modules/core/test_datetime.py b/tests/modules/core/test_datetime.py index f99d552..33b7d58 100644 --- a/tests/modules/core/test_datetime.py +++ b/tests/modules/core/test_datetime.py @@ -1,7 +1,5 @@ import pytest from unittest import mock, TestCase - -import datetime from freezegun import freeze_time import core.config