This commit is contained in:
2025-03-15 00:08:39 +03:00
parent 09c40978a1
commit baaf0e25f3
11 changed files with 77 additions and 9 deletions
View File
+20
View File
@@ -0,0 +1,20 @@
from argenta.app import App
from argenta.app.exceptions import (InvalidDescriptionMessagePatternException,
NoRegisteredRoutersException)
import unittest
class TestApp(unittest.TestCase):
def test_set_invalid_description_message_pattern(self):
with self.assertRaises(InvalidDescriptionMessagePatternException):
App().set_description_message_pattern('Invalid description pattern')
def test_set_invalid_description_message_pattern2(self):
with self.assertRaises(InvalidDescriptionMessagePatternException):
App().set_description_message_pattern('Invalid {desription} description {comand} pattern')
def test_no_registered_router(self):
with self.assertRaises(NoRegisteredRoutersException):
App()._validate_number_of_routers()
+27
View File
@@ -0,0 +1,27 @@
from argenta.command import Command
from argenta.command.exceptions import (UnprocessedInputFlagException,
RepeatedInputFlagsException,
EmptyInputCommandException)
import unittest
class TestCommand(unittest.TestCase):
def test_parse_correct_raw_command(self):
self.assertEqual(Command.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')
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')
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')
+91
View File
@@ -0,0 +1,91 @@
from argenta.command.flag import Flag
import unittest
import re
class TestFlag(unittest.TestCase):
def test_get_string_entity(self):
self.assertEqual(Flag(flag_name='test').get_string_entity(),
'--test')
def test_get_string_entity2(self):
self.assertEqual(Flag(flag_name='test',
flag_prefix='---').get_string_entity(),
'---test')
def test_get_flag_name(self):
self.assertEqual(Flag(flag_name='test').get_flag_name(),
'test')
def test_get_flag_prefix(self):
self.assertEqual(Flag(flag_name='test').get_flag_prefix(),
'--')
def test_get_flag_prefix2(self):
self.assertEqual(Flag(flag_name='test',
flag_prefix='--').get_flag_prefix(),
'--')
def test_get_flag_value_without_set(self):
self.assertEqual(Flag(flag_name='test').get_value(),
None)
def test_get_flag_value_with_set(self):
flag = Flag(flag_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'])
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'])
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+'))
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+'))
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)
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)
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)
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)
self.assertEqual(flag.validate_input_flag_value('random value'), True)
+49
View File
@@ -0,0 +1,49 @@
from argenta.command.flag import Flag, FlagsGroup
import unittest
class TestFlagsGroup(unittest.TestCase):
def test_get_flags(self):
flags = FlagsGroup()
list_of_flags = [
Flag('test1'),
Flag('test2'),
Flag('test3'),
]
flags.add_flags(list_of_flags)
self.assertEqual(flags.get_flags(),
list_of_flags)
def test_add_flag(self):
flags = FlagsGroup()
flags.add_flag(Flag('test'))
self.assertEqual(len(flags.get_flags()), 1)
def test_add_flags(self):
flags = FlagsGroup()
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)
+133
View File
@@ -0,0 +1,133 @@
from argenta.command.flag import FlagsGroup, Flag
from argenta.router import Router
from argenta.command import Command
from argenta.router.exceptions import RepeatedCommandException, TriggerCannotContainSpacesException
import unittest
class TestRouter(unittest.TestCase):
def test_get_router_name(self):
self.assertEqual(Router(name='test name').get_name(), 'test name')
def test_get_router_title(self):
self.assertEqual(Router(title='test title').get_title(), 'test title')
def test_input_correct_command(self):
router = Router()
@router.command(Command(trigger='test'))
def test():
return 'correct result'
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
def test_input_command_with_invalid_flag(self):
router = Router()
router.set_invalid_input_flag_handler(lambda x: x)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
input_command = Command(trigger='test')
input_command._set_input_flags(FlagsGroup([Flag('host')]))
self.assertEqual(router.input_command_handler(input_command), None)
def test_input_correct_command_with_one_register_and_ignore_command_register(self):
router = Router()
router.set_ignore_command_register(True)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
self.assertEqual(router.input_command_handler(Command(trigger='test')), 'correct result')
def test_input_correct_command_with_different_register_and_ignore_command_register(self):
router = Router()
router.set_ignore_command_register(True)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
self.assertEqual(router.input_command_handler(Command(trigger='TeSt')), 'correct result')
def test_input_incorrect_command_with_ignore_command_register(self):
router = Router()
router.set_ignore_command_register(True)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
self.assertEqual(router.input_command_handler(Command(trigger='Test2')), None)
def test_register_repeated_commands_with_one_register(self):
router = Router()
@router.command(Command(trigger='test'))
def test():
return 'correct result'
with self.assertRaises(RepeatedCommandException):
@router.command(Command(trigger='test'))
def test():
return 'correct result'
def test_register_commands_with_different_register(self):
router = Router()
@router.command(Command(trigger='test'))
def test():
return 'correct result'
try:
@router.command(Command(trigger='Test'))
def test():
return 'correct result'
except RepeatedCommandException:
self.fail('RepeatedCommandException should not have been thrown')
def test_register_repeated_commands_with_one_register_and_set_ignore_command_register(self):
router = Router()
router.set_ignore_command_register(True)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
with self.assertRaises(RepeatedCommandException):
@router.command(Command(trigger='test'))
def test():
return 'correct result'
def test_register_repeated_commands_with_different_register_and_set_ignore_command_register(self):
router = Router()
router.set_ignore_command_register(True)
@router.command(Command(trigger='test'))
def test():
return 'correct result'
with self.assertRaises(RepeatedCommandException):
@router.command(Command(trigger='Test'))
def test():
return 'correct result'
def test_register_command_with_spaces_in_trigger(self):
router = Router()
with self.assertRaises(TriggerCannotContainSpacesException):
@router.command(Command(trigger='command with spaces'))
def test():
return 'correct result'