new models, a model is passed to the command handler instead of a dictionary, removal of checks for intersection of processed triggers in handlers and much, much more

This commit is contained in:
2025-03-31 19:14:42 +03:00
parent 2918bc9f81
commit 5c6fa5151a
24 changed files with 283 additions and 279 deletions
@@ -7,8 +7,8 @@ import re
from argenta.app import App
from argenta.command import Command
from argenta.router import Router
from argenta.command.flag.registered_flag import FlagsGroup
from argenta.command.flag.registered_flag.defaults import DefaultFlags
from argenta.command.flag.models import Flags
from argenta.command.flag.defaults import DefaultFlags
@@ -64,7 +64,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nUndefined or incorrect input registered_flag: --help\n', output)
self.assertIn('\nUndefined or incorrect input flag: --help\n', output)
@patch("builtins.input", side_effect=["test --port 22", "q"])
@@ -82,14 +82,14 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nUndefined or incorrect input registered_flag: --port 22\n', output)
self.assertIn('\nUndefined or incorrect input flag: --port 22\n', output)
@patch("builtins.input", side_effect=["test --host 192.168.32.1 --port 132", "q"])
@patch("sys.stdout", new_callable=io.StringIO)
def test_input_correct_command_with_one_correct_flag_an_one_incorrect_flag(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
flags = FlagsGroup(DefaultFlags.HOST)
flags = Flags(DefaultFlags.HOST)
@router.command(Command('test', flags=flags))
def test(args: dict):
@@ -101,7 +101,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nUndefined or incorrect input registered_flag: --port 132\n', output)
self.assertIn('\nUndefined or incorrect input flag: --port 132\n', output)
@patch("builtins.input", side_effect=["test", "some", "q"])
@@ -159,7 +159,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn("\nIncorrect registered_flag syntax: \"test 535 --port\"\n", output)
self.assertIn("\nIncorrect flag syntax: \"test 535 --port\"\n", output)
@patch("builtins.input", side_effect=["", "q"])
@@ -5,10 +5,10 @@ import io
import re
from argenta.app import App
from argenta.command import Command
from argenta.command.models import Command
from argenta.router import Router
from argenta.command.flag.registered_flag import Flag, FlagsGroup
from argenta.command.flag.registered_flag.defaults import DefaultFlags
from argenta.command.flag.models import Flag, Flags, InputFlags
from argenta.command.flag.defaults import DefaultFlags
@@ -56,8 +56,8 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
flag = Flag('help', '--', False)
@router.command(Command('test', flags=flag))
def test(args: dict):
print(f'\nhelp for {args['help']['name']} registered_flag\n')
def test(args: InputFlags):
print(f'\nhelp for {args.get_flag('help').get_name()} flag\n')
app = App()
app.include_router(router)
@@ -65,7 +65,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nhelp for help registered_flag\n', output)
self.assertIn('\nhelp for help flag\n', output)
@patch("builtins.input", side_effect=["test --port 22", "q"])
@patch("sys.stdout", new_callable=io.StringIO)
@@ -74,8 +74,8 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
flag = Flag('port', '--', re.compile(r'^\d{1,5}$'))
@router.command(Command('test', flags=flag))
def test(args: dict):
print(f'registered_flag value for {args['port']['name']} registered_flag : {args["port"]["value"]}')
def test(args: InputFlags):
print(f'flag value for {args.get_flag('port').get_name()} flag : {args.get_flag('port').get_value()}')
app = App()
app.include_router(router)
@@ -83,7 +83,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nregistered_flag value for port registered_flag : 22\n', output)
self.assertIn('\nflag value for port flag : 22\n', output)
@patch("builtins.input", side_effect=["test -h", "q"])
@@ -94,7 +94,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
@router.command(Command('test', flags=flag))
def test(args: dict):
print(f'help for {args['h']['name']} registered_flag')
print(f'help for {args[0].get_name()} flag')
app = App()
app.include_router(router)
@@ -102,7 +102,7 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
output = mock_stdout.getvalue()
self.assertIn('\nhelp for h registered_flag\n', output)
self.assertIn('\nhelp for h flag\n', output)
@patch("builtins.input", side_effect=["test --info", "q"])
@@ -112,8 +112,8 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
flag = DefaultFlags.INFO
@router.command(Command('test', flags=flag))
def test(args: dict):
if args.get('info'):
def test(args: InputFlags):
if args.get_flag('info'):
print('info about test command')
app = App()
@@ -132,8 +132,8 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
flag = DefaultFlags.HOST
@router.command(Command('test', flags=flag))
def test(args: dict):
print(f'connecting to host {args["host"]["value"]}')
def test(args: InputFlags):
print(f'connecting to host {args[0].get_value()}')
app = App()
app.include_router(router)
@@ -148,11 +148,11 @@ class TestSystemHandlerNormalWork(unittest.TestCase):
@patch("sys.stdout", new_callable=io.StringIO)
def test_input_correct_command_with_two_flags(self, mock_stdout: _io.StringIO, magick_mock: MagicMock):
router = Router()
flags = FlagsGroup(DefaultFlags.HOST, DefaultFlags.PORT)
flags = Flags(DefaultFlags.HOST, DefaultFlags.PORT)
@router.command(Command('test', flags=flags))
def test(args: dict):
print(f'connecting to host {args["host"]["value"]} and port {args["port"]["value"]}')
def test(args: InputFlags):
print(f'connecting to host {args[0].get_value()} and port {args[1].get_value()}')
app = App()
app.include_router(router)
+6 -9
View File
@@ -1,4 +1,4 @@
from argenta.command import Command
from argenta.command.models import InputCommand
from argenta.command.exceptions import (UnprocessedInputFlagException,
RepeatedInputFlagsException,
EmptyInputCommandException)
@@ -6,22 +6,19 @@ from argenta.command.exceptions import (UnprocessedInputFlagException,
import unittest
class TestCommand(unittest.TestCase):
class TestInputCommand(unittest.TestCase):
def test_parse_correct_raw_command(self):
self.assertEqual(Command.parse_input_command('ssh --host 192.168.0.3').get_trigger(), 'ssh')
self.assertEqual(InputCommand.parse_input_command('ssh --host 192.168.0.3').get_trigger(), 'ssh')
def test_parse_raw_command_without_flag_name_with_value(self):
with self.assertRaises(UnprocessedInputFlagException):
Command.parse_input_command('ssh 192.168.0.3')
InputCommand.parse_input_command('ssh 192.168.0.3')
def test_parse_raw_command_with_repeated_flag_name(self):
with self.assertRaises(RepeatedInputFlagsException):
Command.parse_input_command('ssh --host 192.168.0.3 --host 172.198.0.43')
InputCommand.parse_input_command('ssh --host 192.168.0.3 --host 172.198.0.43')
def test_parse_empty_raw_command(self):
with self.assertRaises(EmptyInputCommandException):
Command.parse_input_command('')
def test_get_command_description(self):
self.assertEqual(Command(trigger='test', description='test description').get_description(), 'test description')
InputCommand.parse_input_command('')
+18 -18
View File
@@ -1,4 +1,4 @@
from argenta.command.flag.registered_flag import Flag
from argenta.command.flag.models import Flag, InputFlag
import unittest
import re
@@ -6,66 +6,66 @@ import re
class TestFlag(unittest.TestCase):
def test_get_string_entity(self):
self.assertEqual(Flag(flag_name='test').get_string_entity(),
self.assertEqual(Flag(name='test').get_string_entity(),
'--test')
def test_get_string_entity2(self):
self.assertEqual(Flag(flag_name='test',
flag_prefix='---').get_string_entity(),
self.assertEqual(Flag(name='test',
prefix='---').get_string_entity(),
'---test')
def test_get_flag_name(self):
self.assertEqual(Flag(flag_name='test').get_flag_name(),
self.assertEqual(Flag(name='test').get_name(),
'test')
def test_get_flag_prefix(self):
self.assertEqual(Flag(flag_name='test').get_flag_prefix(),
self.assertEqual(Flag(name='test').get_prefix(),
'--')
def test_get_flag_prefix2(self):
self.assertEqual(Flag(flag_name='test',
flag_prefix='--').get_flag_prefix(),
self.assertEqual(Flag(name='test',
prefix='--').get_prefix(),
'--')
def test_get_flag_value_without_set(self):
self.assertEqual(Flag(flag_name='test').get_value(),
self.assertEqual(InputFlag(name='test').get_value(),
None)
def test_get_flag_value_with_set(self):
flag = Flag(flag_name='test')
flag = InputFlag(name='test')
flag.set_value('example')
self.assertEqual(flag.get_value(), 'example')
def test_validate_incorrect_flag_value_with_list_of_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3'])
flag = Flag(name='test', possible_values=['1', '2', '3'])
self.assertEqual(flag.validate_input_flag_value('bad value'), False)
def test_validate_correct_flag_value_with_list_of_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=['1', '2', '3'])
flag = Flag(name='test', possible_values=['1', '2', '3'])
self.assertEqual(flag.validate_input_flag_value('1'), True)
def test_validate_incorrect_flag_value_with_pattern_of_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+'))
flag = Flag(name='test', possible_values=re.compile(r'192.168.\d+.\d+'))
self.assertEqual(flag.validate_input_flag_value('152.123.9.8'), False)
def test_validate_correct_flag_value_with_pattern_of_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=re.compile(r'192.168.\d+.\d+'))
flag = Flag(name='test', possible_values=re.compile(r'192.168.\d+.\d+'))
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(flag_name='test', possible_flag_values=False)
flag = Flag(name='test', possible_values=False)
self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_correct_empty_flag_value_with_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=True)
flag = Flag(name='test', possible_values=True)
self.assertEqual(flag.validate_input_flag_value(None), True)
def test_validate_incorrect_random_flag_value_without_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=False)
flag = Flag(name='test', possible_values=False)
self.assertEqual(flag.validate_input_flag_value('random value'), False)
def test_validate_correct_random_flag_value_with_possible_flag_values(self):
flag = Flag(flag_name='test', possible_flag_values=True)
flag = Flag(name='test', possible_values=True)
self.assertEqual(flag.validate_input_flag_value('random value'), True)
+5 -28
View File
@@ -1,11 +1,11 @@
from argenta.command.flag.registered_flag import Flag, FlagsGroup
from argenta.command.flag.models import Flag, Flags
import unittest
class TestFlagsGroup(unittest.TestCase):
class TestFlags(unittest.TestCase):
def test_get_flags(self):
flags = FlagsGroup()
flags = Flags()
list_of_flags = [
Flag('test1'),
Flag('test2'),
@@ -16,34 +16,11 @@ class TestFlagsGroup(unittest.TestCase):
list_of_flags)
def test_add_flag(self):
flags = FlagsGroup()
flags = Flags()
flags.add_flag(Flag('test'))
self.assertEqual(len(flags.get_flags()), 1)
def test_add_flags(self):
flags = FlagsGroup()
flags = Flags()
flags.add_flags([Flag('test'), Flag('test2')])
self.assertEqual(len(flags.get_flags()), 2)
def test_unparse_flags_to_dict(self):
list_of_flags = [
Flag('test1'),
Flag('test2'),
Flag('test3'),
]
flags = FlagsGroup(*list_of_flags)
serialized_flags = flags.unparse_to_dict()
needed_result = {'test1': {'name': 'test1',
'prefix': '--',
'string_entity': '--test1',
'value': None},
'test2': {'name': 'test2',
'prefix': '--',
'string_entity': '--test2',
'value': None},
'test3': {'name': 'test3',
'prefix': '--',
'string_entity': '--test3',
'value': None}}
self.assertDictEqual(serialized_flags, needed_result)