Skip to content

Commit

Permalink
Improve: test coverage bouncer (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts authored Mar 2, 2023
2 parents ea9ed11 + d01b26d commit 5171b3b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 26 deletions.
37 changes: 25 additions & 12 deletions qgis_deployment_toolbelt/utils/bouncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Standard library
import logging
import sys
from typing import Union

# #############################################################################
# ########## Globals ###############
Expand All @@ -26,11 +27,15 @@
# ##################################


def exit_cli_error(message: str, abort: bool = True):
"""Display error message (red) and stop execution.
def exit_cli_error(message: Union[str, Exception], abort: bool = True):
"""Display error message and stop execution.
:param str message: message to log and display in terminal.
:param bool abort: option to abort after displaying . Defaults to: True - optional
Args:
message (Union[str, Exception]): message to log and display in terminal.
abort (bool, optional): option to abort after displaying. Defaults to True.
Raises:
SystemExit: when abort is True
"""
# log
logger.error(message, exc_info=True)
Expand All @@ -45,23 +50,31 @@ def exit_cli_error(message: str, abort: bool = True):
sys.exit(message)


def exit_cli_normal(message: str, abort: bool = True):
"""Display normal message (magenta) and stop execution.
def exit_cli_normal(message: Union[str, Exception], abort: bool = True):
"""Display normal message and stop execution if required.
Args:
message (Union[str, Exception]): message to log and display in terminal.
abort (bool, optional): option to abort after displaying. Defaults to True.
:param str message: message to log and display in terminal.
:param bool abort: option to abort after displaying . Defaults to: True - optional
Raises:
SystemExit: when abort is True
"""
logger.info(message)

if abort:
sys.exit(0)


def exit_cli_success(message: str, abort: bool = True):
"""Display success message (green) and stop execution.
def exit_cli_success(message: Union[str, Exception], abort: bool = True):
"""Display success message and stop execution ir required.
Args:
message (Union[str, Exception]): message to log and display in terminal.
abort (bool, optional): option to abort after displaying. Defaults to True.
:param str message: message to log and display in terminal.
:param bool abort: option to abort after displaying the message. Defaults to: True - optional
Raises:
SystemExit: when abort is True
"""
logger.info(message)

Expand Down
14 changes: 0 additions & 14 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ def test_proxy_settings(self):
environ["HTTP_PROXY"] = "http://proxy.example.com:3128"
self.assertIsInstance(get_proxy_settings(), dict)

def test_slugger(self):
"""Test minimalist slugify function."""
# hyphen by default
self.assertEqual(
sluggy("Oyé oyé brâves gens de 1973 ! Hé oh ! Sentons-nous l'ail %$*§ ?!"),
"oye-oye-braves-gens-de-1973-he-oh-sentons-nous-lail",
)

# with underscore
self.assertEqual(
sluggy("Nín hǎo. Wǒ shì zhōng guó rén", "_"),
"nin_hao_wo_shi_zhong_guo_ren",
)

def test_str2bool(self):
"""Test str2bool."""
# OK
Expand Down
78 changes: 78 additions & 0 deletions tests/test_utils_bouncer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#! python3 # noqa E265

"""
Usage from the repo root folder:
.. code-block:: bash
# for whole tests
python -m unittest tests.test_utils_bouncer
# for specific test
python -m unittest tests.test_utils_bouncer.TestUtilsBouncer.test_bouncer_error
"""

# standard library
import unittest

# project
from qgis_deployment_toolbelt.utils.bouncer import (
exit_cli_error,
exit_cli_normal,
exit_cli_success,
)

# ############################################################################
# ########## Classes #############
# ################################


class TestUtilsBouncer(unittest.TestCase):
"""Test package utilities."""

def test_bouncer_error(self):
"""Test bouncer error."""
# test with simple string
error_message = "fake error"

with self.assertRaises(SystemExit) as err:
exit_cli_error(error_message)
exception = err.exception
self.assertEqual(exception.code, error_message)

self.assertIsNone(exit_cli_error(error_message, abort=False))

# test with an Exception
error = TypeError("fake type error")
with self.assertRaises(SystemExit):
exit_cli_error(error)
self.assertIsNone(exit_cli_error(error, abort=False))

def test_bouncer_normal(self):
"""Test bouncer normal."""
# test with simple string
error_message = "fake error"

with self.assertRaises(SystemExit) as err:
exit_cli_normal(error_message)
exception = err.exception
self.assertEqual(exception.code, 0)

self.assertIsNone(exit_cli_normal(error_message, abort=False))

def test_bouncer_success(self):
"""Test bouncer success."""
# test with simple string
error_message = "fake error"

with self.assertRaises(SystemExit) as err:
exit_cli_success(error_message)
exception = err.exception
self.assertEqual(exception.code, 0)

self.assertIsNone(exit_cli_success(error_message, abort=False))


# ############################################################################
# ####### Stand-alone run ########
# ################################
if __name__ == "__main__":
unittest.main()

0 comments on commit 5171b3b

Please sign in to comment.