Skip to content

Commit

Permalink
unit test fix [test_localization_disable.py]
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
MothScientist committed Jan 11, 2025
1 parent 7af084d commit bbb471f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 59 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
6 changes: 0 additions & 6 deletions budget_graph/build_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
7 changes: 5 additions & 2 deletions budget_graph/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'))


Expand Down
4 changes: 2 additions & 2 deletions budget_graph/global_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import tomllib
from tomllib import load as tomllib_load


class GlobalConfig:
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions budget_graph/time_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion tests/build_test_infrastructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
63 changes: 35 additions & 28 deletions tests/test_localization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -72,15 +79,15 @@ 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
"""
for lang_keys in TestLanguages.all_keys_for_each_language:
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
"""
Expand All @@ -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))

Expand Down
22 changes: 14 additions & 8 deletions tests/test_localization_disable.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit bbb471f

Please sign in to comment.