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
+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)