1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 13:35:23 +02:00

feature/additional-db (#371)

* add support for setting db_url

* fix tests

* add db_username/password env variables

* init db if super user doesn't exist

* fix tests

* fix tests

* set SQLite default DB_URL

* don't run tests on draft PRs

* add lint/black tests

* add test-all

* spell check settings

* black/flake8

* check format fail

* new badges

* rename workflow

* fix formatting

* remove white-space

* test connection arguments for pg

* format

* add new values to template

* format

* remove old script

* monkeypatch test db

* working docker-compose for postgres

* update docs

* test pg workflow

* format

* add driver

* install w/ poetry

* setup container

* change image

* set database to localhost

* update tests

* set url

* fix url path

* disable cache

* database init

* bust cache

* get by name

Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
Hayden 2021-05-01 13:35:57 -08:00 committed by GitHub
parent 52e5e9da5d
commit c196445e61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 257 additions and 195 deletions

View file

@ -1,31 +1,24 @@
from mealie.core.config import app_dirs, settings
# ! I don't like it either!
SQLITE_FILE = app_dirs.SQLITE_DIR.joinpath("test.db")
SQLITE_FILE.unlink(missing_ok=True)
settings.SQLITE_FILE = SQLITE_FILE
from tests.pre_test import DB_URL, settings # isort:skip
import json
import requests
from fastapi.testclient import TestClient
from mealie.app import app
from mealie.db.db_setup import generate_session, sql_global_init
from mealie.db.init_db import init_db
from mealie.db.db_setup import SessionLocal, generate_session
from mealie.db.init_db import main
from pytest import fixture
from tests.app_routes import AppRoutes
from tests.test_config import TEST_DATA
from tests.utils.recipe_data import build_recipe_store, get_raw_no_image, get_raw_recipe
TestSessionLocal = sql_global_init(SQLITE_FILE, check_thread=False)
init_db(TestSessionLocal())
main()
def override_get_db():
try:
db = TestSessionLocal()
db = SessionLocal()
yield db
finally:
db.close()
@ -38,7 +31,7 @@ def api_client():
yield TestClient(app)
SQLITE_FILE.unlink()
DB_URL.unlink(missing_ok=True)
@fixture(scope="session")

8
tests/pre_test.py Normal file
View file

@ -0,0 +1,8 @@
from mealie.core.config import determine_sqlite_path, settings
DB_URL = determine_sqlite_path(path=True, suffix="test")
DB_URL.unlink(missing_ok=True)
if settings.DB_ENGINE != "postgres":
# Monkeypatch Database Testing
settings.DB_URL = determine_sqlite_path(path=False, suffix="test")

View file

@ -1,22 +1,25 @@
import re
from pathlib import Path
import pytest
from mealie.core.config import CWD, DATA_DIR, AppDirectories, AppSettings, determine_data_dir, determine_secrets
def test_default_settings(monkeypatch):
monkeypatch.delenv("DEFAULT_GROUP", raising=False)
monkeypatch.delenv("DEFAULT_PASSWORD", raising=False)
monkeypatch.delenv("POSTGRES_USER", raising=False)
monkeypatch.delenv("POSTGRES_PASSWORD", raising=False)
monkeypatch.delenv("DEFAULT_PASSWORD", raising=False)
monkeypatch.delenv("API_PORT", raising=False)
monkeypatch.delenv("API_DOCS", raising=False)
monkeypatch.delenv("DB_TYPE", raising=False)
monkeypatch.delenv("IS_DEMO", raising=False)
app_settings = AppSettings()
assert app_settings.DEFAULT_GROUP == "Home"
assert app_settings.DEFAULT_PASSWORD == "MyPassword"
assert app_settings.DATABASE_TYPE == "sqlite"
assert app_settings.POSTGRES_USER == "mealie"
assert app_settings.POSTGRES_PASSWORD == "mealie"
assert app_settings.API_PORT == 9000
assert app_settings.API_DOCS is True
assert app_settings.IS_DEMO is False
@ -28,6 +31,8 @@ def test_default_settings(monkeypatch):
def test_non_default_settings(monkeypatch):
monkeypatch.setenv("DEFAULT_GROUP", "Test Group")
monkeypatch.setenv("DEFAULT_PASSWORD", "Test Password")
monkeypatch.setenv("POSTGRES_USER", "mealie-test")
monkeypatch.setenv("POSTGRES_PASSWORD", "mealie-test")
monkeypatch.setenv("API_PORT", "8000")
monkeypatch.setenv("API_DOCS", "False")
@ -35,6 +40,8 @@ def test_non_default_settings(monkeypatch):
assert app_settings.DEFAULT_GROUP == "Test Group"
assert app_settings.DEFAULT_PASSWORD == "Test Password"
assert app_settings.POSTGRES_USER == "mealie-test"
assert app_settings.POSTGRES_PASSWORD == "mealie-test"
assert app_settings.API_PORT == 8000
assert app_settings.API_DOCS is False
@ -42,11 +49,17 @@ def test_non_default_settings(monkeypatch):
assert app_settings.DOCS_URL is None
def test_unknown_database(monkeypatch):
monkeypatch.setenv("DB_TYPE", "nonsense")
def test_default_connection_args(monkeypatch):
monkeypatch.setenv("DB_ENGINE", "sqlite")
app_settings = AppSettings()
assert re.match(r"sqlite:////.*mealie/dev/data/mealie_v0.5.0.db", app_settings.DB_URL)
with pytest.raises(ValueError, match="Unable to determine database type. Acceptible options are 'sqlite'"):
AppSettings()
def test_pg_connection_args(monkeypatch):
monkeypatch.setenv("DB_ENGINE", "postgres")
monkeypatch.setenv("POSTGRES_SERVER", "postgres")
app_settings = AppSettings()
assert app_settings.DB_URL == "postgresql://mealie:mealie@postgres:5432/mealie"
def test_secret_generation(tmp_path):