mirror of
https://github.com/koloideal/Argenta.git
synced 2026-06-10 10:05:28 +03:00
better testsssssss
This commit is contained in:
+168
-108
@@ -1,4 +1,5 @@
|
||||
import re
|
||||
|
||||
import pytest
|
||||
from pytest import CaptureFixture
|
||||
|
||||
@@ -9,131 +10,48 @@ from argenta.command.flag.models import PossibleValues, ValidationStatus
|
||||
from argenta.response.entity import Response
|
||||
from argenta.router import Router
|
||||
from argenta.router.entity import _structuring_input_flags, _validate_func_args # pyright: ignore[reportPrivateUsage]
|
||||
from argenta.router.exceptions import (RepeatedFlagNameException,
|
||||
RequiredArgumentNotPassedException,
|
||||
TriggerContainSpacesException)
|
||||
from argenta.router.exceptions import (
|
||||
RepeatedFlagNameException,
|
||||
RequiredArgumentNotPassedException,
|
||||
TriggerContainSpacesException,
|
||||
)
|
||||
|
||||
|
||||
def test_register_command_with_spaces_in_trigger():
|
||||
# ============================================================================
|
||||
# Tests for command validation
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_validate_command_raises_error_for_trigger_with_spaces() -> None:
|
||||
router = Router()
|
||||
with pytest.raises(TriggerContainSpacesException):
|
||||
router._validate_command(Command(trigger='command with spaces'))
|
||||
|
||||
def test_register_command_with_repeated_flags():
|
||||
|
||||
def test_validate_command_raises_error_for_repeated_flag_names() -> None:
|
||||
router = Router()
|
||||
with pytest.raises(RepeatedFlagNameException):
|
||||
router._validate_command(Command(trigger='command', flags=Flags([Flag('test'), Flag('test')])))
|
||||
|
||||
def test_structuring_input_flags1():
|
||||
cmd = Command('cmd')
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='', status=ValidationStatus.UNDEFINED)])
|
||||
|
||||
def test_structuring_input_flags2():
|
||||
cmd = Command('cmd')
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some', status=ValidationStatus.UNDEFINED)])
|
||||
# ============================================================================
|
||||
# Tests for function argument validation
|
||||
# ============================================================================
|
||||
|
||||
def test_structuring_input_flags3():
|
||||
cmd = Command('cmd', flags=Flag('port'))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some2', status=ValidationStatus.UNDEFINED)])
|
||||
|
||||
def test_structuring_input_flags4():
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some3', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some3', status=ValidationStatus.INVALID)])
|
||||
|
||||
def test_structuring_input_flags5():
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'some[1-5]$')))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some40', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some40', status=ValidationStatus.INVALID)])
|
||||
|
||||
def test_structuring_input_flags6():
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='example2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='example2', status=ValidationStatus.INVALID)])
|
||||
|
||||
def test_structuring_input_flags7():
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)])
|
||||
|
||||
def test_structuring_input_flags8():
|
||||
command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3']))
|
||||
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)])
|
||||
|
||||
def test_structuring_input_flags9():
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$')))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='more5', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='more5', status=ValidationStatus.VALID)])
|
||||
|
||||
def test_structuring_input_flags10():
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='', status=ValidationStatus.VALID)])
|
||||
|
||||
def test_validate_incorrect_func_args1():
|
||||
def handler():
|
||||
def test_validate_func_args_raises_error_for_missing_response_parameter() -> None:
|
||||
def handler() -> None:
|
||||
pass
|
||||
with pytest.raises(RequiredArgumentNotPassedException):
|
||||
_validate_func_args(handler) # pyright: ignore[reportArgumentType]
|
||||
_validate_func_args(handler) # pyright: ignore[reportArgumentType]
|
||||
|
||||
def test_get_router_aliases():
|
||||
router = Router()
|
||||
@router.command(Command('some', aliases={'test', 'case'}))
|
||||
def handler(response: Response) -> None:
|
||||
|
||||
def test_validate_func_args_prints_warning_for_wrong_type_hint(capsys: CaptureFixture[str]) -> None:
|
||||
class NotResponse:
|
||||
pass
|
||||
assert router.aliases == {'test', 'case'}
|
||||
|
||||
def test_get_router_aliases2():
|
||||
router = Router()
|
||||
@router.command(Command('some', aliases={'test', 'case'}))
|
||||
def handler(response: Response):
|
||||
def func(_response: NotResponse) -> None:
|
||||
pass
|
||||
@router.command(Command('ext', aliases={'more', 'foo'}))
|
||||
def handler2(response: Response):
|
||||
pass
|
||||
assert router.aliases == {'test', 'case', 'more', 'foo'}
|
||||
|
||||
def test_get_router_aliases3():
|
||||
router = Router()
|
||||
@router.command(Command('some'))
|
||||
def handler(response: Response):
|
||||
pass
|
||||
assert router.aliases == set()
|
||||
|
||||
def test_find_appropiate_handler(capsys: pytest.CaptureFixture[str]):
|
||||
router = Router()
|
||||
|
||||
@router.command(Command('hello', aliases={'hi'}))
|
||||
def handler(res: Response):
|
||||
print("Hello World!")
|
||||
|
||||
router.finds_appropriate_handler(InputCommand('hi'))
|
||||
|
||||
output = capsys.readouterr()
|
||||
|
||||
assert "Hello World!" in output.out
|
||||
|
||||
def test_find_appropiate_handler2(capsys: CaptureFixture[str]):
|
||||
router = Router()
|
||||
|
||||
@router.command(Command('hello', flags=Flag('flag'), aliases={'hi'}))
|
||||
def handler(res: Response):
|
||||
print("Hello World!")
|
||||
|
||||
router.finds_appropriate_handler(InputCommand('hi'))
|
||||
|
||||
output = capsys.readouterr()
|
||||
|
||||
assert "Hello World!" in output.out
|
||||
|
||||
def test_wrong_typehint(capsys: pytest.CaptureFixture[str]):
|
||||
class NotResponse: pass
|
||||
|
||||
def func(response: NotResponse): pass
|
||||
|
||||
_validate_func_args(func)
|
||||
|
||||
@@ -141,8 +59,150 @@ def test_wrong_typehint(capsys: pytest.CaptureFixture[str]):
|
||||
|
||||
assert "WARNING" in output.out
|
||||
|
||||
def test_missing_typehint(capsys: pytest.CaptureFixture[str]):
|
||||
def func(response): pass # pyright: ignore[reportMissingParameterType, reportUnknownParameterType]
|
||||
|
||||
def test_validate_func_args_accepts_missing_type_hint(capsys: CaptureFixture[str]) -> None:
|
||||
def func(response) -> None: # pyright: ignore[reportMissingParameterType, reportUnknownParameterType]
|
||||
pass
|
||||
_validate_func_args(func) # pyright: ignore[reportUnknownArgumentType]
|
||||
output = capsys.readouterr()
|
||||
assert output.out == ''
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tests for input flag structuring - undefined flags
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_unregistered_flag_as_undefined() -> None:
|
||||
cmd = Command('cmd')
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='', status=ValidationStatus.UNDEFINED)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_unregistered_flag_with_value_as_undefined() -> None:
|
||||
cmd = Command('cmd')
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some', status=ValidationStatus.UNDEFINED)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_undefined_when_different_flag_registered() -> None:
|
||||
cmd = Command('cmd', flags=Flag('port'))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(cmd, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some2', status=ValidationStatus.UNDEFINED)])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tests for input flag structuring - invalid flags
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_invalid_when_value_provided_for_neither() -> None:
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some3', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some3', status=ValidationStatus.INVALID)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_invalid_when_value_not_matching_regex() -> None:
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'some[1-5]$')))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='some40', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='some40', status=ValidationStatus.INVALID)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_invalid_when_value_not_in_list() -> None:
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=['example']))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='example2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='example2', status=ValidationStatus.INVALID)])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tests for input flag structuring - valid flags
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_registered_flag_as_valid() -> None:
|
||||
command = Command('cmd', flags=Flag('port'))
|
||||
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_valid_when_value_in_list() -> None:
|
||||
command = Command('cmd', flags=Flag('port', possible_values=['some2', 'some3']))
|
||||
input_flags = InputFlags([InputFlag('port', input_value='some2', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('port', input_value='some2', status=ValidationStatus.VALID)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_valid_when_value_matches_regex() -> None:
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=re.compile(r'more[1-5]$')))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='more5', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='more5', status=ValidationStatus.VALID)])
|
||||
|
||||
|
||||
def test_structuring_input_flags_marks_flag_valid_when_empty_value_for_neither() -> None:
|
||||
command = Command('cmd', flags=Flag('ssh', possible_values=PossibleValues.NEITHER))
|
||||
input_flags = InputFlags([InputFlag('ssh', input_value='', status=None)])
|
||||
assert _structuring_input_flags(command, input_flags).input_flags == InputFlags([InputFlag('ssh', input_value='', status=ValidationStatus.VALID)])
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tests for router aliases
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_router_aliases_returns_command_aliases() -> None:
|
||||
router = Router()
|
||||
@router.command(Command('some', aliases={'test', 'case'}))
|
||||
def handler(_response: Response) -> None:
|
||||
pass
|
||||
assert router.aliases == {'test', 'case'}
|
||||
|
||||
|
||||
def test_router_aliases_returns_combined_aliases_from_multiple_commands() -> None:
|
||||
router = Router()
|
||||
@router.command(Command('some', aliases={'test', 'case'}))
|
||||
def handler(_response: Response) -> None:
|
||||
pass
|
||||
@router.command(Command('ext', aliases={'more', 'foo'}))
|
||||
def handler2(_response: Response) -> None:
|
||||
pass
|
||||
assert router.aliases == {'test', 'case', 'more', 'foo'}
|
||||
|
||||
|
||||
def test_router_aliases_returns_empty_set_when_no_aliases() -> None:
|
||||
router = Router()
|
||||
@router.command(Command('some'))
|
||||
def handler(_response: Response) -> None:
|
||||
pass
|
||||
assert router.aliases == set()
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Tests for handler finding and execution
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def test_finds_appropriate_handler_executes_handler_by_alias(capsys: CaptureFixture[str]) -> None:
|
||||
router = Router()
|
||||
|
||||
@router.command(Command('hello', aliases={'hi'}))
|
||||
def handler(_res: Response) -> None:
|
||||
print("Hello World!")
|
||||
|
||||
router.finds_appropriate_handler(InputCommand('hi'))
|
||||
|
||||
output = capsys.readouterr()
|
||||
|
||||
assert "Hello World!" in output.out
|
||||
|
||||
|
||||
def test_finds_appropriate_handler_executes_handler_with_flags_by_alias(capsys: CaptureFixture[str]) -> None:
|
||||
router = Router()
|
||||
|
||||
@router.command(Command('hello', flags=Flag('flag'), aliases={'hi'}))
|
||||
def handler(_res: Response) -> None:
|
||||
print("Hello World!")
|
||||
|
||||
router.finds_appropriate_handler(InputCommand('hi'))
|
||||
|
||||
output = capsys.readouterr()
|
||||
|
||||
assert "Hello World!" in output.out
|
||||
|
||||
Reference in New Issue
Block a user