Ensure one db instance only
Ensure testing db is correctly set to sqlite
This commit is contained in:
parent
c0b7bf76f5
commit
71257e4d67
8
.envrc
8
.envrc
@ -4,17 +4,17 @@ export MAIL_PORT=587
|
|||||||
export MAIL_SERVER="smtp.fastmail.com"
|
export MAIL_SERVER="smtp.fastmail.com"
|
||||||
export MAIL_USERNAME="kae@midnighthax.com"
|
export MAIL_USERNAME="kae@midnighthax.com"
|
||||||
export MAIL_USE_TLS=True
|
export MAIL_USE_TLS=True
|
||||||
export PYGAME_HIDE_SUPPORT_PROMPT=1
|
|
||||||
branch=$(git branch --show-current)
|
branch=$(git branch --show-current)
|
||||||
|
|
||||||
# Always treat running from /home/kae/mm as production
|
# Always treat running from /home/kae/mm as production
|
||||||
if [ $(pwd) == /home/kae/mm ]; then
|
if [ $(pwd) == /home/kae/mm ]; then
|
||||||
export MM_ENV="PRODUCTION"
|
export MM_ENV="PRODUCTION"
|
||||||
export ALCHEMICAL_DATABASE_URI="mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod"
|
export DATABASE_URL="mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod"
|
||||||
elif on_git_branch master; then
|
elif on_git_branch master; then
|
||||||
export MM_ENV="PRODUCTION"
|
export MM_ENV="PRODUCTION"
|
||||||
export ALCHEMICAL_DATABASE_URI="mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod"
|
export DATABASE_URL="mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod"
|
||||||
else
|
else
|
||||||
export MM_ENV="DEVELOPMENT"
|
export MM_ENV="DEVELOPMENT"
|
||||||
export ALCHEMICAL_DATABASE_URI="mysql+mysqldb://dev_musicmuster:dev_musicmuster@localhost/dev_musicmuster"
|
export DATABASE_URL="mysql+mysqldb://dev_musicmuster:dev_musicmuster@localhost/dev_musicmuster"
|
||||||
export PYTHONBREAKPOINT="pudb.set_trace"
|
export PYTHONBREAKPOINT="pudb.set_trace"
|
||||||
fi
|
fi
|
||||||
|
|||||||
30
app/dbmanager.py
Normal file
30
app/dbmanager.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# Standard library imports
|
||||||
|
|
||||||
|
# PyQt imports
|
||||||
|
|
||||||
|
# Third party imports
|
||||||
|
from alchemical import Alchemical # type:ignore
|
||||||
|
|
||||||
|
# App imports
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseManager:
|
||||||
|
"""
|
||||||
|
Singleton class to ensure we only ever have one db object
|
||||||
|
"""
|
||||||
|
|
||||||
|
__instance = None
|
||||||
|
|
||||||
|
def __init__(self, database_url, **kwargs):
|
||||||
|
if DatabaseManager.__instance is None:
|
||||||
|
self.db = Alchemical(database_url, **kwargs)
|
||||||
|
self.db.create_all()
|
||||||
|
DatabaseManager.__instance = self
|
||||||
|
else:
|
||||||
|
raise Exception("Attempted to create a second DatabaseManager instance")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_instance(database_url, **kwargs):
|
||||||
|
if DatabaseManager.__instance is None:
|
||||||
|
DatabaseManager(database_url, **kwargs)
|
||||||
|
return DatabaseManager.__instance
|
||||||
@ -8,7 +8,6 @@ import sys
|
|||||||
# PyQt imports
|
# PyQt imports
|
||||||
|
|
||||||
# Third party imports
|
# Third party imports
|
||||||
from alchemical import Alchemical # type:ignore
|
|
||||||
from sqlalchemy import (
|
from sqlalchemy import (
|
||||||
bindparam,
|
bindparam,
|
||||||
delete,
|
delete,
|
||||||
@ -22,18 +21,20 @@ from sqlalchemy.orm import joinedload
|
|||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
# App imports
|
# App imports
|
||||||
|
from dbmanager import DatabaseManager
|
||||||
import dbtables
|
import dbtables
|
||||||
from config import Config
|
from config import Config
|
||||||
from log import log
|
from log import log
|
||||||
|
|
||||||
|
|
||||||
# Establish database connection
|
# Establish database connection
|
||||||
ALCHEMICAL_DATABASE_URI = os.environ.get("ALCHEMICAL_DATABASE_URI")
|
DATABASE_URL = os.environ.get("DATABASE_URL")
|
||||||
if ALCHEMICAL_DATABASE_URI is None:
|
if DATABASE_URL is None:
|
||||||
raise ValueError("ALCHEMICAL_DATABASE_URI is undefined")
|
raise ValueError("DATABASE_URL is undefined")
|
||||||
if "unittest" in sys.modules and "sqlite" not in ALCHEMICAL_DATABASE_URI:
|
if "unittest" in sys.modules and "sqlite" not in DATABASE_URL:
|
||||||
raise ValueError("Unit tests running on non-Sqlite database")
|
raise ValueError("Unit tests running on non-Sqlite database")
|
||||||
db = Alchemical(ALCHEMICAL_DATABASE_URI, engine_options=Config.ENGINE_OPTIONS)
|
db = DatabaseManager.get_instance(DATABASE_URL, engine_options=Config.ENGINE_OPTIONS).db
|
||||||
|
db.create_all()
|
||||||
|
|
||||||
|
|
||||||
# Database classes
|
# Database classes
|
||||||
|
|||||||
14
tests/__init__.py
Normal file
14
tests/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# Standard library imports
|
||||||
|
import os
|
||||||
|
|
||||||
|
# PyQt imports
|
||||||
|
|
||||||
|
# Third party imports
|
||||||
|
|
||||||
|
# App imports
|
||||||
|
# Set up test database before importing db
|
||||||
|
# https://blog.miguelgrinberg.com/post/how-to-write-unit-tests-in-python-part-3-web-applications
|
||||||
|
DB_FILE = "/tmp/mm.db"
|
||||||
|
if os.path.exists(DB_FILE):
|
||||||
|
os.unlink(DB_FILE)
|
||||||
|
os.environ["DATABASE_URL"] = "sqlite:///" + DB_FILE
|
||||||
@ -1,5 +1,4 @@
|
|||||||
# Standard library imports
|
# Standard library imports
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
# PyQt imports
|
# PyQt imports
|
||||||
@ -8,15 +7,7 @@ import unittest
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
# App imports
|
# App imports
|
||||||
# Set up test database before importing db
|
from app.models import db, Settings
|
||||||
# Mark subsequent lines to ignore E402, imports not at top of file
|
|
||||||
# Set up test database before importing db
|
|
||||||
# Mark subsequent lines to ignore E402, imports not at top of file
|
|
||||||
DB_FILE = "/tmp/mm.db"
|
|
||||||
if os.path.exists(DB_FILE):
|
|
||||||
os.unlink(DB_FILE)
|
|
||||||
os.environ["ALCHEMICAL_DATABASE_URI"] = "sqlite:///" + DB_FILE
|
|
||||||
from models import db, Settings # noqa: E402
|
|
||||||
|
|
||||||
|
|
||||||
class TestMMMisc(unittest.TestCase):
|
class TestMMMisc(unittest.TestCase):
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
# Standard library imports
|
# Standard library imports
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
# PyQt imports
|
# PyQt imports
|
||||||
@ -10,13 +9,7 @@ import unittest
|
|||||||
# App imports
|
# App imports
|
||||||
from app import helpers
|
from app import helpers
|
||||||
|
|
||||||
# Set up test database before importing db
|
from app.models import (
|
||||||
# Mark subsequent lines to ignore E402, imports not at top of file
|
|
||||||
DB_FILE = "/tmp/mm.db"
|
|
||||||
if os.path.exists(DB_FILE):
|
|
||||||
os.unlink(DB_FILE)
|
|
||||||
os.environ["ALCHEMICAL_DATABASE_URI"] = "sqlite:///" + DB_FILE
|
|
||||||
from app.models import ( # noqa: E402
|
|
||||||
db,
|
db,
|
||||||
NoteColours,
|
NoteColours,
|
||||||
Playdates,
|
Playdates,
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
# Standard library imports
|
# Standard library imports
|
||||||
import os
|
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
# PyQt imports
|
# PyQt imports
|
||||||
@ -9,15 +8,8 @@ from PyQt6.QtCore import Qt, QModelIndex
|
|||||||
|
|
||||||
# App imports
|
# App imports
|
||||||
from app.helpers import get_all_track_metadata
|
from app.helpers import get_all_track_metadata
|
||||||
|
from app import playlistmodel
|
||||||
# Set up test database before importing db
|
from app.models import (
|
||||||
# Mark subsequent lines to ignore E402, imports not at top of file
|
|
||||||
DB_FILE = "/tmp/mm.db"
|
|
||||||
if os.path.exists(DB_FILE):
|
|
||||||
os.unlink(DB_FILE)
|
|
||||||
os.environ["ALCHEMICAL_DATABASE_URI"] = "sqlite:///" + DB_FILE
|
|
||||||
from app import playlistmodel # noqa: E402
|
|
||||||
from app.models import ( # noqa: E402
|
|
||||||
db,
|
db,
|
||||||
Playlists,
|
Playlists,
|
||||||
Tracks,
|
Tracks,
|
||||||
|
|||||||
@ -3,22 +3,19 @@ import os
|
|||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
# PyQt imports
|
# PyQt imports
|
||||||
|
from PyQt6.QtCore import Qt
|
||||||
|
from PyQt6.QtGui import QColor
|
||||||
|
|
||||||
# Third party imports
|
# Third party imports
|
||||||
import pytest
|
import pytest
|
||||||
from pytestqt.plugin import QtBot # type: ignore
|
from pytestqt.plugin import QtBot # type: ignore
|
||||||
|
|
||||||
# App imports
|
# App imports
|
||||||
|
from config import Config
|
||||||
# Set up test database before importing db
|
|
||||||
# Mark subsequent lines to ignore E402, imports not at top of file
|
|
||||||
DB_FILE = "/tmp/mm.db"
|
|
||||||
if os.path.exists(DB_FILE):
|
|
||||||
os.unlink(DB_FILE)
|
|
||||||
os.environ["ALCHEMICAL_DATABASE_URI"] = "sqlite:///" + DB_FILE
|
|
||||||
from app import playlistmodel, utilities
|
from app import playlistmodel, utilities
|
||||||
from app.models import ( # noqa: E402
|
from app.models import (
|
||||||
db,
|
db,
|
||||||
|
NoteColours,
|
||||||
Playlists,
|
Playlists,
|
||||||
Tracks,
|
Tracks,
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user