diff --git a/alembic.ini b/alembic.ini index 3bfbfba..214a93f 100644 --- a/alembic.ini +++ b/alembic.ini @@ -39,8 +39,10 @@ prepend_sys_path = . # are written from script.py.mako # output_encoding = utf-8 -sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_dev +sqlalchemy.url = SET # sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_prod +# sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_dev +# sqlalchemy.url = mysql+mysqldb://musicmuster:musicmuster@localhost/musicmuster_v2 [post_write_hooks] # post_write_hooks defines scripts or Python functions that are run diff --git a/app/model.py b/app/models.py similarity index 96% rename from app/model.py rename to app/models.py index 747c4ad..80fb963 100644 --- a/app/model.py +++ b/app/models.py @@ -45,7 +45,7 @@ class NoteColours(Base): id = Column(Integer, primary_key=True, autoincrement=True) substring = Column(String(256), index=False) - hexcolour = Column(String(6), index=False) + colour = Column(String(21), index=False) enabled = Column(Boolean, default=True, index=True) is_regex = Column(Boolean, default=False, index=False) is_casesensitive = Column(Boolean, default=False, index=False) @@ -53,9 +53,16 @@ class NoteColours(Base): def __repr__(self): return ( - f"" + f"" ) + @classmethod + def get_all(self): + """Return all records""" + + return session.query(cls).all() + @staticmethod def get_colour(session, text): """ @@ -66,24 +73,24 @@ class NoteColours(Base): for rec in ( session.query(NoteColours) - .filter(NoteColours.enabled == True) - .order_by(NoteColours.order) - .all() + .filter(NoteColours.enabled.is_(True)) + .order_by(NoteColours.order) + .all() ): if rec.is_regex: - if rec.is_casesensitive: - p = re.compile(rec.substring) - else: - p = re.compile(rec.substring, re.IGNORECASE) + flags = re.UNICODE + if not rec.is_casesensitive: + flags |= re.IGNORECASE + p = re.compile(rec.substring, flags) if p.match(text): - return '#' + rec.hexcolour + return rec.colour else: if rec.is_casesensitive: if rec.substring in text: - return '#' + rec.hexcolour + return rec.colour else: if rec.substring.lower() in text.lower(): - return '#' + rec.hexcolour + return rec.colour return None diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..97f95e7 --- /dev/null +++ b/conftest.py @@ -0,0 +1,83 @@ +# https://stewartadam.io/blog/2019/04/04/testing-flask-applications-code-database-views-flask-config-and-app-context-pytest + +import pytest +import sys + +sys.path.append("app") + +from config import Config # noqa E402 +from flask import g # noqa E402 +from kpi import create_app # noqa E402 +from kpi import db as _db # noqa E402 + + +class TestConfig(Config): + # TESTING = True + SQLALCHEMY_DATABASE_URI = 'sqlite://' + PYTESTING = True + +# @pytest.fixture(scope="module") +# def app(): +# app = create_app(TestConfig) +# app_context = app.app_context() +# app_context.push() +# db.create_all() +# return app + + +@pytest.fixture +def no_bank_holidays(): + "Set no bank holidays" + + g.BankHolidays = [] + + +@pytest.fixture(scope="session") +def app(request): + """Test session-wide test `Flask` application.""" + + app = create_app(TestConfig) + return app + + +@pytest.fixture(autouse=True) +def _setup_app_context_for_test(request, app): + """ + Given app is session-wide, sets up a app context per test to ensure that + app and request stack is not shared between tests. + """ + + ctx = app.app_context() + ctx.push() + yield # tests will run here + ctx.pop() + + +@pytest.fixture(scope="session") +def db(app, request): + """Returns session-wide initialized database""" + + with app.app_context(): + _db.create_all() + yield _db + _db.drop_all() + + +@pytest.fixture(scope="function") +def session(app, db, request): + """Creates a new database session for each test, + rolling back changes afterwards""" + + connection = _db.engine.connect() + transaction = connection.begin() + + options = dict(bind=connection, binds={}) + session = _db.create_scoped_session(options=options) + + _db.session = session + + yield session + + transaction.rollback() + connection.close() + session.remove() diff --git a/conftest.py.2b b/conftest.py.2b new file mode 100644 index 0000000..97f95e7 --- /dev/null +++ b/conftest.py.2b @@ -0,0 +1,83 @@ +# https://stewartadam.io/blog/2019/04/04/testing-flask-applications-code-database-views-flask-config-and-app-context-pytest + +import pytest +import sys + +sys.path.append("app") + +from config import Config # noqa E402 +from flask import g # noqa E402 +from kpi import create_app # noqa E402 +from kpi import db as _db # noqa E402 + + +class TestConfig(Config): + # TESTING = True + SQLALCHEMY_DATABASE_URI = 'sqlite://' + PYTESTING = True + +# @pytest.fixture(scope="module") +# def app(): +# app = create_app(TestConfig) +# app_context = app.app_context() +# app_context.push() +# db.create_all() +# return app + + +@pytest.fixture +def no_bank_holidays(): + "Set no bank holidays" + + g.BankHolidays = [] + + +@pytest.fixture(scope="session") +def app(request): + """Test session-wide test `Flask` application.""" + + app = create_app(TestConfig) + return app + + +@pytest.fixture(autouse=True) +def _setup_app_context_for_test(request, app): + """ + Given app is session-wide, sets up a app context per test to ensure that + app and request stack is not shared between tests. + """ + + ctx = app.app_context() + ctx.push() + yield # tests will run here + ctx.pop() + + +@pytest.fixture(scope="session") +def db(app, request): + """Returns session-wide initialized database""" + + with app.app_context(): + _db.create_all() + yield _db + _db.drop_all() + + +@pytest.fixture(scope="function") +def session(app, db, request): + """Creates a new database session for each test, + rolling back changes afterwards""" + + connection = _db.engine.connect() + transaction = connection.begin() + + options = dict(bind=connection, binds={}) + session = _db.create_scoped_session(options=options) + + _db.session = session + + yield session + + transaction.rollback() + connection.close() + session.remove() diff --git a/test_model.py b/test_model.py new file mode 100644 index 0000000..b659ecd --- /dev/null +++ b/test_model.py @@ -0,0 +1,7 @@ +from model import Playlists, Session + + +def test_get_colour(): + with Session() as session: + x = Playlists.get_all_playlists(session) + assert False diff --git a/tests/__init__.py b/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_model.py b/tests/test_model.py deleted file mode 100644 index 49c2587..0000000 --- a/tests/test_model.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_get_colour(): - assert False