starting refactor tests

This commit is contained in:
2025-05-04 16:40:10 +03:00
parent adf3431388
commit 477f3a7dec
8 changed files with 57 additions and 49 deletions
+4 -5
View File
@@ -13,10 +13,9 @@ from argenta.orchestrator import Orchestrator
from argenta.command.models import InputCommand from argenta.command.models import InputCommand
import inspect import inspect
def some():
ellipsis inv = InvalidValueInputFlags(InputFlag('test'))
global inspect inve = InvalidValueInputFlags(InputFlag('test'))
pass
print(inspect.getsourcelines(some)) print(inv == inve)
+2 -2
View File
@@ -13,7 +13,7 @@ console = Console()
@work_router.command(Command('get', 'Get Help', aliases=['help', 'Get_help'], flags=Flags(PredefinedFlags.PORT, PredefinedFlags.HOST))) @work_router.command(Command('get', 'Get Help', aliases=['help', 'Get_help'], flags=Flags(PredefinedFlags.PORT, PredefinedFlags.HOST)))
def command_help(response: int): def command_help(response: Response):
print(response.status) print(response.status)
print(response.undefined_flags.get_flags()) print(response.undefined_flags.get_flags())
print(response.valid_flags.get_flags()) print(response.valid_flags.get_flags())
@@ -21,7 +21,7 @@ def command_help(response: int):
@work_router.command(Command('run', 'Run All')) @work_router.command(Command('run', 'Run All'))
def command_start_solving(response: str): def command_start_solving(response: Response):
print(response.status) print(response.status)
print(response.undefined_flags.get_flags()) print(response.undefined_flags.get_flags())
print(response.valid_flags.get_flags()) print(response.valid_flags.get_flags())
+6
View File
@@ -36,6 +36,9 @@ class BaseFlag:
""" """
return self._prefix return self._prefix
def __eq__(self, other) -> bool:
return self.get_string_entity() == other.get_string_entity()
class Flag(BaseFlag): class Flag(BaseFlag):
def __init__(self, name: str, def __init__(self, name: str,
@@ -110,3 +113,6 @@ class InputFlag(BaseFlag):
""" """
self._flag_value = value self._flag_value = value
def __eq__(self, other) -> bool:
return self.get_string_entity() == other.get_string_entity() and self.get_value() == other.get_value()
+9
View File
@@ -58,6 +58,15 @@ class BaseFlags(Generic[FlagType]):
def __getitem__(self, item): def __getitem__(self, item):
return self._flags[item] return self._flags[item]
def __eq__(self, other):
if len(self.get_flags()) != len(other.get_flags()):
return False
else:
for flag, other_flag in zip(self.get_flags(), other.get_flags()):
if not flag == other_flag:
return False
return True
class Flags(BaseFlags[Flag]): pass class Flags(BaseFlags[Flag]): pass
+5
View File
@@ -3,6 +3,11 @@ from argenta.response.status import Status
class Response: class Response:
__slots__ = ('status',
'valid_flags',
'undefined_flags',
'invalid_value_flags')
def __init__(self, status: Status = None, def __init__(self, status: Status = None,
valid_flags: ValidInputFlags = ValidInputFlags(), valid_flags: ValidInputFlags = ValidInputFlags(),
undefined_flags: UndefinedInputFlags = UndefinedInputFlags(), undefined_flags: UndefinedInputFlags = UndefinedInputFlags(),
+10 -10
View File
@@ -73,9 +73,7 @@ class Router:
response: Response = Response() response: Response = Response()
if handle_command.get_registered_flags().get_flags(): if handle_command.get_registered_flags().get_flags():
if input_command_flags.get_flags(): if input_command_flags.get_flags():
flags, status = self._validate_input_flags(handle_command, input_command_flags) response: Response = self._structuring_input_flags(handle_command, input_command_flags)
response.valid_flags, response.undefined_flags, response.invalid_value_flags = flags
response.status = status
command_handler.handling(response) command_handler.handling(response)
else: else:
response.status = Status.ALL_FLAGS_VALID response.status = Status.ALL_FLAGS_VALID
@@ -92,15 +90,12 @@ class Router:
@staticmethod @staticmethod
def _validate_input_flags(handled_command: Command, input_flags: InputFlags) -> tuple[tuple[ValidInputFlags, def _structuring_input_flags(handled_command: Command, input_flags: InputFlags) -> Response:
UndefinedInputFlags,
InvalidValueInputFlags],
Status]:
""" """
Private. Validates flags of input command Private. Validates flags of input command
:param handled_command: entity of the handled command :param handled_command: entity of the handled command
:param input_flags: :param input_flags:
:return: is flags of input command valid as bool :return: entity of response as Response
""" """
valid_input_flags: ValidInputFlags = ValidInputFlags() valid_input_flags: ValidInputFlags = ValidInputFlags()
invalid_value_input_flags: InvalidValueInputFlags = InvalidValueInputFlags() invalid_value_input_flags: InvalidValueInputFlags = InvalidValueInputFlags()
@@ -124,7 +119,12 @@ class Router:
else: else:
status = Status.UNDEFINED_AND_INVALID_FLAGS status = Status.UNDEFINED_AND_INVALID_FLAGS
return (valid_input_flags, undefined_input_flags, invalid_value_input_flags), status response = Response(invalid_value_flags=invalid_value_input_flags,
valid_flags=valid_input_flags,
status=status,
undefined_flags=undefined_input_flags,)
return response
@staticmethod @staticmethod
@@ -160,7 +160,7 @@ class Router:
arg_annotation: Type = get_annotations(func)[transferred_args[0]] arg_annotation: Type = get_annotations(func)[transferred_args[0]]
if not arg_annotation is Response: if not arg_annotation is Response:
Console().print(f'\n\nFile "{getsourcefile(func).replace("\\", "/")}", line {getsourcelines(func)[1]+1}\n' Console().print(f'\n\nFile "{getsourcefile(func)}", line {getsourcelines(func)[1]+1}\n'
f'[b red]WARNING:[/b red] [i]The typehint of argument([green]{transferred_args[0]}[/green]) passed to the handler is [/i][blue]{Response}[/blue],' f'[b red]WARNING:[/b red] [i]The typehint of argument([green]{transferred_args[0]}[/green]) passed to the handler is [/i][blue]{Response}[/blue],'
f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False) f' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False)
+2 -1
View File
@@ -1,4 +1,5 @@
from argenta.command.flag.models import Flag, InputFlag, InputFlags, Flags from argenta.command.flag import Flag, InputFlag
from argenta.command.flags import InputFlags, Flags
import unittest import unittest
import re import re
+19 -31
View File
@@ -1,4 +1,6 @@
from argenta.command.flag import InputFlags, InputFlag, Flag, Flags from argenta.command.flag import InputFlag, Flag
from argenta.command.flags import Flags, InputFlags, UndefinedInputFlags
from argenta.response import Response
from argenta.router import Router from argenta.router import Router
from argenta.command import Command from argenta.command import Command
from argenta.router.exceptions import (TriggerContainSpacesException, from argenta.router.exceptions import (TriggerContainSpacesException,
@@ -26,89 +28,75 @@ class TestRouter(unittest.TestCase):
def test_validate_incorrect_input_flag1(self): def test_validate_incorrect_input_flag1(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None) cmd = Command('cmd')
self.assertEqual(router._validate_input_flags(Command('cmd'), InputFlags(InputFlag('ssh'))), False) input_flags = InputFlags(InputFlag('ssh'))
self.assertEqual(router._structuring_input_flags(cmd, input_flags).undefined_flags, UndefinedInputFlags(InputFlag('ssh')))
def test_validate_incorrect_input_flag2(self): def test_validate_incorrect_input_flag2(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None) self.assertEqual(router._structuring_input_flags(Command('cmd'), InputFlags(InputFlag('ssh', value='some'))), False)
self.assertEqual(router._validate_input_flags(Command('cmd'), InputFlags(InputFlag('ssh', value='some'))), False)
def test_validate_incorrect_input_flag3(self): def test_validate_incorrect_input_flag3(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None)
command = Command('cmd', flags=Flag('port')) command = Command('cmd', flags=Flag('port'))
input_flags = InputFlags(InputFlag('ssh', value='some2')) input_flags = InputFlags(InputFlag('ssh', value='some2'))
self.assertEqual(router._validate_input_flags(command, input_flags), False) self.assertEqual(router._structuring_input_flags(command, input_flags), False)
def test_validate_incorrect_input_flag4(self): def test_validate_incorrect_input_flag4(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None)
command = Command('cmd', flags=Flag('ssh', possible_values=False)) command = Command('cmd', flags=Flag('ssh', possible_values=False))
input_flags = InputFlags(InputFlag('ssh', value='some3')) input_flags = InputFlags(InputFlag('ssh', value='some3'))
self.assertEqual(router._validate_input_flags(command, input_flags), False) self.assertEqual(router._structuring_input_flags(command, input_flags), False)
def test_validate_incorrect_input_flag5(self): def test_validate_incorrect_input_flag5(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None)
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'some[1-5]$'))) command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'some[1-5]$')))
input_flags = InputFlags(InputFlag('ssh', value='some40')) input_flags = InputFlags(InputFlag('ssh', value='some40'))
self.assertEqual(router._validate_input_flags(command, input_flags), False) self.assertEqual(router._structuring_input_flags(command, input_flags), False)
def test_validate_incorrect_input_flag6(self): def test_validate_incorrect_input_flag6(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None)
command = Command('cmd', flags=Flag('ssh', possible_values=['example'])) command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
input_flags = InputFlags(InputFlag('ssh', value='example2')) input_flags = InputFlags(InputFlag('ssh', value='example2'))
self.assertEqual(router._validate_input_flags(command, input_flags), False) self.assertEqual(router._structuring_input_flags(command, input_flags), False)
def test_validate_incorrect_input_flag7(self): def test_validate_incorrect_input_flag7(self):
router = Router() router = Router()
router.set_invalid_input_flag_handler(lambda flag: None)
command = Command('cmd', flags=Flag('ssh', possible_values=['example'])) command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
input_flags = InputFlags(InputFlag('ssh')) input_flags = InputFlags(InputFlag('ssh'))
self.assertEqual(router._validate_input_flags(command, input_flags), False) self.assertEqual(router._structuring_input_flags(command, input_flags), False)
def test_validate_correct_input_flag1(self): def test_validate_correct_input_flag1(self):
command = Command('cmd', flags=Flag('port')) command = Command('cmd', flags=Flag('port'))
input_flags = InputFlags(InputFlag('port', value='some2')) input_flags = InputFlags(InputFlag('port', value='some2'))
self.assertEqual(Router()._validate_input_flags(command, input_flags), True) self.assertEqual(Router()._structuring_input_flags(command, input_flags), True)
def test_validate_correct_input_flag2(self): def test_validate_correct_input_flag2(self):
command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3'])) command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3']))
input_flags = InputFlags(InputFlag('port', value='some2')) input_flags = InputFlags(InputFlag('port', value='some2'))
self.assertEqual(Router()._validate_input_flags(command, input_flags), True) self.assertEqual(Router()._structuring_input_flags(command, input_flags), True)
def test_validate_correct_input_flag3(self): def test_validate_correct_input_flag3(self):
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$'))) command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$')))
input_flags = InputFlags(InputFlag('ssh', value='more5')) input_flags = InputFlags(InputFlag('ssh', value='more5'))
self.assertEqual(Router()._validate_input_flags(command, input_flags), True) self.assertEqual(Router()._structuring_input_flags(command, input_flags), True)
def test_validate_correct_input_flag4(self): def test_validate_correct_input_flag4(self):
command = Command('cmd', flags=Flag('ssh', possible_values=False)) command = Command('cmd', flags=Flag('ssh', possible_values=False))
input_flags = InputFlags(InputFlag('ssh')) input_flags = InputFlags(InputFlag('ssh'))
self.assertEqual(Router()._validate_input_flags(command, input_flags), True) self.assertEqual(Router()._structuring_input_flags(command, input_flags), True)
def test_validate_incorrect_func_args1(self): def test_validate_incorrect_func_args1(self):
command = Command('cmd', flags=Flag('port'))
def handler(): def handler():
pass pass
with self.assertRaises(RequiredArgumentNotPassedException): with self.assertRaises(RequiredArgumentNotPassedException):
Router()._validate_func_args(command, handler) Router()._validate_func_args(handler)
def test_validate_incorrect_func_args2(self): def test_validate_incorrect_func_args2(self):
command = Command('cmd', flags=Flag('port'))
def handler(args, kwargs): def handler(args, kwargs):
pass pass
with self.assertRaises(TooManyTransferredArgsException): with self.assertRaises(TooManyTransferredArgsException):
Router()._validate_func_args(command, handler) Router()._validate_func_args(handler)
def test_validate_incorrect_func_args3(self):
command = Command('cmd')
def handler(args):
pass
with self.assertRaises(TooManyTransferredArgsException):
Router()._validate_func_args(command, handler)
def test_get_router_aliases(self): def test_get_router_aliases(self):
router = Router() router = Router()
@@ -130,7 +118,7 @@ class TestRouter(unittest.TestCase):
def test_get_router_aliases3(self): def test_get_router_aliases3(self):
router = Router() router = Router()
@router.command(Command('some')) @router.command(Command('some'))
def handler(): def handler(response):
pass pass
self.assertListEqual(router.get_aliases(), []) self.assertListEqual(router.get_aliases(), [])