2022-06-26 00:39:16 +02:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import shutil
|
2022-06-26 01:05:45 +02:00
|
|
|
import time
|
2022-05-26 13:49:31 +02:00
|
|
|
import unittest
|
|
|
|
|
2022-06-26 00:39:16 +02:00
|
|
|
from cpl_core.utils import String
|
|
|
|
from unittests_cli.constants import PLAYGROUND_PATH
|
|
|
|
from unittests_cli.threads.start_test_thread import StartTestThread
|
|
|
|
from unittests_shared.cli_commands import CLICommands
|
|
|
|
|
2022-05-26 13:49:31 +02:00
|
|
|
|
|
|
|
class StartTestCase(unittest.TestCase):
|
|
|
|
|
2022-06-26 00:39:16 +02:00
|
|
|
def __init__(self, methodName: str):
|
|
|
|
unittest.TestCase.__init__(self, methodName)
|
|
|
|
self._source = 'start-test'
|
|
|
|
self._project_file = f'src/{String.convert_to_snake_case(self._source)}/{self._source}.json'
|
|
|
|
self._appsettings = f'src/{String.convert_to_snake_case(self._source)}/appsettings.json'
|
|
|
|
self._application = f'src/{String.convert_to_snake_case(self._source)}/application.py'
|
|
|
|
self._test_code = f"""
|
|
|
|
import json
|
|
|
|
settings = dict()
|
|
|
|
with open('appsettings.json', 'r', encoding='utf-8') as cfg:
|
|
|
|
# load json
|
|
|
|
settings = json.load(cfg)
|
|
|
|
cfg.close()
|
|
|
|
|
2022-06-26 00:41:25 +02:00
|
|
|
if settings['RunTest']['WasStarted'] == 'True':
|
2022-06-26 00:39:16 +02:00
|
|
|
settings['RunTest']['WasRestarted'] = 'True'
|
|
|
|
|
|
|
|
settings['RunTest']['WasStarted'] = 'True'
|
2022-06-26 01:05:45 +02:00
|
|
|
|
2022-06-26 00:39:16 +02:00
|
|
|
with open('appsettings.json', 'w', encoding='utf-8') as project_file:
|
|
|
|
project_file.write(json.dumps(settings, indent=2))
|
|
|
|
project_file.close()
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _get_appsettings(self):
|
|
|
|
with open(os.path.join(os.getcwd(), self._appsettings), 'r', encoding='utf-8') as cfg:
|
|
|
|
# load json
|
|
|
|
project_json = json.load(cfg)
|
|
|
|
cfg.close()
|
|
|
|
|
|
|
|
return project_json
|
|
|
|
|
|
|
|
def _save_appsettings(self, settings: dict):
|
|
|
|
with open(os.path.join(os.getcwd(), self._appsettings), 'w', encoding='utf-8') as project_file:
|
|
|
|
project_file.write(json.dumps(settings, indent=2))
|
|
|
|
project_file.close()
|
|
|
|
|
2022-05-26 13:49:31 +02:00
|
|
|
def setUp(self):
|
2022-12-01 17:00:17 +01:00
|
|
|
if not os.path.exists(PLAYGROUND_PATH):
|
|
|
|
os.makedirs(PLAYGROUND_PATH)
|
|
|
|
|
|
|
|
os.chdir(PLAYGROUND_PATH)
|
2022-06-26 00:39:16 +02:00
|
|
|
# create projects
|
|
|
|
CLICommands.new('console', self._source, '--ab', '--s')
|
|
|
|
os.chdir(os.path.join(os.getcwd(), self._source))
|
|
|
|
settings = {'RunTest': {'WasStarted': 'False', 'WasRestarted': 'False'}}
|
|
|
|
self._save_appsettings(settings)
|
|
|
|
with open(os.path.join(os.getcwd(), self._application), 'a', encoding='utf-8') as file:
|
|
|
|
file.write(f'\t\t{self._test_code}')
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
def cleanUp(self):
|
|
|
|
# remove projects
|
|
|
|
if not os.path.exists(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source))):
|
|
|
|
return
|
|
|
|
|
|
|
|
shutil.rmtree(os.path.abspath(os.path.join(PLAYGROUND_PATH, self._source)))
|
|
|
|
|
|
|
|
def test_start(self):
|
|
|
|
thread = StartTestThread()
|
2022-06-26 01:05:45 +02:00
|
|
|
thread.start()
|
|
|
|
time.sleep(1)
|
2022-06-26 00:39:16 +02:00
|
|
|
settings = self._get_appsettings()
|
|
|
|
self.assertNotEqual(settings, {})
|
|
|
|
self.assertIn('RunTest', settings)
|
|
|
|
self.assertIn('WasStarted', settings['RunTest'])
|
|
|
|
self.assertEqual(
|
|
|
|
'True',
|
|
|
|
settings['RunTest']['WasStarted']
|
|
|
|
)
|
|
|
|
|
|
|
|
with open(os.path.join(os.getcwd(), self._application), 'a', encoding='utf-8') as file:
|
|
|
|
file.write(f'# trigger restart (comment generated by unittest)')
|
|
|
|
file.close()
|
2022-05-26 13:49:31 +02:00
|
|
|
|
2022-06-26 01:05:45 +02:00
|
|
|
time.sleep(1)
|
|
|
|
|
2022-06-26 00:39:16 +02:00
|
|
|
settings = self._get_appsettings()
|
|
|
|
self.assertNotEqual(settings, {})
|
|
|
|
self.assertIn('RunTest', settings)
|
|
|
|
self.assertIn('WasStarted', settings['RunTest'])
|
|
|
|
self.assertIn('WasRestarted', settings['RunTest'])
|
|
|
|
self.assertEqual(
|
|
|
|
'True',
|
|
|
|
settings['RunTest']['WasStarted']
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
'True',
|
|
|
|
settings['RunTest']['WasRestarted']
|
|
|
|
)
|