Added ui tests for discord bot #139
This commit is contained in:
1
kdb-bot/test/ui_tests/__init__.py
Normal file
1
kdb-bot/test/ui_tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# imports:
|
16
kdb-bot/test/ui_tests/config/appsettings.edrafts-pc.json
Normal file
16
kdb-bot/test/ui_tests/config/appsettings.edrafts-pc.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"DatabaseSettings": {
|
||||
"Host": "localhost",
|
||||
"User": "kd_kdb",
|
||||
"Password": "",
|
||||
"Database": "keksdose_bot_dev",
|
||||
"Charset": "utf8mb4",
|
||||
"UseUnicode": "true",
|
||||
"Buffered": "true",
|
||||
"AuthPlugin": "mysql_native_password"
|
||||
},
|
||||
"DiscordBot": {
|
||||
"Token": "",
|
||||
"Prefix": "!kab-e "
|
||||
}
|
||||
}
|
20
kdb-bot/test/ui_tests/config/appsettings.json
Normal file
20
kdb-bot/test/ui_tests/config/appsettings.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"TimeFormatSettings": {
|
||||
"DateFormat": "%Y-%m-%d",
|
||||
"TimeFormat": "%H:%M:%S",
|
||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||
},
|
||||
"LoggingSettings": {
|
||||
"Path": "logs/$date_now/",
|
||||
"Filename": "bot.log",
|
||||
"ConsoleLogLevel": "ERROR",
|
||||
"FileLogLevel": "WARN"
|
||||
},
|
||||
"Translation": {
|
||||
"DefaultLanguage": "de",
|
||||
"Languages": [
|
||||
"de"
|
||||
]
|
||||
}
|
||||
}
|
34
kdb-bot/test/ui_tests/main.py
Normal file
34
kdb-bot/test/ui_tests/main.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import asyncio
|
||||
|
||||
from cpl_core.application import ApplicationABC
|
||||
from cpl_core.application import ApplicationBuilder
|
||||
|
||||
from bot.startup_test_extension import StartupTestExtension
|
||||
from ui_tests.startup import Startup
|
||||
from ui_tests_shared.declarations import Declarations
|
||||
|
||||
try:
|
||||
from test_application import TestApplication
|
||||
except ImportError:
|
||||
from .test_application import TestApplication
|
||||
|
||||
|
||||
def get_app() -> ApplicationABC:
|
||||
app_builder = ApplicationBuilder(TestApplication) \
|
||||
.use_extension(StartupTestExtension) \
|
||||
.use_startup(Startup)
|
||||
app = app_builder.build()
|
||||
Declarations.app = app
|
||||
return app
|
||||
|
||||
|
||||
async def main():
|
||||
app = get_app()
|
||||
await app.run_async()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import nest_asyncio
|
||||
|
||||
nest_asyncio.apply()
|
||||
asyncio.run(main())
|
56
kdb-bot/test/ui_tests/startup.py
Normal file
56
kdb-bot/test/ui_tests/startup.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.application import StartupABC
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.database import DatabaseSettings
|
||||
from cpl_core.database.context import DatabaseContext
|
||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||
from cpl_core.environment import ApplicationEnvironment
|
||||
from cpl_discord import get_discord_collection
|
||||
from cpl_discord.discord_event_types_enum import DiscordEventTypesEnum
|
||||
|
||||
from ui_tests.test_on_ready_event import TestOnReadyEvent
|
||||
|
||||
|
||||
class Startup(StartupABC):
|
||||
|
||||
def __init__(self):
|
||||
StartupABC.__init__(self)
|
||||
|
||||
self._config: Optional[ConfigurationABC] = None
|
||||
|
||||
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
|
||||
configuration.add_environment_variables('KDB-TEST_')
|
||||
configuration.add_environment_variables('DISCORD_')
|
||||
|
||||
cwd = os.path.dirname(os.path.realpath(__file__))
|
||||
configuration.add_json_file(f'{cwd}/config/appsettings.json', optional=False)
|
||||
configuration.add_json_file(f'{cwd}/config/appsettings.{environment.host_name}.json', optional=True)
|
||||
|
||||
self._config = configuration
|
||||
return configuration
|
||||
|
||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||
services.add_logging()
|
||||
services.add_translation()
|
||||
db_settings: DatabaseSettings = self._config.get_configuration(DatabaseSettings)
|
||||
db_settings_with_pw = DatabaseSettings()
|
||||
pw = self._config.get_configuration('DB_PASSWORD')
|
||||
db_settings_with_pw.from_dict({
|
||||
"Host": db_settings.host,
|
||||
"User": db_settings.user,
|
||||
"Password": '' if pw is None else pw,
|
||||
"Database": db_settings.database,
|
||||
"Charset": db_settings.charset,
|
||||
"UseUnicode": db_settings.use_unicode,
|
||||
"Buffered": db_settings.buffered,
|
||||
"AuthPlugin": db_settings.auth_plugin
|
||||
})
|
||||
services.add_db_context(DatabaseContext, db_settings_with_pw)
|
||||
|
||||
services.add_discord()
|
||||
dc = get_discord_collection(services)
|
||||
dc.add_event(DiscordEventTypesEnum.on_ready.value, TestOnReadyEvent)
|
||||
|
||||
return services.build_service_provider()
|
34
kdb-bot/test/ui_tests/test_application.py
Normal file
34
kdb-bot/test/ui_tests/test_application.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from cpl_core.configuration import ConfigurationABC
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_discord.application import DiscordBotApplicationABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_translation import TranslationSettings, TranslationServiceABC
|
||||
|
||||
|
||||
class TestApplication(DiscordBotApplicationABC):
|
||||
|
||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||
DiscordBotApplicationABC.__init__(self, config, services)
|
||||
|
||||
self._config = config
|
||||
self._services = services
|
||||
|
||||
self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC)
|
||||
self._translation: TranslationServiceABC = services.get_service(TranslationServiceABC)
|
||||
|
||||
@property
|
||||
def config(self) -> ConfigurationABC:
|
||||
return self._config
|
||||
|
||||
@property
|
||||
def services(self) -> ServiceProviderABC:
|
||||
return self._services
|
||||
|
||||
async def configure(self):
|
||||
self._translation.load_by_settings(self._configuration.get_configuration(TranslationSettings))
|
||||
|
||||
async def main(self):
|
||||
await self._bot.start_async()
|
||||
|
||||
async def stop_async(self):
|
||||
await self._bot.close()
|
43
kdb-bot/test/ui_tests/test_on_ready_event.py
Normal file
43
kdb-bot/test/ui_tests/test_on_ready_event.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from cpl_core.console import Console, ForegroundColorEnum
|
||||
from cpl_core.dependency_injection import ServiceProviderABC
|
||||
from cpl_core.logging import LoggerABC
|
||||
from cpl_discord.events import OnReadyABC
|
||||
from cpl_discord.service import DiscordBotServiceABC
|
||||
from cpl_translation import TranslatePipe
|
||||
|
||||
from bot_core.abc.client_utils_service_abc import ClientUtilsServiceABC
|
||||
|
||||
|
||||
class TestOnReadyEvent(OnReadyABC):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
logger: LoggerABC,
|
||||
bot: DiscordBotServiceABC,
|
||||
services: ServiceProviderABC,
|
||||
client_utils: ClientUtilsServiceABC,
|
||||
t: TranslatePipe
|
||||
):
|
||||
OnReadyABC.__init__(self)
|
||||
|
||||
self._logger = logger
|
||||
self._bot = bot
|
||||
self._services = services
|
||||
self._client_utils = client_utils
|
||||
self._t = t
|
||||
|
||||
async def on_ready(self):
|
||||
Console.write_line('\nStarting tests:\n')
|
||||
loader = unittest.TestLoader()
|
||||
path = f'{os.path.dirname(os.path.realpath(__file__))}/../'
|
||||
tests = loader.discover(path, pattern='*_test_case.py')
|
||||
runner = unittest.TextTestRunner()
|
||||
runner.run(tests)
|
||||
# for cls in CommandTestABC.__subclasses__():
|
||||
# service: CommandTestABC = self._services.get_service(cls)
|
||||
# await service.run(self._tests)
|
||||
|
||||
await self._bot.close()
|
0
kdb-bot/test/ui_tests/ui-tests.json
Normal file
0
kdb-bot/test/ui_tests/ui-tests.json
Normal file
Reference in New Issue
Block a user