mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
starting refactor tests
This commit is contained in:
+4
-5
@@ -13,10 +13,9 @@ from argenta.orchestrator import Orchestrator
|
||||
from argenta.command.models import InputCommand
|
||||
import inspect
|
||||
|
||||
def some():
|
||||
ellipsis
|
||||
global inspect
|
||||
pass
|
||||
|
||||
inv = InvalidValueInputFlags(InputFlag('test'))
|
||||
inve = InvalidValueInputFlags(InputFlag('test'))
|
||||
|
||||
|
||||
print(inspect.getsourcelines(some))
|
||||
print(inv == inve)
|
||||
@@ -13,7 +13,7 @@ console = Console()
|
||||
|
||||
|
||||
@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.undefined_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'))
|
||||
def command_start_solving(response: str):
|
||||
def command_start_solving(response: Response):
|
||||
print(response.status)
|
||||
print(response.undefined_flags.get_flags())
|
||||
print(response.valid_flags.get_flags())
|
||||
|
||||
@@ -36,6 +36,9 @@ class BaseFlag:
|
||||
"""
|
||||
return self._prefix
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
return self.get_string_entity() == other.get_string_entity()
|
||||
|
||||
|
||||
class Flag(BaseFlag):
|
||||
def __init__(self, name: str,
|
||||
@@ -110,3 +113,6 @@ class InputFlag(BaseFlag):
|
||||
"""
|
||||
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()
|
||||
|
||||
|
||||
@@ -58,6 +58,15 @@ class BaseFlags(Generic[FlagType]):
|
||||
def __getitem__(self, 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
|
||||
|
||||
|
||||
@@ -3,6 +3,11 @@ from argenta.response.status import Status
|
||||
|
||||
|
||||
class Response:
|
||||
__slots__ = ('status',
|
||||
'valid_flags',
|
||||
'undefined_flags',
|
||||
'invalid_value_flags')
|
||||
|
||||
def __init__(self, status: Status = None,
|
||||
valid_flags: ValidInputFlags = ValidInputFlags(),
|
||||
undefined_flags: UndefinedInputFlags = UndefinedInputFlags(),
|
||||
|
||||
@@ -73,9 +73,7 @@ class Router:
|
||||
response: Response = Response()
|
||||
if handle_command.get_registered_flags().get_flags():
|
||||
if input_command_flags.get_flags():
|
||||
flags, status = self._validate_input_flags(handle_command, input_command_flags)
|
||||
response.valid_flags, response.undefined_flags, response.invalid_value_flags = flags
|
||||
response.status = status
|
||||
response: Response = self._structuring_input_flags(handle_command, input_command_flags)
|
||||
command_handler.handling(response)
|
||||
else:
|
||||
response.status = Status.ALL_FLAGS_VALID
|
||||
@@ -92,15 +90,12 @@ class Router:
|
||||
|
||||
|
||||
@staticmethod
|
||||
def _validate_input_flags(handled_command: Command, input_flags: InputFlags) -> tuple[tuple[ValidInputFlags,
|
||||
UndefinedInputFlags,
|
||||
InvalidValueInputFlags],
|
||||
Status]:
|
||||
def _structuring_input_flags(handled_command: Command, input_flags: InputFlags) -> Response:
|
||||
"""
|
||||
Private. Validates flags of input command
|
||||
:param handled_command: entity of the handled command
|
||||
:param input_flags:
|
||||
:return: is flags of input command valid as bool
|
||||
:return: entity of response as Response
|
||||
"""
|
||||
valid_input_flags: ValidInputFlags = ValidInputFlags()
|
||||
invalid_value_input_flags: InvalidValueInputFlags = InvalidValueInputFlags()
|
||||
@@ -124,7 +119,12 @@ class Router:
|
||||
else:
|
||||
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
|
||||
@@ -160,7 +160,7 @@ class Router:
|
||||
|
||||
arg_annotation: Type = get_annotations(func)[transferred_args[0]]
|
||||
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' [i]but[/i] [bold blue]{arg_annotation}[/bold blue] [i]is specified[/i]', highlight=False)
|
||||
|
||||
|
||||
@@ -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 re
|
||||
|
||||
@@ -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.command import Command
|
||||
from argenta.router.exceptions import (TriggerContainSpacesException,
|
||||
@@ -26,89 +28,75 @@ class TestRouter(unittest.TestCase):
|
||||
|
||||
def test_validate_incorrect_input_flag1(self):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
self.assertEqual(router._validate_input_flags(Command('cmd'), InputFlags(InputFlag('ssh'))), False)
|
||||
cmd = Command('cmd')
|
||||
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):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
self.assertEqual(router._validate_input_flags(Command('cmd'), InputFlags(InputFlag('ssh', value='some'))), False)
|
||||
self.assertEqual(router._structuring_input_flags(Command('cmd'), InputFlags(InputFlag('ssh', value='some'))), False)
|
||||
|
||||
def test_validate_incorrect_input_flag3(self):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
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):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=False))
|
||||
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):
|
||||
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]$')))
|
||||
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):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
|
||||
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):
|
||||
router = Router()
|
||||
router.set_invalid_input_flag_handler(lambda flag: None)
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
|
||||
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):
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
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):
|
||||
command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3']))
|
||||
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):
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$')))
|
||||
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):
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=False))
|
||||
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):
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
def handler():
|
||||
pass
|
||||
with self.assertRaises(RequiredArgumentNotPassedException):
|
||||
Router()._validate_func_args(command, handler)
|
||||
Router()._validate_func_args(handler)
|
||||
|
||||
def test_validate_incorrect_func_args2(self):
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
def handler(args, kwargs):
|
||||
pass
|
||||
with self.assertRaises(TooManyTransferredArgsException):
|
||||
Router()._validate_func_args(command, 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)
|
||||
Router()._validate_func_args(handler)
|
||||
|
||||
def test_get_router_aliases(self):
|
||||
router = Router()
|
||||
@@ -130,7 +118,7 @@ class TestRouter(unittest.TestCase):
|
||||
def test_get_router_aliases3(self):
|
||||
router = Router()
|
||||
@router.command(Command('some'))
|
||||
def handler():
|
||||
def handler(response):
|
||||
pass
|
||||
self.assertListEqual(router.get_aliases(), [])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user