ref: typehints, enum instead of raw string, abc and other (#1)

Full code coverage with annotations, fixing errors in various linters: ruff, wps, etc. Fixing errors in type checkers: ty, mypy, pyright. Formatting and bringing code to a consistent style, applying best practices in various aspects.
This commit is contained in:
kolo
2025-10-08 13:37:31 +03:00
committed by GitHub
parent 22f1171192
commit 73303b1c08
45 changed files with 983 additions and 996 deletions
@@ -5,10 +5,10 @@ import io
import re
from argenta.app import App
from argenta.command import Command
from argenta.command import Command, PredefinedFlags
from argenta.command.flag.models import ValidationStatus
from argenta.router import Router
from argenta.command.flag.flags.models import Flags
from argenta.command.flag.defaults import PredefinedFlags
from argenta.orchestrator import Orchestrator
from argenta.response import Response
@@ -22,13 +22,13 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -43,14 +43,14 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(ignore_command_register=False,
override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -65,8 +65,10 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
print(f'test command with undefined flag: {response.undefined_flags.get_flag('help').get_string_entity()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name('help')
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag: {undefined_flag.string_entity}')
app = App(override_system_messages=True,
print_func=print)
@@ -85,9 +87,12 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
flag = response.undefined_flags.get_flag("port")
print(f'test command with undefined flag with value: {flag.get_string_entity()} {flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name("port")
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag with value: {undefined_flag.string_entity} {undefined_flag.input_value}')
else:
raise
app = App(override_system_messages=True,
print_func=print)
@@ -104,12 +109,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flags = Flags(PredefinedFlags.HOST)
flags = Flags([PredefinedFlags.HOST])
@router.command(Command('test', flags=flags))
def test(response: Response):
flag = response.undefined_flags.get_flag("port")
print(f'connecting to host with flag: {flag.get_string_entity()} {flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name("port")
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'connecting to host with flag: {undefined_flag.string_entity} {undefined_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -128,13 +134,13 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -149,17 +155,17 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('more'))
def test(response: Response):
def test1(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'more command')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.get_trigger()}'))
app.set_unknown_command_handler(lambda command: print(f'Unknown command: {command.trigger}'))
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
@@ -174,7 +180,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
@@ -195,7 +201,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
app = App(override_system_messages=True,
@@ -216,7 +222,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test', flags=PredefinedFlags.PORT))
def test(response: Response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
@@ -227,4 +233,25 @@ class TestSystemHandlerNormalWork(TestCase):
output = mock_stdout.getvalue()
self.assertIn("\nRepeated input flags: \"test --port 22 --port 33\"\n", output)
self.assertIn('Repeated input flags: "test --port 22 --port 33"', output)
@patch("builtins.input", side_effect=["test --help", "q"])
@patch("sys.stdout", new_callable=io.StringIO)
def test_input_correct_command_with_unregistered_flag3(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
undefined_flag = response.input_flags.get_flag_by_name('help')
if undefined_flag and undefined_flag.status == ValidationStatus.UNDEFINED:
print(f'test command with undefined flag: {undefined_flag.string_entity}')
app = App(override_system_messages=True,
print_func=print)
app.include_router(router)
orchestrator.start_polling(app)
output = mock_stdout.getvalue()
self.assertIn('\ntest command with undefined flag: --help\n', output)
@@ -5,13 +5,13 @@ import io
import re
from argenta.app import App
from argenta.command import Command
from argenta.command import Command, PredefinedFlags
from argenta.command.flag.models import PossibleValues, ValidationStatus
from argenta.response import Response
from argenta.router import Router
from argenta.orchestrator import Orchestrator
from argenta.command.flag import Flag
from argenta.command.flag.flags import Flags
from argenta.command.flag.defaults import PredefinedFlags
@@ -23,7 +23,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(override_system_messages=True,
@@ -43,7 +43,7 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print('test command')
app = App(ignore_command_register=True,
@@ -62,11 +62,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_custom_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flag = Flag('help', '--', False)
flag = Flag('help', prefix='--', possible_values=PossibleValues.NEITHER)
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'\nhelp for {response.valid_flags.get_flag('help').get_name()} flag\n')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('help')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'\nhelp for {valid_flag.name} flag\n')
app = App(override_system_messages=True,
print_func=print)
@@ -82,12 +84,13 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_custom_flag2(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flag = Flag('port', '--', re.compile(r'^\d{1,5}$'))
flag = Flag('port', prefix='--', possible_values=re.compile(r'^\d{1,5}$'))
@router.command(Command('test', flags=flag))
def test(response: Response):
input_flag = response.valid_flags.get_flag('port')
print(f'flag value for {input_flag.get_name()} flag : {input_flag.get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('port')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'flag value for {valid_flag.name} flag : {valid_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -107,8 +110,10 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.SHORT_HELP
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'help for {response.valid_flags.get_flag('H').get_name()} flag')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('H')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'help for {valid_flag.name} flag')
app = App(override_system_messages=True,
print_func=print)
@@ -128,8 +133,9 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.INFO
@router.command(Command('test', flags=flag))
def test(response: Response):
if response.valid_flags.get_flag('info'):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('info')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print('info about test command')
app = App(override_system_messages=True,
@@ -150,8 +156,10 @@ class TestSystemHandlerNormalWork(TestCase):
flag = PredefinedFlags.HOST
@router.command(Command('test', flags=flag))
def test(response: Response):
print(f'connecting to host {response.valid_flags.get_flag('host').get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
valid_flag = response.input_flags.get_flag_by_name('host')
if valid_flag and valid_flag.status == ValidationStatus.VALID:
print(f'connecting to host {valid_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -168,12 +176,14 @@ class TestSystemHandlerNormalWork(TestCase):
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
orchestrator = Orchestrator()
flags = Flags(PredefinedFlags.HOST, PredefinedFlags.PORT)
flags = Flags([PredefinedFlags.HOST, PredefinedFlags.PORT])
@router.command(Command('test', flags=flags))
def test(response: Response):
valid_flags = response.valid_flags
print(f'connecting to host {valid_flags.get_flag('host').get_value()} and port {valid_flags.get_flag('port').get_value()}')
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
host_flag = response.input_flags.get_flag_by_name('host')
port_flag = response.input_flags.get_flag_by_name('port')
if (host_flag and host_flag.status == ValidationStatus.VALID) and (port_flag and port_flag.status == ValidationStatus.VALID):
print(f'connecting to host {host_flag.input_value} and port {port_flag.input_value}')
app = App(override_system_messages=True,
print_func=print)
@@ -192,11 +202,11 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('some'))
def test2(response):
def test2(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'some command')
app = App(override_system_messages=True,
@@ -216,15 +226,15 @@ class TestSystemHandlerNormalWork(TestCase):
orchestrator = Orchestrator()
@router.command(Command('test'))
def test(response):
def test(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'test command')
@router.command(Command('some'))
def test(response):
def test1(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'some command')
@router.command(Command('more'))
def test(response):
def test2(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
print(f'more command')
app = App(override_system_messages=True,
+14 -13
View File
@@ -1,6 +1,7 @@
from argenta.command.flag import Flag, InputFlag
from argenta.command.flag.flags import Flags
from argenta.command.models import InputCommand, Command
from argenta.command.flag.models import PossibleValues
from argenta.command.models import InputCommand, Command, ValidationStatus
from argenta.command.exceptions import (UnprocessedInputFlagException,
RepeatedInputFlagsException,
EmptyInputCommandException)
@@ -11,7 +12,7 @@ import re
class TestInputCommand(unittest.TestCase):
def test_parse_correct_raw_command(self):
self.assertEqual(InputCommand.parse('ssh --host 192.168.0.3').get_trigger(), 'ssh')
self.assertEqual(InputCommand.parse('ssh --host 192.168.0.3').trigger, 'ssh')
def test_parse_raw_command_without_flag_name_with_value(self):
with self.assertRaises(UnprocessedInputFlagException):
@@ -27,35 +28,35 @@ class TestInputCommand(unittest.TestCase):
def test_validate_valid_input_flag1(self):
command = Command('some', flags=Flag('test'))
self.assertEqual(command.validate_input_flag(InputFlag('test')), 'Valid')
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value=None, status=None)), ValidationStatus.VALID)
def test_validate_valid_input_flag2(self):
command = Command('some', flags=Flags(Flag('test'), Flag('more')))
self.assertEqual(command.validate_input_flag(InputFlag('more')), 'Valid')
command = Command('some', flags=Flags([Flag('test'), Flag('more')]))
self.assertEqual(command.validate_input_flag(InputFlag('more', input_value=None, status=None)), ValidationStatus.VALID)
def test_validate_undefined_input_flag1(self):
command = Command('some', flags=Flag('test'))
self.assertEqual(command.validate_input_flag(InputFlag('more')), 'Undefined')
self.assertEqual(command.validate_input_flag(InputFlag('more', input_value=None, status=None)), ValidationStatus.UNDEFINED)
def test_validate_undefined_input_flag2(self):
command = Command('some', flags=Flags(Flag('test'), Flag('more')))
self.assertEqual(command.validate_input_flag(InputFlag('case')), 'Undefined')
command = Command('some', flags=Flags([Flag('test'), Flag('more')]))
self.assertEqual(command.validate_input_flag(InputFlag('case', input_value=None, status=None)), ValidationStatus.UNDEFINED)
def test_validate_undefined_input_flag3(self):
command = Command('some')
self.assertEqual(command.validate_input_flag(InputFlag('case')), 'Undefined')
self.assertEqual(command.validate_input_flag(InputFlag('case', input_value=None, status=None)), ValidationStatus.UNDEFINED)
def test_invalid_input_flag1(self):
command = Command('some', flags=Flag('test', possible_values=False))
self.assertEqual(command.validate_input_flag(InputFlag('test', value='example')), 'Invalid')
command = Command('some', flags=Flag('test', possible_values=PossibleValues.NEITHER))
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value='example', status=None)), ValidationStatus.INVALID)
def test_invalid_input_flag2(self):
command = Command('some', flags=Flag('test', possible_values=['some', 'case']))
self.assertEqual(command.validate_input_flag(InputFlag('test', value='slay')), 'Invalid')
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value='slay', status=None)), ValidationStatus.INVALID)
def test_invalid_input_flag3(self):
command = Command('some', flags=Flag('test', possible_values=re.compile(r'^ex\d{, 2}op$')))
self.assertEqual(command.validate_input_flag(InputFlag('test', value='example')), 'Invalid')
self.assertEqual(command.validate_input_flag(InputFlag('test', input_value='example', status=None)), ValidationStatus.INVALID)
def test_isinstance_parse_correct_raw_command(self):
cmd = InputCommand.parse('ssh --host 192.168.0.3')
+2 -2
View File
@@ -6,11 +6,11 @@ import unittest
class TestDividingLine(unittest.TestCase):
def test_get_static_dividing_line_full_line(self):
line = StaticDividingLine('-')
self.assertEqual(line.get_full_static_line(True).count('-'), 25)
self.assertEqual(line.get_full_static_line(is_override=True).count('-'), 25)
def test_get_dynamic_dividing_line_full_line(self):
line = DynamicDividingLine()
self.assertEqual(line.get_full_dynamic_line(20, True).count('-'), 20)
self.assertEqual(line.get_full_dynamic_line(length=20, is_override=True).count('-'), 20)
def test_get_dividing_line_unit_part(self):
line = StaticDividingLine('')
+25 -26
View File
@@ -7,35 +7,34 @@ import re
class TestFlag(unittest.TestCase):
def test_get_string_entity(self):
self.assertEqual(Flag(name='test').get_string_entity(),
self.assertEqual(Flag(name='test').string_entity,
'--test')
def test_get_string_entity2(self):
self.assertEqual(Flag(name='test',
prefix='---').get_string_entity(),
prefix='---').string_entity,
'---test')
def test_get_flag_name(self):
self.assertEqual(Flag(name='test').get_name(),
self.assertEqual(Flag(name='test').name,
'test')
def test_get_flag_prefix(self):
self.assertEqual(Flag(name='test').get_prefix(),
self.assertEqual(Flag(name='test').prefix,
'--')
def test_get_flag_prefix2(self):
self.assertEqual(Flag(name='test',
prefix='--').get_prefix(),
prefix='--').prefix,
'--')
def test_get_flag_value_without_set(self):
self.assertEqual(InputFlag(name='test').get_value(),
self.assertEqual(InputFlag(name='test', input_value=None, status=None).input_value,
None)
def test_get_flag_value_with_set(self):
flag = InputFlag(name='test')
flag.set_value('example')
self.assertEqual(flag.get_value(), 'example')
flag = InputFlag(name='test', input_value='example', status=None)
self.assertEqual(flag.input_value, 'example')
def test_validate_incorrect_flag_value_with_list_of_possible_flag_values(self):
flag = Flag(name='test', possible_values=['1', '2', '3'])
@@ -54,15 +53,15 @@ class TestFlag(unittest.TestCase):
self.assertEqual(flag.validate_input_flag_value('192.168.9.8'), True)
def test_validate_correct_empty_flag_value_without_possible_flag_values(self):
flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
flag = Flag(name='test', possible_values=PossibleValues.NEITHER)
self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_correct_empty_flag_value_with_possible_flag_values(self):
flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
flag = Flag(name='test', possible_values=PossibleValues.NEITHER)
self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_incorrect_random_flag_value_without_possible_flag_values(self):
flag = Flag(name='test', possible_values=PossibleValues.DISABLE)
flag = Flag(name='test', possible_values=PossibleValues.NEITHER)
self.assertEqual(flag.validate_input_flag_value('random value'), False)
def test_validate_correct_random_flag_value_with_possible_flag_values(self):
@@ -70,21 +69,21 @@ class TestFlag(unittest.TestCase):
self.assertEqual(flag.validate_input_flag_value('random value'), True)
def test_get_input_flag1(self):
flag = InputFlag(name='test')
input_flags = InputFlags(flag)
self.assertEqual(input_flags.get_flag('test'), flag)
flag = InputFlag(name='test', input_value=None, status=None)
input_flags = InputFlags([flag])
self.assertEqual(input_flags.get_flag_by_name('test'), flag)
def test_get_input_flag2(self):
flag = InputFlag(name='test')
flag2 = InputFlag(name='some')
input_flags = InputFlags(flag, flag2)
self.assertEqual(input_flags.get_flag('some'), flag2)
flag = InputFlag(name='test', input_value=None, status=None)
flag2 = InputFlag(name='some', input_value=None, status=None)
input_flags = InputFlags([flag, flag2])
self.assertEqual(input_flags.get_flag_by_name('some'), flag2)
def test_get_undefined_input_flag(self):
flag = InputFlag(name='test')
flag2 = InputFlag(name='some')
input_flags = InputFlags(flag, flag2)
self.assertEqual(input_flags.get_flag('case'), None)
flag = InputFlag(name='test', input_value=None, status=None)
flag2 = InputFlag(name='some', input_value=None, status=None)
input_flags = InputFlags([flag, flag2])
self.assertEqual(input_flags.get_flag_by_name('case'), None)
def test_get_flags(self):
flags = Flags()
@@ -94,18 +93,18 @@ class TestFlag(unittest.TestCase):
Flag('test3'),
]
flags.add_flags(list_of_flags)
self.assertEqual(flags.get_flags(),
self.assertEqual(flags.flags,
list_of_flags)
def test_add_flag(self):
flags = Flags()
flags.add_flag(Flag('test'))
self.assertEqual(len(flags.get_flags()), 1)
self.assertEqual(len(flags.flags), 1)
def test_add_flags(self):
flags = Flags()
flags.add_flags([Flag('test'), Flag('test2')])
self.assertEqual(len(flags.get_flags()), 2)
self.assertEqual(len(flags.flags), 2)
+38 -43
View File
@@ -1,7 +1,10 @@
from argenta.command.flag import InputFlag, Flag
from argenta.command.flag.flags import Flags, InputFlags, UndefinedInputFlags, InvalidValueInputFlags, ValidInputFlags
from argenta.command.flag.flags import Flags, InputFlags
from argenta.command.flag.models import PossibleValues, ValidationStatus
from argenta.response.entity import Response
from argenta.router import Router
from argenta.command import Command
from argenta.router.entity import _structuring_input_flags, _validate_command, _validate_func_args # pyright: ignore[reportPrivateUsage]
from argenta.router.exceptions import (TriggerContainSpacesException,
RepeatedFlagNameException,
TooManyTransferredArgsException,
@@ -13,106 +16,98 @@ import re
class TestRouter(unittest.TestCase):
def test_register_command_with_spaces_in_trigger(self):
router = Router()
with self.assertRaises(TriggerContainSpacesException):
router._validate_command(Command(trigger='command with spaces'))
_validate_command(Command(trigger='command with spaces'))
def test_register_command_with_repeated_flags(self):
router = Router()
with self.assertRaises(RepeatedFlagNameException):
router._validate_command(Command(trigger='command', flags=Flags(Flag('test'), Flag('test'))))
_validate_command(Command(trigger='command', flags=Flags([Flag('test'), Flag('test')])))
def test_structuring_input_flags1(self):
router = Router()
cmd = Command('cmd')
input_flags = InputFlags(InputFlag('ssh'))
self.assertEqual(router._structuring_input_flags(cmd, input_flags).undefined_flags, UndefinedInputFlags(InputFlag('ssh')))
input_flags = InputFlags([InputFlag('ssh', input_value=None, status=None)])
self.assertEqual(_structuring_input_flags(cmd, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value=None, status=ValidationStatus.UNDEFINED)]))
def test_structuring_input_flags2(self):
router = Router()
cmd = Command('cmd')
input_flags = InputFlags(InputFlag('ssh', value='some'))
self.assertEqual(router._structuring_input_flags(cmd, input_flags).undefined_flags, UndefinedInputFlags(InputFlag('ssh', value='some')))
input_flags = InputFlags([InputFlag('ssh', input_value='some', status=None)])
self.assertEqual(_structuring_input_flags(cmd, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='some', status=ValidationStatus.UNDEFINED)]))
def test_structuring_input_flags3(self):
router = Router()
cmd = Command('cmd', flags=Flag('port'))
input_flags = InputFlags(InputFlag('ssh', value='some2'))
self.assertEqual(router._structuring_input_flags(cmd, input_flags).undefined_flags, UndefinedInputFlags(InputFlag('ssh', value='some2')))
input_flags = InputFlags([InputFlag('ssh', input_value='some2', status=None)])
self.assertEqual(_structuring_input_flags(cmd, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='some2', status=ValidationStatus.UNDEFINED)]))
def test_structuring_input_flags4(self):
router = Router()
command = Command('cmd', flags=Flag('ssh', possible_values=False))
input_flags = InputFlags(InputFlag('ssh', value='some3'))
self.assertEqual(router._structuring_input_flags(command, input_flags).invalid_value_flags, InvalidValueInputFlags(InputFlag('ssh', value='some3')))
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
input_flags = InputFlags([InputFlag('ssh', input_value='some3', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='some3', status=ValidationStatus.INVALID)]))
def test_structuring_input_flags5(self):
router = Router()
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'some[1-5]$')))
input_flags = InputFlags(InputFlag('ssh', value='some40'))
self.assertEqual(router._structuring_input_flags(command, input_flags).invalid_value_flags, InvalidValueInputFlags(InputFlag('ssh', value='some40')))
input_flags = InputFlags([InputFlag('ssh', input_value='some40', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='some40', status=ValidationStatus.INVALID)]))
def test_structuring_input_flags6(self):
router = Router()
command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
input_flags = InputFlags(InputFlag('ssh', value='example2'))
self.assertEqual(router._structuring_input_flags(command, input_flags).invalid_value_flags, InvalidValueInputFlags(InputFlag('ssh', value='example2')))
input_flags = InputFlags([InputFlag('ssh', input_value='example2', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='example2', status=ValidationStatus.INVALID)]))
def test_structuring_input_flags7(self):
command = Command('cmd', flags=Flag('port'))
input_flags = InputFlags(InputFlag('port', value='some2'))
self.assertEqual(Router()._structuring_input_flags(command, input_flags).valid_flags, ValidInputFlags(InputFlag('port', value='some2')))
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)]))
def test_structuring_input_flags8(self):
command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3']))
input_flags = InputFlags(InputFlag('port', value='some2'))
self.assertEqual(Router()._structuring_input_flags(command, input_flags).valid_flags, ValidInputFlags(InputFlag('port', value='some2')))
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)]))
def test_structuring_input_flags9(self):
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$')))
input_flags = InputFlags(InputFlag('ssh', value='more5'))
self.assertEqual(Router()._structuring_input_flags(command, input_flags).valid_flags, ValidInputFlags(InputFlag('ssh', value='more5')))
input_flags = InputFlags([InputFlag('ssh', input_value='more5', status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value='more5', status=ValidationStatus.VALID)]))
def test_structuring_input_flags10(self):
command = Command('cmd', flags=Flag('ssh', possible_values=False))
input_flags = InputFlags(InputFlag('ssh'))
self.assertEqual(Router()._structuring_input_flags(command, input_flags).valid_flags, ValidInputFlags(InputFlag('ssh')))
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
input_flags = InputFlags([InputFlag('ssh', input_value=None, status=None)])
self.assertEqual(_structuring_input_flags(command, input_flags).input_flags, InputFlags([InputFlag('ssh', input_value=None, status=ValidationStatus.VALID)]))
def test_validate_incorrect_func_args1(self):
def handler():
pass
with self.assertRaises(RequiredArgumentNotPassedException):
Router()._validate_func_args(handler)
_validate_func_args(handler) # pyright: ignore[reportArgumentType]
def test_validate_incorrect_func_args2(self):
def handler(args, kwargs):
def handler(args, kwargs): # pyright: ignore[reportMissingParameterType, reportUnknownParameterType]
pass
with self.assertRaises(TooManyTransferredArgsException):
Router()._validate_func_args(handler)
_validate_func_args(handler) # pyright: ignore[reportArgumentType]
def test_get_router_aliases(self):
router = Router()
@router.command(Command('some', aliases=['test', 'case']))
def handler(response):
def handler(response: Response) -> None: # pyright: ignore[reportUnusedFunction]
pass
self.assertListEqual(router.get_aliases(), ['test', 'case'])
self.assertListEqual(router.aliases, ['test', 'case'])
def test_get_router_aliases2(self):
router = Router()
@router.command(Command('some', aliases=['test', 'case']))
def handler(response):
def handler(response: Response): # pyright: ignore[reportUnusedFunction]
pass
@router.command(Command('ext', aliases=['more', 'foo']))
def handler2(response):
def handler2(response: Response): # pyright: ignore[reportUnusedFunction]
pass
self.assertListEqual(router.get_aliases(), ['test', 'case', 'more', 'foo'])
self.assertListEqual(router.aliases, ['test', 'case', 'more', 'foo'])
def test_get_router_aliases3(self):
router = Router()
@router.command(Command('some'))
def handler(response):
def handler(response: Response): # pyright: ignore[reportUnusedFunction]
pass
self.assertListEqual(router.get_aliases(), [])
self.assertListEqual(router.aliases, [])