Skip to content

Commit

Permalink
increased coverage of autotests and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MothScientist committed Jan 1, 2025
1 parent 1834132 commit f35d7a8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
4 changes: 2 additions & 2 deletions budget_graph/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ def process_delete_account(db_connection, message, user_language: str):
if user_choice == f"👍 {receive_translation(user_language, "YES")}":
# removing a user from the cache
UserRegistrationStatusCache.delete_data_from_cache(telegram_id)
db_connection.delete_username_from_group_by_telegram_id(telegram_id)
db_connection.delete_user_from_group_by_telegram_id(telegram_id)
bot.send_message(message.chat.id, receive_translation(user_language, 'parting'))
bot.send_message(message.chat.id, receive_translation(user_language, 'account_is_deleted'),
reply_markup=markup_1)
Expand Down Expand Up @@ -757,7 +757,7 @@ def process_delete_user(db_connection, message, group_id: int, group_users_list:
else:
# removing a user from the cache
UserRegistrationStatusCache.delete_data_from_cache(telegram_id_user_to_delete)
if db_connection.delete_username_from_group_by_telegram_id(telegram_id_user_to_delete):
if db_connection.delete_user_from_group_by_telegram_id(telegram_id_user_to_delete):
bot.send_message(message.chat.id, receive_translation(user_language, 'user_removed'))
logger_bot.info(f"'User {logging_hash(username_user_to_delete)}' "
f"deleted by group owner from group #{group_id}")
Expand Down
3 changes: 1 addition & 2 deletions budget_graph/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,6 @@ def change_feature_status_del_msg_after_transaction(self, telegram_id: int) -> b
f"telegram_id: {telegram_id}")
return False

