forkeado de spla/fediverse
parametrize db connection, with tests
This commit is contained in:
pare
298d8cb9c2
commit
f0ef8cd43f
S'han modificat 3 arxius amb 28 adicions i 10 eliminacions
|
@ -0,0 +1,3 @@
|
||||||
|
fediverse_db: fediverse_test
|
||||||
|
fediverse_db_user: tester
|
||||||
|
fediverse_db_user_password: tester
|
|
@ -6,7 +6,6 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import pytz
|
import pytz
|
||||||
import pdb
|
|
||||||
|
|
||||||
tz = pytz.timezone('Europe/Madrid')
|
tz = pytz.timezone('Europe/Madrid')
|
||||||
|
|
||||||
|
@ -14,12 +13,14 @@ class Database():
|
||||||
|
|
||||||
name = 'fediverse database library'
|
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.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 = 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 = 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_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)
|
db_setup = self.__check_dbsetup(self)
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ class Database():
|
||||||
|
|
||||||
try:
|
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()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
@ -73,7 +74,7 @@ class Database():
|
||||||
|
|
||||||
try:
|
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()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
@ -743,7 +744,7 @@ class Database():
|
||||||
|
|
||||||
conn = None
|
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()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
@ -787,7 +788,7 @@ class Database():
|
||||||
|
|
||||||
conn = None
|
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()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
@ -935,6 +936,13 @@ class Database():
|
||||||
|
|
||||||
conn.close()
|
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
|
@staticmethod
|
||||||
def __check_dbsetup(self):
|
def __check_dbsetup(self):
|
||||||
|
|
||||||
|
@ -944,7 +952,7 @@ class Database():
|
||||||
|
|
||||||
conn = None
|
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
|
db_setup = True
|
||||||
|
|
||||||
|
@ -1023,7 +1031,7 @@ class Database():
|
||||||
|
|
||||||
try:
|
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()
|
cur = conn.cursor()
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
from federation import Database
|
from federation import Database
|
||||||
|
|
||||||
def test__database_connection():
|
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()
|
checked_server = db.get_last_checked_servers()
|
||||||
|
|
||||||
assert checked_server
|
assert checked_server == []
|
Loading…
Referencia en una nova incidència