From f0ef8cd43fadf4027100d2f6a77d6ebb1e40ceb1 Mon Sep 17 00:00:00 2001 From: quimnuss Date: Sat, 11 Feb 2023 20:59:53 +0100 Subject: [PATCH] parametrize db connection, with tests --- config/test_db_config.txt | 3 +++ federation/database.py | 24 ++++++++++++++++-------- tests/test_database.py | 11 +++++++++-- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/config/test_db_config.txt b/config/test_db_config.txt index e69de29..6faa159 100644 --- a/config/test_db_config.txt +++ b/config/test_db_config.txt @@ -0,0 +1,3 @@ +fediverse_db: fediverse_test +fediverse_db_user: tester +fediverse_db_user_password: tester \ No newline at end of file diff --git a/federation/database.py b/federation/database.py index e18aca9..5313dbb 100644 --- a/federation/database.py +++ b/federation/database.py @@ -6,7 +6,6 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT import uuid from datetime import datetime import pytz -import pdb tz = pytz.timezone('Europe/Madrid') @@ -14,12 +13,14 @@ class Database(): name = 'fediverse database library' - def __init__(self, config_file=None, fediverse_db=None, fediverse_db_user=None, fediverse_db_user_password=None): + def __init__(self, config_file=None, fediverse_db=None, fediverse_db_user=None, fediverse_db_user_password=None, fediverse_db_host='/var/run/postgresql', fediverse_db_port='6432'): self.config_file = config_file or "config/db_config.txt" self.fediverse_db = fediverse_db or self.__get_parameter("fediverse_db", self.config_file) self.fediverse_db_user = fediverse_db_user or self.__get_parameter("fediverse_db_user", self.config_file) self.fediverse_db_user_password = fediverse_db_user_password or self.__get_parameter("fediverse_db_user_password", self.config_file) + self.fediverse_db_host = fediverse_db_host + self.fediverse_db_port = fediverse_db_port db_setup = self.__check_dbsetup(self) @@ -43,7 +44,7 @@ class Database(): try: - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) cur = conn.cursor() @@ -73,7 +74,7 @@ class Database(): try: - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) cur = conn.cursor() @@ -743,7 +744,7 @@ class Database(): conn = None - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) cur = conn.cursor() @@ -787,7 +788,7 @@ class Database(): conn = None - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) cur = conn.cursor() @@ -935,6 +936,13 @@ class Database(): conn.close() + def check_connection(self): + + try: + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) + finally: + conn.close() + @staticmethod def __check_dbsetup(self): @@ -944,7 +952,7 @@ class Database(): conn = None - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) db_setup = True @@ -1023,7 +1031,7 @@ class Database(): try: - conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = "/var/run/postgresql", port = "6432") + conn = psycopg2.connect(database = self.fediverse_db, user = self.fediverse_db_user, password = self.fediverse_db_user_password, host = self.fediverse_db_host, port = self.fediverse_db_port) cur = conn.cursor() diff --git a/tests/test_database.py b/tests/test_database.py index 9eb8288..cee424f 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -1,8 +1,15 @@ from federation import Database def test__database_connection(): - db = Database(config_file='config/test_db_config.txt') + db = Database(config_file='config/test_db_config.txt', fediverse_db_host='localhost', fediverse_db_port='5432') + + # noexcept + db.check_connection() + + +def test__database_connection__noservers(): + db = Database(config_file='config/test_db_config.txt', fediverse_db_host='localhost', fediverse_db_port='5432') checked_server = db.get_last_checked_servers() - assert checked_server \ No newline at end of file + assert checked_server == [] \ No newline at end of file