@timeit
def get_user_language(self, telegram_id: int) -> str:
"""
Gets the user's (telegram_id) language from the database.
Expand Down Expand Up @@ -574,7 +573,6 @@ def add_user_timezone(self, telegram_id: int, timezone: int) -> bool:
return False

# pylint: disable=too-many-arguments, too-many-positional-arguments
@timeit
def add_transaction_to_db(self,
transaction_amount: int,
record_date: str,
Expand Down Expand Up @@ -748,6 +746,7 @@ def update_group_owner(self, new_owner_telegram_id: int, group_id: int) -> bool:
return False

def delete_username_from_group_by_telegram_id(self, telegram_id: int) -> bool:
def delete_user_from_group_by_telegram_id(self, telegram_id: int) -> bool:
try:
with self.__conn as conn:
with conn.cursor() as cur:
Expand Down
10 changes: 5 additions & 5 deletions budget_graph/webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ def login():
logger_app.warning(f"Failed authorization attempt: username: {logging_hash(username)}, "
f"user salt is exist: {len(psw_salt) != 0}")

return render_template("login.html", title="Budget Graph - Login")
return render_template('login.html', title='Budget Graph - Login')


# TODO - too-many-branches
# pylint: disable=too-many-branches
@app.route('/household/<username>', methods=["GET", "POST"]) # user's personal account
@app.route('/household/<username>', methods=['GET', 'POST']) # user's personal account
def household(username):
"""
user's personal account with his group table
"""
if "userLogged" not in session or session["userLogged"] != username:
if "userLogged" not in session or session['userLogged'] != username:
abort(401)

dbase = DatabaseQueries(connect_db_flask_g())
Expand All @@ -150,11 +150,11 @@ def household(username):
record_date_is_valid: bool = asyncio.run(date_validation(record_date))
description_is_valid: bool = description_validation(description)

if "submit-button-2" in request.form: # if this is an expense category
if 'submit-button-2' in request.form: # if this is an expense category
value *= -1

if not value:
flash("The value format is invalid", category="error")
flash('The value format is invalid', category='error')

elif not record_date_is_valid:
flash("Date format is invalid", category="error")
Expand Down
3 changes: 2 additions & 1 deletion tests/test_database_queries_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from budget_graph.registration_service import user_registration
from budget_graph.encryption import getting_hash, get_salt, get_token

from tests.build_test_infrastructure import connect_test_db, close_test_db
from tests.build_test_infrastructure import connect_test_db, close_test_db, prepare_db_tables_for_tests

LANGUAGES: tuple = get_list_languages()
LANG_LEN: int = len(LANGUAGES)
Expand Down Expand Up @@ -1330,4 +1330,5 @@ def test_050_user_registration_8(self):


if __name__ == '__main__':
prepare_db_tables_for_tests() # clear the database (required for local running outside the test environment)
unittest.main()
93 changes: 91 additions & 2 deletions tests/test_database_queries_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

from budget_graph.db_manager import DatabaseQueries
from budget_graph.dictionary import get_list_languages
# from budget_graph.registration_service import user_registration
from budget_graph.registration_service import user_registration
from budget_graph.encryption import getting_hash, get_salt, get_token

from tests.build_test_infrastructure import connect_test_db, close_test_db
from tests.build_test_infrastructure import connect_test_db, close_test_db, prepare_db_tables_for_tests

LANGUAGES: tuple = get_list_languages()
LANG_LEN: int = len(LANGUAGES)
Expand Down Expand Up @@ -355,3 +355,92 @@ def test_004_update_group_uuid_after_transaction_4(self):

self.assertEqual(res_1, res_2, f'res_1 = {res_1}\n'
f'res_2 = {res_2}')

def test_005_get_group_users_data_1(self):
group_id: int = 1

res = self.test_db.get_group_users_data(group_id)

self.assertEqual(len(res), 3, f'len(res) = {len(res)}')
self.assertTrue(isinstance(res[0], list), f'type(res[0]) = {type(res[0])}')
self.assertTrue(isinstance(res[0][0], str), f'type(res[0]) = {type(res[0][0])}')
self.assertTrue(isinstance(res[0][1], datetime), f'type(res[0]) = {type(res[0][1])}')

def test_005_get_group_users_data_2(self):
group_id: int = 2

res = self.test_db.get_group_users_data(group_id)

self.assertEqual(len(res), 1, f'len(res) = {len(res)}')
self.assertTrue(isinstance(res[0], list), f'type(res[0]) = {type(res[0])}')
self.assertTrue(isinstance(res[0][0], str), f'type(res[0]) = {type(res[0][0])}')
self.assertTrue(isinstance(res[0][1], datetime), f'type(res[0]) = {type(res[0][1])}')

def test_005_get_group_users_data_3(self):
group_id: int = 3

res = self.test_db.get_group_users_data(group_id)

self.assertEqual(len(res), 0, f'len(res) = {len(res)}')
self.assertTrue(isinstance(res, list), f'type(res[0]) = {type(res)}')

def test_005_get_group_users_data_4(self):
group_id: int = 0

res = self.test_db.get_group_users_data(group_id)

self.assertEqual(len(res), 0, f'len(res) = {len(res)}')
self.assertTrue(isinstance(res, list), f'type(res[0]) = {type(res)}')

def test_005_get_group_users_data_5(self):
group_id: int = -1

res = self.test_db.get_group_users_data(group_id)

self.assertEqual(len(res), 0, f'len(res) = {len(res)}')
self.assertTrue(isinstance(res, list), f'type(res[0]) = {type(res)}')

def test_006_delete_user_from_group_by_telegram_id_1(self):
""" First we create a new one - then we delete it """
# let's use the universal registration service
telegram_id: int = randint(10000, 1000000) * randint(5, 7)
res: tuple[bool, str] = user_registration(
self.test_db,
TestDbQueries1.group_1_token,
telegram_id,
get_token()[:8],
get_salt(),
getting_hash(get_salt(), get_salt())

)

# check the registration status
self.assertTrue(res[0])
self.assertEqual(res[1], '', f'res = {res}')

# we check that such a user is in the group
group_id: int = self.test_db.get_group_id_by_telegram_id(telegram_id)
self.assertEqual(group_id, 1, f'group_id = {group_id}')

exists_res: bool = self.test_db.check_telegram_id_is_exist(telegram_id)
self.assertTrue(exists_res)

# delete user
del_res: bool = self.test_db.delete_user_from_group_by_telegram_id(telegram_id)
self.assertTrue(del_res)

# check the user group again
group_id: int = self.test_db.get_group_id_by_telegram_id(telegram_id)
self.assertEqual(group_id, 0, f'group_id = {group_id}')

exists_res: bool = self.test_db.check_telegram_id_is_exist(telegram_id)
self.assertFalse(exists_res)

def test_006_delete_user_from_group_by_telegram_id_2(self):
""" Removing a non-existent user """
pass


if __name__ == '__main__':
prepare_db_tables_for_tests() # clear the database (required for local running outside the test environment)
unittest.main()

0 comments on commit f35d7a8

Please sign in to comment.