sh_cpl/unittests/unittests_cli/run_test_case.py

118 lines
4.7 KiB
Python
Raw Normal View History

2022-06-26 00:15:05 +02:00
import json
import os
import shutil
2022-05-26 13:49:31 +02:00
import unittest
2022-06-26 00:15:05 +02:00
from cpl_core.utils import String
2022-12-01 23:23:48 +01:00
from unittests_cli.abc.command_test_case import CommandTestCase
2022-06-26 00:15:05 +02:00
from unittests_cli.constants import PLAYGROUND_PATH
from unittests_shared.cli_commands import CLICommands
2022-05-26 13:49:31 +02:00
2022-12-01 23:23:48 +01:00
class RunTestCase(CommandTestCase):
def __init__(self, method_name: str):
CommandTestCase.__init__(self, method_name)
2023-02-20 15:55:20 +01:00
self._source = "run-test"
self._project_file = f"src/{String.convert_to_snake_case(self._source)}/{self._source}.json"
self._application = f"src/{String.convert_to_snake_case(self._source)}/application.py"
2022-06-26 00:15:05 +02:00
self._test_code = f"""
import json
import os
2022-06-26 00:15:05 +02:00
settings = dict()
with open('appsettings.json', 'r', encoding='utf-8') as cfg:
# load json
settings = json.load(cfg)
cfg.close()
settings['RunTest']['WasStarted'] = 'True'
settings['RunTest']['Path'] = os.path.dirname(os.path.realpath(__file__))
2022-06-26 00:15:05 +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, is_dev=False):
2023-02-20 15:55:20 +01:00
appsettings = f"dist/{self._source}/build/{String.convert_to_snake_case(self._source)}/appsettings.json"
if is_dev:
2023-02-20 15:55:20 +01:00
appsettings = f"src/{String.convert_to_snake_case(self._source)}/appsettings.json"
2023-02-20 15:55:20 +01:00
with open(os.path.join(os.getcwd(), appsettings), "r", encoding="utf-8") as cfg:
2022-06-26 00:15:05 +02:00
# load json
project_json = json.load(cfg)
cfg.close()
return project_json
def _save_appsettings(self, settings: dict):
2023-02-20 15:55:20 +01:00
with open(
os.path.join(os.getcwd(), f"src/{String.convert_to_snake_case(self._source)}/appsettings.json"),
"w",
encoding="utf-8",
) as project_file:
2022-06-26 00:15:05 +02:00
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
os.chdir(PLAYGROUND_PATH)
2022-06-26 00:15:05 +02:00
# create projects
2023-02-20 15:55:20 +01:00
CLICommands.new("console", self._source, "--ab", "--s")
2022-06-26 00:15:05 +02:00
os.chdir(os.path.join(os.getcwd(), self._source))
2023-02-20 15:55:20 +01:00
settings = {"RunTest": {"WasStarted": "False"}}
2022-06-26 00:15:05 +02:00
self._save_appsettings(settings)
2023-02-20 15:55:20 +01:00
with open(os.path.join(os.getcwd(), self._application), "a", encoding="utf-8") as file:
file.write(f"\t\t{self._test_code}")
2022-06-26 00:15:05 +02:00
file.close()
def test_run(self):
CLICommands.run()
2022-06-26 00:15:05 +02:00
settings = self._get_appsettings()
self.assertNotEqual(settings, {})
2023-02-20 15:55:20 +01:00
self.assertIn("RunTest", settings)
self.assertIn("WasStarted", settings["RunTest"])
self.assertEqual("True", settings["RunTest"]["WasStarted"])
self.assertNotEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"src/{String.convert_to_snake_case(self._source)}"), settings["RunTest"]["Path"]
)
self.assertEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"dist/{self._source}/build/{String.convert_to_snake_case(self._source)}"),
settings["RunTest"]["Path"],
)
2022-05-26 13:49:31 +02:00
2022-06-26 00:15:05 +02:00
def test_run_by_project(self):
CLICommands.run(self._source)
2022-06-26 00:15:05 +02:00
settings = self._get_appsettings()
self.assertNotEqual(settings, {})
2023-02-20 15:55:20 +01:00
self.assertIn("RunTest", settings)
self.assertIn("WasStarted", settings["RunTest"])
self.assertEqual("True", settings["RunTest"]["WasStarted"])
self.assertNotEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"src/{String.convert_to_snake_case(self._source)}"), settings["RunTest"]["Path"]
)
self.assertEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"dist/{self._source}/build/{String.convert_to_snake_case(self._source)}"),
settings["RunTest"]["Path"],
)
def test_run_dev(self):
CLICommands.run(is_dev=True)
settings = self._get_appsettings(is_dev=True)
self.assertNotEqual(settings, {})
2023-02-20 15:55:20 +01:00
self.assertIn("RunTest", settings)
self.assertIn("WasStarted", settings["RunTest"])
self.assertEqual("True", settings["RunTest"]["WasStarted"])
self.assertEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"src/{String.convert_to_snake_case(self._source)}"), settings["RunTest"]["Path"]
)
def test_run_dev_by_project(self):
CLICommands.run(self._source, is_dev=True)
settings = self._get_appsettings(is_dev=True)
self.assertNotEqual(settings, {})
2023-02-20 15:55:20 +01:00
self.assertIn("RunTest", settings)
self.assertIn("WasStarted", settings["RunTest"])
self.assertEqual("True", settings["RunTest"]["WasStarted"])
self.assertEqual(
2023-02-20 15:55:20 +01:00
os.path.join(os.getcwd(), f"src/{String.convert_to_snake_case(self._source)}"), settings["RunTest"]["Path"]
)