Skip to content

Commit

Permalink
more updatechecker tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Dec 13, 2024
1 parent af3fe71 commit 27dbe78
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cat_win/src/web/updatechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand All @@ -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'


Expand Down
8 changes: 4 additions & 4 deletions cat_win/tests/src/service/test_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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('', '')
Expand Down
4 changes: 1 addition & 3 deletions cat_win/tests/src/service/test_hexeditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
16 changes: 16 additions & 0 deletions cat_win/tests/src/web/test_updatechecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 27dbe78

Please sign in to comment.