From a6dfb726d39a4f545c94a87c445a5924edd4d7aa Mon Sep 17 00:00:00 2001 From: freeram Date: Thu, 4 Apr 2024 11:55:29 -0600 Subject: [PATCH] Add basic tests --- README.md | 3 + pomodorodiscord/README.md | 4 ++ pomodorodiscord/tests/test_pom_frame.py | 92 +++++++++++++++++++++++++ pomodorodiscord/tests/test_utils.py | 37 ++++++++++ 4 files changed, 136 insertions(+) create mode 100644 pomodorodiscord/tests/test_pom_frame.py create mode 100644 pomodorodiscord/tests/test_utils.py diff --git a/README.md b/README.md index 6de2fd1..d45dd46 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,6 @@ Pomodoro timer GUI with Discord Rich Presence integration and stats tracking ## Acknowledgements Some themes adapted from: https://github.com/avalon60/ctk_theme_builder/tree/develop/user_themes + +## Testing +`python3 -m unittest discover tests` diff --git a/pomodorodiscord/README.md b/pomodorodiscord/README.md index 4caa9a6..86cdae2 100644 --- a/pomodorodiscord/README.md +++ b/pomodorodiscord/README.md @@ -1,3 +1,6 @@ +## Running + +`python3 run.py` ## Structure @@ -17,6 +20,7 @@ - `graphs.py`: matplotlib plots using data collected. - `sounds/`: Beep. - `themes/`: Responsible for all colors in the GUI. Theme is loaded on launch in run.py. +- `tests/`: Basic tests. Ignore ResourceWarnings. ## File Dependencies diff --git a/pomodorodiscord/tests/test_pom_frame.py b/pomodorodiscord/tests/test_pom_frame.py new file mode 100644 index 0000000..cc69256 --- /dev/null +++ b/pomodorodiscord/tests/test_pom_frame.py @@ -0,0 +1,92 @@ +import warnings +import unittest +from src.frames.pomodoro_frame import PomodoroFrame +from src.utils import load_config, DEF_POMODORO_MINS, DEF_SB_BEFORE_L + + +def ignore_warnings(test_func): + def do_test(self, *args, **kwargs): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ResourceWarning) + test_func(self, *args, **kwargs) + return do_test + + +class TestPomodoroFrame(unittest.TestCase): + + @ignore_warnings + def setUp(self): + global root + self.config = load_config() + self.pomodoro_frame = PomodoroFrame(None) + + @ignore_warnings + def test_initialize_ui(self): + config = self.config + frame = self.pomodoro_frame + self.assertIsNotNone(frame) + + self.assertIsNotNone(frame.break_text) + self.assertEqual(frame.pomodoro_time, config.get("pomodoro_time", DEF_POMODORO_MINS) * 60) + self.assertIsNotNone(frame.timer_display) + self.assertIsNotNone(frame.start_button) + self.assertIsNotNone(frame.start_button) + self.assertIsNotNone(frame.sb_button) + self.assertIsNotNone(frame.lb_button) + self.assertIsNotNone(frame.reset_button) + + self.assertFalse(frame.running) + self.assertFalse(frame.break_running) + self.assertIsNone(frame.next_timer_update) + self.assertEqual(frame.remaining_time, frame.pomodoro_time) + + self.assertEqual(frame.auto_break_cycling, config.get("auto_break_cycling", False)) + self.assertEqual(frame.short_breaks_before_long, config.get("short_breaks_before_long", DEF_SB_BEFORE_L)) + self.assertFalse(frame.short_break_running) + self.assertEqual(frame.short_break_counter, 0) + + self.assertIsNone(frame.start_time_timestamp) + self.assertIsNone(frame.end_time_timestamp) + + self.assertEqual(frame.session_counter, 0) + self.assertEqual(frame.seconds_studied, 0) + + @ignore_warnings + def test_toggle_timer(self): + frame = self.pomodoro_frame + frame.toggle_timer() + self.assertTrue(frame.running) + self.assertIsNotNone(frame.start_time_timestamp) + self.assertIsNotNone(frame.end_time_timestamp) + frame.toggle_timer() + self.assertFalse(frame.running) + + @ignore_warnings + def test_reset(self): + frame = self.pomodoro_frame + frame.running = True + frame.break_running = True + frame.short_break_running = True + frame.break_text.set("Short break") + frame.remaining_time = 100 + frame.reset() + self.assertFalse(frame.running) + self.assertFalse(frame.break_running) + self.assertFalse(frame.short_break_running) + self.assertEqual(frame.break_text.get(), "") + self.assertEqual(frame.remaining_time, frame.pomodoro_time) + + @ignore_warnings + def test_short_break(self): + frame = self.pomodoro_frame + frame.short_break() + self.assertTrue(frame.break_running) + self.assertTrue(frame.short_break_running) + self.assertEqual(frame.break_text.get(), "Short break") + + @ignore_warnings + def test_long_break(self): + frame = self.pomodoro_frame + frame.long_break() + self.assertTrue(frame.break_running) + self.assertEqual(frame.break_text.get(), "Long break") diff --git a/pomodorodiscord/tests/test_utils.py b/pomodorodiscord/tests/test_utils.py new file mode 100644 index 0000000..e782de9 --- /dev/null +++ b/pomodorodiscord/tests/test_utils.py @@ -0,0 +1,37 @@ +import unittest +from src.utils import load_config, save_config, load_data, save_data + + +class TestUtils(unittest.TestCase): + + def setUp(self): + self.saved_config = load_config() + self.saved_data = load_data() + + def tearDown(self): + save_config(self.saved_config) + save_data(self.saved_data) + + def test_load_config(self): + config = load_config() + self.assertIsInstance(config, dict) + self.assertIn('theme', config) + self.assertIn('sound', config) + + def test_save_config(self): + config = {'theme': 'test_theme', 'sound': 'test_sound.mp3'} + save_config(config) + loaded_config = load_config() + self.assertEqual(config, loaded_config) + + def test_load_data(self): + data = load_data() + self.assertIsInstance(data, dict) + self.assertIn('total_seconds_studied', data) + self.assertIn('total_pomodoro_sessions', data) + + def test_save_data(self): + data = {'total_seconds_studied': 1000, 'total_pomodoro_sessions': 10} + save_data(data) + loaded_data = load_data() + self.assertEqual(data, loaded_data)