Skip to content

Commit

Permalink
Getting unit test db to work without template
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckinghamAJ committed Oct 24, 2023
1 parent a387bde commit 9bc36a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
26 changes: 16 additions & 10 deletions src/fbo_scraper/db/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,23 @@ class TestDAL(DataAccessLayer):
Creates a database connection and session specifically for testing.
"""

test_db_uris = ['postgres://circleci@localhost:5432/smartie-test?sslmode=disable',
'postgresql+psycopg2://localhost/test',
'postgresql+psycopg2://circleci_dev:srtpass@localhost:5432/test']
_is_test = None
def __init__(self):
self._username = "circleci"
self._password = "srtpass"
conn_string = f"postgresql+psycopg2://{self.username}:{self.password}@localhost/test"
super().__init__(conn_string)

@property
def username(self):
if self._username is None:
self._username = input("Enter PostgreSQL username: ")
return self._username

@property
def is_test(self):
if self._is_test is None:
self._is_test = self.conn_string in self.test_db_uris
return self._is_test
def password(self):
if self._password is None:
self._password = input("Enter PostgreSQL password: ")
return self._password

def connect(self):
self.create_test_postgres_db()
Expand All @@ -139,9 +145,9 @@ def connect(self):
self.Session = sessionmaker(self.engine)

def drop_test_postgres_db(self):
if database_exists(self.conn_string) and self.is_test:
if database_exists(self.conn_string):
drop_database(self.conn_string)

def create_test_postgres_db(self):
if not database_exists(self.conn_string) and self.is_test:
if not database_exists(self.conn_string):
create_database(self.conn_string)
3 changes: 2 additions & 1 deletion src/fbo_scraper/db/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime

from sqlalchemy.ext.declarative import declarative_base
#from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy import (
Column,
Integer,
Expand Down
36 changes: 3 additions & 33 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,17 @@
import pytest
from fbo_scraper.db.db_utils import DataAccessLayer
from sqlalchemy_utils import database_exists, create_database, drop_database
import getpass
from fbo_scraper.db.connection import TestDAL

class TestDB(DataAccessLayer):

def __init__(self):
self._username = "circleci"
self._password = "srtpass"
conn_string = f"postgresql+psycopg2://{self.username}:{self.password}@localhost/test"
super().__init__(conn_string)


@property
def username(self):
if self._username is None:
self._username = getpass.getuser()
return self._username

@property
def password(self):
if self._password is None:
self._password = getpass.getpass()
return self._password

def drop_test_postgres_db(self):
if database_exists(self.conn_string):
drop_database(self.conn_string)

def create_test_postgres_db(self):
if not database_exists(self.conn_string):
create_database(self.conn_string, template="template_srt")

@pytest.fixture(scope="class")
def db_class(request):

# set a class attribute on the invoking test context
request.cls.dal = TestDB()
request.cls.dal = TestDAL()

@pytest.fixture()
def db_access_layer():

dal = TestDB()
dal = TestDAL()
dal.create_test_postgres_db()
dal.connect()

Expand Down

0 comments on commit 9bc36a3

Please sign in to comment.