From bbb471f70617e1d2a2e3d056ada240c150a907ae Mon Sep 17 00:00:00 2001 From: Moth Date: Fri, 10 Jan 2025 23:58:18 +0300 Subject: [PATCH] unit test fix [test_localization_disable.py] unit test fix #2 [test_localization_disable.py] unit test fix #3 [test_localization_disable.py] unit test fix #4 [test_localization_disable.py] unit test fix #5 [test_localization_disable.py] unit test fix #6 [test_localization_disable.py] unit test fix #7 [test_localization_disable.py] unit test fix #8 [test_localization_disable.py] unit test fix #9 [test_localization_disable.py] unit test fix #10 [test_localization_disable.py] unit test fix #11 [test_localization_disable.py] unit test fix #12 [test_localization_disable.py] unit test fix #13 [test_localization_disable.py] --- .github/workflows/unit_tests.yml | 10 ----- budget_graph/build_project.py | 6 --- budget_graph/dictionary.py | 7 +++- budget_graph/global_config.py | 4 +- budget_graph/time_checking.py | 3 +- tests/build_test_infrastructure.py | 1 - tests/test_localization.py | 63 +++++++++++++++++------------- tests/test_localization_disable.py | 22 +++++++---- 8 files changed, 57 insertions(+), 59 deletions(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index aca2c8d..433ea60 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -38,11 +38,6 @@ jobs: pip install pytest pip install pytest-asyncio - - name: Load conf.toml - run: | - cd budget_graph || exit 1 - python -c "import build_project; build_project.load_global_config(); build_project.create_directories();" - - name: Create .env file env: HASH_LOG_SALT_TEST: ${{ secrets.HASH_LOG_SALT_TEST }} @@ -220,11 +215,6 @@ jobs: ls -l # log/debug mv tests/configs/test_localization_disable.toml conf.toml # change the standard config to a test one - - name: Load conf.toml - run: | - cd budget_graph || exit 1 - python -c "import build_project; build_project.load_global_config(); build_project.create_directories();" - - name: Create .env file env: HASH_LOG_SALT_TEST: ${{ secrets.HASH_LOG_SALT_TEST }} diff --git a/budget_graph/build_project.py b/budget_graph/build_project.py index 202e607..c07d5d0 100644 --- a/budget_graph/build_project.py +++ b/budget_graph/build_project.py @@ -2,7 +2,6 @@ from sys import path as sys_path sys_path.append('../') from budget_graph.db_manager import connect_db, close_db -from budget_graph.global_config import GlobalConfig def drop_tables_in_db() -> None: @@ -56,12 +55,7 @@ def create_tables_in_db() -> None: close_db(conn) -def load_global_config() -> None: - GlobalConfig.set_config() - - if __name__ == '__main__': - load_global_config() create_directories() drop_tables_in_db() create_tables_in_db() diff --git a/budget_graph/dictionary.py b/budget_graph/dictionary.py index ac0b83f..c34301a 100644 --- a/budget_graph/dictionary.py +++ b/budget_graph/dictionary.py @@ -10,7 +10,6 @@ from budget_graph.global_config import GlobalConfig logger_dict = setup_logger('logs/DictLog.log', 'dict_loger') -localization_enable: bool = GlobalConfig.timeit_enable # create a link to the var for convenience class Emoji: @@ -52,8 +51,12 @@ def receive_translation(language: str, phrase: str) -> str: :param phrase: string that is a key in the language dictionary. :return: value in the json-dictionary in the selected language. """ + if language not in get_list_languages(): + return 'Error' + # if localization is disabled - the default language is English - dict_language_obj: dict = get_translate_from_json(language if localization_enable else language) + dict_language_obj: dict = get_translate_from_json(language if GlobalConfig.localization_enable else 'en') + return str(dict_language_obj.get(phrase, 'Error')) diff --git a/budget_graph/global_config.py b/budget_graph/global_config.py index a531fc4..6c4e51c 100644 --- a/budget_graph/global_config.py +++ b/budget_graph/global_config.py @@ -1,4 +1,4 @@ -import tomllib +from tomllib import load as tomllib_load class GlobalConfig: @@ -11,7 +11,7 @@ class GlobalConfig: @staticmethod def set_config(): with open('../conf.toml', 'rb') as toml_conf_file: - conf_data = tomllib.load(toml_conf_file) + conf_data = tomllib_load(toml_conf_file) # vars are written only once when the application is launched and are then immutable diff --git a/budget_graph/time_checking.py b/budget_graph/time_checking.py index a031914..8e56c88 100644 --- a/budget_graph/time_checking.py +++ b/budget_graph/time_checking.py @@ -6,14 +6,13 @@ from budget_graph.global_config import GlobalConfig logger_time = setup_logger("logs/TimeLog.log", "time_logger") -timeit_enable: bool = GlobalConfig.timeit_enable # create a link to the var for convenience def timeit(func): @wraps(func) def wrapper(*args, **kwargs): - if not timeit_enable: + if not GlobalConfig.timeit_enable: return func(*args, **kwargs) _start = perf_counter() diff --git a/tests/build_test_infrastructure.py b/tests/build_test_infrastructure.py index 009f9da..4c166b2 100644 --- a/tests/build_test_infrastructure.py +++ b/tests/build_test_infrastructure.py @@ -41,6 +41,5 @@ def close_test_db(conn) -> None: if __name__ == '__main__': logs_dir_delete_and_create() - load_global_config() drop_tables_in_db() create_tables_in_db() diff --git a/tests/test_localization.py b/tests/test_localization.py index 040fa6e..6953e31 100644 --- a/tests/test_localization.py +++ b/tests/test_localization.py @@ -2,6 +2,10 @@ import json from time import time from budget_graph.dictionary import receive_translation, get_list_languages +from budget_graph.global_config import GlobalConfig + + +GlobalConfig.set_config() class TestLanguages(unittest.TestCase): @@ -16,7 +20,10 @@ class TestLanguages(unittest.TestCase): # immutable all_keys_for_each_language: tuple = tuple(all_keys_for_each_language_list) - def test_languages_1(self): + def test_languages_001(self): + self.assertTrue(GlobalConfig.localization_enable) + + def test_languages_002(self): """ Checking multiple keys for presence in each language dictionary """ @@ -29,7 +36,7 @@ def test_languages_1(self): self.assertTrue(res, f'Missing keys: {[(_key, lang) for _key in _keys for lang in TestLanguages.languages if receive_translation(lang, _key) is None]}') - def test_languages_2(self): + def test_languages_003(self): """ Checking for equality of number of keys in each language """ @@ -39,7 +46,7 @@ def test_languages_2(self): for i in range(number_of_languages)) self.assertTrue(res) - def test_languages_3(self): + def test_languages_004(self): """ Checking the loading time of dictionaries """ @@ -53,7 +60,7 @@ def test_languages_3(self): res: bool = True if finish - start < 0.05 else False self.assertTrue(res, f'Actual time: {finish - start}') - def test_languages_4(self): + def test_languages_005(self): """ Checking for empty keys """ @@ -63,7 +70,7 @@ def test_languages_4(self): res = all(_key.replace(' ', '') != '' for _key in _keys) self.assertTrue(res) - def test_languages_5(self): + def test_languages_006(self): """ Checking for empty values """ @@ -72,7 +79,7 @@ def test_languages_5(self): res = all(_value.replace(' ', '') != '' for _value in _values) self.assertTrue(res) - def test_languages_6(self): + def test_languages_007(self): """ Checking for uniqueness of keys """ @@ -80,7 +87,7 @@ def test_languages_6(self): res: bool = (len(lang_keys) == len(set(lang_keys))) self.assertTrue(res) - def test_languages_7(self): + def test_languages_008(self): """ Checking for uniqueness of values """ @@ -91,95 +98,95 @@ def test_languages_7(self): self.assertTrue(res, f'Language: {lang}; len(_values) = {len(_values)}; ' f'len(set(_values)) = {len(set(_values))}') - def test_languages_8(self): + def test_languages_009(self): unknown_key: str = '123123' res: str = receive_translation('es', unknown_key) self.assertEqual(res, 'Error') - def test_languages_9(self): + def test_languages_010(self): res: str = receive_translation('es', 'invalid_value') self.assertEqual(res, 'Valor no válido') - def test_languages_10(self): + def test_languages_011(self): res: str = receive_translation('en', 'none_token') self.assertEqual(res, "(if you don't have one, enter \'None\')") - def test_languages_11(self): + def test_languages_012(self): res: str = receive_translation('fr', 'current_owner_exception') self.assertEqual(res, "C'est l'actuel propriétaire du groupe.") - def test_languages_12(self): + def test_languages_013(self): res: str = receive_translation('ru', 'no_description') self.assertEqual(res, 'без описания') - def test_languages_13(self): + def test_languages_014(self): unknown_key: str = 'unknown_key_unknown_key' res: str = receive_translation('en', unknown_key) self.assertEqual(res, 'Error') - def test_languages_14(self): + def test_languages_015(self): res: str = receive_translation('de', 'change_owner') self.assertEqual(res, 'Besitzer wechseln') - def test_languages_15(self): + def test_languages_016(self): res: str = receive_translation('is', 'check_correct_username') self.assertEqual(res, 'Athugaðu rétta stafsetningu notandanafns.') - def test_languages_16(self): + def test_languages_017(self): res: str = receive_translation('en', 'start_after_change_language') self.assertEqual(res, 'To change the language correctly, ' 'please restart the bot by clicking on the /start button.') - def test_languages_17(self): + def test_languages_018(self): res: str = receive_translation('es', 'data_is_safe') self.assertEqual(res, '¡Tus datos no se verán perjudicados!') - def test_languages_18(self): + def test_languages_019(self): unknown_lang: str = 'gb' res: str = receive_translation(unknown_lang, 'data_is_safe') self.assertEqual(res, 'Error') - def test_languages_19(self): + def test_languages_020(self): res: str = receive_translation('kk', 'add_description') self.assertEqual(res, 'Сипаттама қосу (50 таңбадан артық емес)') - def test_languages_20(self): + def test_languages_021(self): res: str = receive_translation('kk', 'add_income') self.assertEqual(res, 'Табыс қосыңыз') - def test_languages_21(self): + def test_languages_022(self): res: str = receive_translation('kk', 'group_is_full') self.assertEqual(res, 'Бұл белгі бар топ жоқ немесе ол толы. Қосымша ақпарат алу үшін топ мүшелеріне ' 'хабарласыңыз немесе өз тобыңызды жасаңыз!') - def test_languages_22(self): + def test_languages_023(self): unknown_phrase: str = 'qwerty' res: str = receive_translation('kk', unknown_phrase) self.assertEqual(res, 'Error') - def test_languages_23(self): + def test_languages_024(self): res: str = receive_translation('pt', 'view_table') self.assertEqual(res, 'Ver tabela') - def test_languages_24(self): + def test_languages_025(self): res: str = receive_translation('pt', 'premium') self.assertEqual(res, 'Prêmio') - def test_languages_25(self): + def test_languages_026(self): res: str = receive_translation('pt', 'USERNAME') self.assertEqual(res, 'NOME DE UTILIZADOR') - def test_languages_26(self): + def test_languages_027(self): unknown_phrase: str = 'earth_moon' res: str = receive_translation('pt', unknown_phrase) self.assertEqual(res, 'Error') - def test_get_list_languages_1(self): + def test_get_list_languages_001(self): res: tuple = get_list_languages() # don`t take into account the order of languages in the tuple self.assertEqual(list(res).sort(), ['ru', 'es', 'de', 'en', 'is', 'fr', 'pt', 'kk'].sort()) - def test_get_list_languages_2(self): + def test_get_list_languages_002(self): languages: tuple = get_list_languages() self.assertTrue(all(len(lang) == 2 for lang in languages)) diff --git a/tests/test_localization_disable.py b/tests/test_localization_disable.py index 5164c23..b6f86b2 100644 --- a/tests/test_localization_disable.py +++ b/tests/test_localization_disable.py @@ -4,44 +4,50 @@ import unittest from budget_graph.dictionary import receive_translation +from budget_graph.global_config import GlobalConfig + +GlobalConfig.set_config() class TestConfigLanguages(unittest.TestCase): """ Testing application localization modules when the parameter in the .toml file localization_enable = false """ - def test_languages_01(self): + def test_languages_001(self): + self.assertFalse(GlobalConfig.localization_enable) + + def test_languages_002(self): unknown_key: str = 'unknown_key_unknown_key' res: str = receive_translation('en', unknown_key) self.assertEqual(res, 'Error') - def test_languages_02(self): + def test_languages_003(self): unknown_lang: str = 'vw' res: str = receive_translation(unknown_lang, 'data_is_safe') self.assertEqual(res, 'Error') - def test_languages_03(self): + def test_languages_004(self): res: str = receive_translation('is', 'check_correct_username') self.assertEqual(res, 'Check the correct spelling of the username.') - def test_languages_04(self): + def test_languages_005(self): res: str = receive_translation('en', 'start_after_change_language') self.assertEqual(res, 'To change the language correctly, ' 'please restart the bot by clicking on the /start button.') - def test_languages_05(self): + def test_languages_006(self): res: str = receive_translation('es', 'data_is_safe') self.assertEqual(res, 'Your data will not be harmed!') - def test_languages_06(self): + def test_languages_007(self): res: str = receive_translation('de', 'change_owner') self.assertNotEqual(res, 'Besitzer wechseln') - def test_languages_07(self): + def test_languages_008(self): res: str = receive_translation('kk', 'add_income') self.assertNotEqual(res, 'Error') - def test_languages_08(self): + def test_languages_009(self): res: str = receive_translation('pt', 'view_table') self.assertNotEqual(res, 'Ver tabela')