Added unittests #143

This commit is contained in:
Sven Heidemann 2022-12-08 14:23:46 +01:00
parent 59920c59e0
commit e46711dc54
2 changed files with 43 additions and 7 deletions

View File

@ -9,6 +9,7 @@ from unittests_shared.cli_commands import CLICommands
class GenerateTestCase(CommandTestCase): class GenerateTestCase(CommandTestCase):
_project = 'test-console' _project = 'test-console'
_t_path = 'test' _t_path = 'test'
_skip_tear_down = True
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
@ -19,29 +20,32 @@ class GenerateTestCase(CommandTestCase):
os.chdir(PLAYGROUND_PATH) os.chdir(PLAYGROUND_PATH)
def _test_file(self, schematic: str, suffix: str, path=None): def _test_file(self, schematic: str, suffix: str, path=None):
file = 'GeneratedFile' file = f'GeneratedFile{"OnReady" if schematic == "event" else ""}'
expected_path = f'generated_file{suffix}.py' expected_path = f'generated_file{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if path is not None: if path is not None:
file = f'{path}/{file}' file = f'{path}/{file}'
expected_path = f'{path}/{expected_path}' expected_path = f'{path}/{expected_path}'
CLICommands.generate(schematic, file) CLICommands.generate(schematic, file)
file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, expected_path)) file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, expected_path))
file_exists = os.path.exists(file_path) file_exists = os.path.exists(file_path)
self.assertTrue(file_exists) self.assertTrue(file_exists)
def _test_file_with_project(self, schematic: str, suffix: str, path=None, enter=True): def _test_file_with_project(self, schematic: str, suffix: str, path=None, enter=True):
file = f'GeneratedFile' file = f'GeneratedFile{"OnReady" if schematic == "event" else ""}'
excepted_path = f'generated_file{suffix}.py' excepted_path = f'generated_file{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if path is not None: if path is not None:
excepted_path = f'{self._project}/src/{String.convert_to_snake_case(self._project)}/{path}/generated_file_in_project{suffix}.py' excepted_path = f'{self._project}/src/{String.convert_to_snake_case(self._project)}/{path}/generated_file_in_project{"_on_ready" if schematic == "event" else ""}{suffix}.py'
if enter: if enter:
os.chdir(path) os.chdir(path)
excepted_path = f'{path}/src/{String.convert_to_snake_case(self._project)}/generated_file_in_project{suffix}.py' excepted_path = f'{path}/src/{String.convert_to_snake_case(self._project)}/generated_file_in_project{"_on_ready" if schematic == "event" else ""}{suffix}.py'
file = f'{path}/GeneratedFileInProject' file = f'{path}/GeneratedFileInProject{"OnReady" if schematic == "event" else ""}'
CLICommands.generate(schematic, file) CLICommands.generate(schematic, file)
file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, excepted_path)) file_path = os.path.abspath(os.path.join(PLAYGROUND_PATH, excepted_path))
print(file_path)
self.assertTrue(os.path.exists(file_path)) self.assertTrue(os.path.exists(file_path))
def test_abc(self): def test_abc(self):
@ -88,3 +92,11 @@ class GenerateTestCase(CommandTestCase):
def test_validator(self): def test_validator(self):
self._test_file('validator', '_validator') self._test_file('validator', '_validator')
self._test_file_with_project('validator', '_validator', path=self._project) self._test_file_with_project('validator', '_validator', path=self._project)
def test_discord_command(self):
self._test_file('command', '_command')
self._test_file_with_project('command', '_command', path=self._project)
def test_discord_event(self):
self._test_file('event', '_event')
self._test_file_with_project('event', '_event', path=self._project)

View File

@ -52,6 +52,15 @@ class NewTestCase(CommandTestCase):
else: else:
self.assertFalse(os.path.isfile(os.path.join(project_path, f'test_case.py'))) self.assertFalse(os.path.isfile(os.path.join(project_path, f'test_case.py')))
if project_type == 'discord-bot':
self.assertTrue(os.path.isfile(os.path.join(project_path, f'events/__init__.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'events/on_ready_event.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'commands/__init__.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'commands/ping_command.py')))
else:
self.assertFalse(os.path.isfile(os.path.join(project_path, f'events/on_ready_event.py')))
self.assertFalse(os.path.isfile(os.path.join(project_path, f'commands/ping_command.py')))
def _test_sub_project(self, project_type: str, name: str, workspace_name: str, *args, test_venv=False): def _test_sub_project(self, project_type: str, name: str, workspace_name: str, *args, test_venv=False):
os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name))) os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name)))
CLICommands.new(project_type, name, *args) CLICommands.new(project_type, name, *args)
@ -72,6 +81,15 @@ class NewTestCase(CommandTestCase):
self.assertTrue(os.path.exists(project_path)) self.assertTrue(os.path.exists(project_path))
self.assertTrue(os.path.join(project_path, f'{name}.json')) self.assertTrue(os.path.join(project_path, f'{name}.json'))
if project_type == 'discord-bot':
self.assertTrue(os.path.isfile(os.path.join(project_path, f'events/__init__.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'events/on_ready_event.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'commands/__init__.py')))
self.assertTrue(os.path.isfile(os.path.join(project_path, f'commands/ping_command.py')))
else:
self.assertFalse(os.path.isfile(os.path.join(project_path, f'events/on_ready_event.py')))
self.assertFalse(os.path.isfile(os.path.join(project_path, f'commands/ping_command.py')))
def _test_sub_directory_project(self, project_type: str, directory: str, name: str, workspace_name: str, *args): def _test_sub_directory_project(self, project_type: str, directory: str, name: str, workspace_name: str, *args):
os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name))) os.chdir(os.path.abspath(os.path.join(os.getcwd(), workspace_name)))
CLICommands.new(project_type, f'{directory}/{name}', *args) CLICommands.new(project_type, f'{directory}/{name}', *args)
@ -117,6 +135,12 @@ class NewTestCase(CommandTestCase):
def test_console_sub_with_other_base(self): def test_console_sub_with_other_base(self):
self._test_sub_project('console', 'tools/test-sub-console', 'test-console', '--ab', '--s', '--sp', '--venv', '--base', test_venv=True) self._test_sub_project('console', 'tools/test-sub-console', 'test-console', '--ab', '--s', '--sp', '--venv', '--base', test_venv=True)
def test_discord_bot(self):
self._test_project('discord-bot', 'test-bot', '--ab', '--s', '--venv', test_venv=True)
def test_discord_bot_sub(self):
self._test_sub_project('discord-bot', 'test-bot-sub', 'test-console', '--ab', '--s', '--venv', test_venv=True)
def test_library(self): def test_library(self):
self._test_project('library', 'test-library', '--ab', '--s', '--sp') self._test_project('library', 'test-library', '--ab', '--s', '--sp')