Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
MothScientist committed Dec 16, 2024
1 parent 74aec48 commit efdab9e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
25 changes: 3 additions & 22 deletions budget_graph/db_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,7 @@ def get_group_usernames(self, group_id: int) -> tuple:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
u."username"
FROM
"budget_graph"."users" u
JOIN
"budget_graph"."users_groups" u_g
ON
u."telegram_id" = u_g."telegram_id"
WHERE
u_g."group_id" = %s::smallint""", (group_id,))
cur.execute(read_sql_file('get_group_usernames'), {'group_id': group_id})
return tuple(str(row[0]) for row in cur.fetchall())

except (DatabaseError, TypeError) as err:
Expand All @@ -326,12 +317,7 @@ def get_group_telegram_ids(self, group_id: int) -> tuple:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""SELECT
"telegram_id"
FROM
"budget_graph"."users_groups"
WHERE
"group_id" = %s::smallint""", (group_id,))
cur.execute(read_sql_file('get_group_telegram_ids'), {'group_id': group_id})
return tuple(row[0] for row in cur.fetchall())

except (DatabaseError, TypeError) as err:
Expand Down Expand Up @@ -577,12 +563,7 @@ def add_user_timezone(self, telegram_id: int, timezone: int) -> bool:
try:
with self.__conn as conn:
with conn.cursor() as cur:
cur.execute("""UPDATE
"budget_graph"."users"
SET
"timezone" = %s::smallint
WHERE
"telegram_id" = %s::bigint""", (timezone, telegram_id))
cur.execute(read_sql_file('add_user_timezone'), {'timezone': timezone, 'telegram_id': telegram_id})
conn.commit()
return True

Expand Down
6 changes: 6 additions & 0 deletions budget_graph/sql/add_user_timezone.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE
"budget_graph"."users"
SET
"timezone" = %(timezone)s::smallint
WHERE
"telegram_id" = %(telegram_id)s::bigint
6 changes: 6 additions & 0 deletions budget_graph/sql/get_group_telegram_ids.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
"telegram_id"
FROM
"budget_graph"."users_groups"
WHERE
"group_id" = %(group_id)s::smallint
7 changes: 7 additions & 0 deletions budget_graph/sql/get_group_usernames.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SELECT
u."username"
FROM
"budget_graph"."users" u
JOIN "budget_graph"."users_groups" u_g ON u."telegram_id" = u_g."telegram_id"
WHERE
u_g."group_id" = %(group_id)s::smallint
15 changes: 15 additions & 0 deletions tests/test_database_queries_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,21 @@ def test_003_user_timezone_to_db_5(self):
res_2: int | None = self.test_db.get_user_timezone_by_telegram_id(telegram_id)
self.assertEqual(res_2, timezone_2)

def test_003_user_timezone_to_db_6(self):
"""
id = 0
"""
res: int | None = self.test_db.get_user_timezone_by_telegram_id(0)
self.assertIsNone(res)

def test_003_user_timezone_to_db_7(self):
"""
negative integers
"""
for i in range(-1_000, 0):
res: int | None = self.test_db.get_user_timezone_by_telegram_id(0)
self.assertIsNone(res, f'i = {i}')

def test_004_update_group_uuid_after_transaction_1(self):
"""
check that the new group has uuid = null
Expand Down

0 comments on commit efdab9e

Please sign in to comment.