From 27dbe78ba7c8c99033267e32200990455a41cf86 Mon Sep 17 00:00:00 2001 From: Silas Kraume Date: Fri, 13 Dec 2024 01:30:06 +0100 Subject: [PATCH] more updatechecker tests --- cat_win/src/web/updatechecker.py | 4 ++-- cat_win/tests/src/service/test_editor.py | 8 ++++---- cat_win/tests/src/service/test_hexeditor.py | 4 +--- cat_win/tests/src/web/test_updatechecker.py | 16 ++++++++++++++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cat_win/src/web/updatechecker.py b/cat_win/src/web/updatechecker.py index 5639506..b010173 100644 --- a/cat_win/src/web/updatechecker.py +++ b/cat_win/src/web/updatechecker.py @@ -44,7 +44,7 @@ def get_stable_package_version(package: str) -> str: timeout=2) as _response: response = _response.read() return json.loads(response)['info']['version'] - except (ValueError, OSError): + except (ValueError, OSError, KeyError): return '0.0.0' @@ -71,7 +71,7 @@ def get_latest_package_version(package: str) -> str: key=lambda x: x[1][0]['upload_time'] if x[1] else '1970-01-01T00:00:00', reverse=True )[0][0] - except (ValueError, OSError, IndexError): + except (ValueError, OSError, KeyError, IndexError): return '0.0.0' diff --git a/cat_win/tests/src/service/test_editor.py b/cat_win/tests/src/service/test_editor.py index e7a2512..3264036 100644 --- a/cat_win/tests/src/service/test_editor.py +++ b/cat_win/tests/src/service/test_editor.py @@ -6,9 +6,9 @@ from cat_win.tests.mocks.error import ErrorDefGen from cat_win.tests.mocks.std import StdOutMock, IoHelperMock -import cat_win.src.service.editor as editor_pkg -if editor_pkg.CURSES_MODULE_ERROR: - setattr(editor_pkg, 'curses', None) +from cat_win.src.service import editor +if editor.CURSES_MODULE_ERROR: + setattr(editor, 'curses', None) from cat_win.src.service.editor import Editor mm = MagicMock() @@ -959,7 +959,7 @@ def test_editor_interrupt(self): with self.assertRaises(KeyboardInterrupt): with patch('sys.stderr', new=StdOutMock()) as fake_out: editor._action_interrupt() - self.assertEqual('Interrupting...\n', fake_out.getvalue()) + self.assertEqual('Interrupting...\n', fake_out.getvalue()) def test__action_resize(self): editor = Editor('', '') diff --git a/cat_win/tests/src/service/test_hexeditor.py b/cat_win/tests/src/service/test_hexeditor.py index 9becadc..716d316 100644 --- a/cat_win/tests/src/service/test_hexeditor.py +++ b/cat_win/tests/src/service/test_hexeditor.py @@ -6,7 +6,7 @@ from cat_win.tests.mocks.error import ErrorDefGen from cat_win.tests.mocks.std import StdOutMock, IoHelperMock -import cat_win.src.service.hexeditor as hexeditor +from cat_win.src.service import hexeditor if hexeditor.CURSES_MODULE_ERROR: setattr(hexeditor, 'curses', None) from cat_win.src.service.hexeditor import HexEditor @@ -671,8 +671,6 @@ def char_gen(user_input: list): def test__action_insert(self): editor = HexEditor('', '') editor.curse_window = MagicMock() - def yield_tuple(a, b): - yield (a, b) def char_gen(user_input: list): yield from user_input editor.cpos.set_pos((1, 3)) diff --git a/cat_win/tests/src/web/test_updatechecker.py b/cat_win/tests/src/web/test_updatechecker.py index 160a9bb..5d80b7a 100644 --- a/cat_win/tests/src/web/test_updatechecker.py +++ b/cat_win/tests/src/web/test_updatechecker.py @@ -14,11 +14,27 @@ def test_get_stable_package_version_error(self): self.assertEqual(updatechecker.get_stable_package_version('cat_win'), '0.0.0') with patch('urllib.request.urlopen', ErrorDefGen.get_def(OSError())): self.assertEqual(updatechecker.get_stable_package_version('cat_win'), '0.0.0') + with patch('urllib.request.urlopen', lambda *args, **kwargs: io.StringIO('{"wrong":{"values":"1.4.5"}}')): + self.assertEqual(updatechecker.get_stable_package_version('cat_win'), '0.0.0') @patch('urllib.request.urlopen', lambda *args, **kwargs: io.StringIO('{"info":{"version":"1.4.5"}}')) def test_get_stable_package_version(self): self.assertEqual(updatechecker.get_stable_package_version('cat_win'), '1.4.5') + def test_get_latest_package_version_error(self): + with patch('urllib.request.urlopen', ErrorDefGen.get_def(ValueError())): + self.assertEqual(updatechecker.get_latest_package_version('cat_win'), '0.0.0') + with patch('urllib.request.urlopen', ErrorDefGen.get_def(OSError())): + self.assertEqual(updatechecker.get_latest_package_version('cat_win'), '0.0.0') + with patch('urllib.request.urlopen', lambda *args, **kwargs: io.StringIO('{"wrong":{"values":"1.4.5"}}')): + self.assertEqual(updatechecker.get_latest_package_version('cat_win'), '0.0.0') + with patch('urllib.request.urlopen', lambda *args, **kwargs: io.StringIO('{"releases":{}}')): + self.assertEqual(updatechecker.get_latest_package_version('cat_win'), '0.0.0') + + @patch('urllib.request.urlopen', lambda *args, **kwargs: io.StringIO('{"releases":{"1.4.5": [{"upload_time": "2022-10-09T15:49:40"}], "1.4.0": [{"upload_time": "2022-10-09T15:49:41"}]}}')) + def test_get_latest_package_version(self): + self.assertEqual(updatechecker.get_latest_package_version('cat_win'), '1.4.0') + def test_only_numeric(self): self.assertEqual(updatechecker.only_numeric('1nh589h15io125b085218'), 158915125085218) self.assertEqual(updatechecker.only_numeric('1nh'), 1)