Skip to content

Commit

Permalink
Add test_choose_option_question
Browse files Browse the repository at this point in the history
  • Loading branch information
moisses89 committed Oct 30, 2023
1 parent 01e9b22 commit 8973866
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 11 additions & 4 deletions safe_cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import os
from typing import Optional

from gnosis.eth import EthereumClient

Expand Down Expand Up @@ -46,12 +47,18 @@ def yes_or_no_question(question: str, default_no: bool = True) -> bool:

def choose_option_question(
question: str, number_options: int, default_option: int = 0
) -> bool:
) -> Optional[int]:
if "PYTEST_CURRENT_TEST" in os.environ:
return 0 # Ignore confirmations when running tests
return default_option # Ignore confirmations when running tests
choices = f" [0-{number_options}] default {default_option}:"
reply = str(input(question + choices)).lower().strip() or str(default_option)
option = int(reply)
reply = str(get_input(question + choices)).lower().strip() or str(default_option)
try:
option = int(reply)
except ValueError:
argparse.ArgumentTypeError(f"{reply} must be a number")
return None
if option not in range(0, number_options):
argparse.ArgumentTypeError(f"{option} is not between [0-{number_options}}}")
return None

return option
19 changes: 18 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import mock
from unittest.mock import MagicMock

from safe_cli.utils import yes_or_no_question
from safe_cli.utils import choose_option_question, yes_or_no_question


class TestUtils(unittest.TestCase):
Expand Down Expand Up @@ -34,6 +34,23 @@ def test_yes_or_no_question(self, input_mock: MagicMock):

os.environ["PYTEST_CURRENT_TEST"] = pytest_current_test

@mock.patch("safe_cli.utils.get_input")
def test_choose_option_question(self, input_mock: MagicMock):
pytest_current_test = os.environ.pop("PYTEST_CURRENT_TEST")

input_mock.return_value = ""
self.assertEqual(choose_option_question("", 1), 0)
input_mock.return_value = ""
self.assertEqual(choose_option_question("", 5, 4), 4)
input_mock.return_value = "m"
self.assertIsNone(choose_option_question("", 1))
input_mock.return_value = "10"
self.assertIsNone(choose_option_question("", 1))
input_mock.return_value = "1"
self.assertEqual(choose_option_question("", 2), 1)

os.environ["PYTEST_CURRENT_TEST"] = pytest_current_test


if __name__ == "__main__":
unittest.main()

0 comments on commit 8973866

Please sign in to comment.