mirror of
https://github.com/simple-login/app.git
synced 2025-02-23 15:23:27 +08:00
use postgres database in test instead of sqlite
This commit is contained in:
parent
006a7b1420
commit
07c912fd35
2 changed files with 27 additions and 31 deletions
|
@ -1,53 +1,49 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
# use the tests/test.env config fle
|
||||||
# flake8: noqa: E402
|
# flake8: noqa: E402
|
||||||
|
|
||||||
os.environ["CONFIG"] = os.path.abspath(
|
os.environ["CONFIG"] = os.path.abspath(
|
||||||
os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests/test.env")
|
os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests/test.env")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# use in-memory database
|
|
||||||
# need to set before importing any other module as DB_URI is init at import time
|
|
||||||
os.environ["DB_URI"] = "sqlite://"
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from app.extensions import db
|
from app.extensions import db
|
||||||
from server import create_app
|
from server import create_app
|
||||||
from init_app import add_sl_domains
|
from init_app import add_sl_domains
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
app.config["TESTING"] = True
|
||||||
|
app.config["WTF_CSRF_ENABLED"] = False
|
||||||
|
app.config["SERVER_NAME"] = "sl.test"
|
||||||
|
|
||||||
@pytest.fixture
|
with app.app_context():
|
||||||
def flask_app():
|
|
||||||
app = create_app()
|
|
||||||
|
|
||||||
# use in-memory database
|
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
|
|
||||||
app.config["TESTING"] = True
|
|
||||||
app.config["WTF_CSRF_ENABLED"] = False
|
|
||||||
app.config["SERVER_NAME"] = "sl.test"
|
|
||||||
|
|
||||||
with app.app_context():
|
|
||||||
db.create_all()
|
db.create_all()
|
||||||
add_sl_domains()
|
add_sl_domains()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def flask_app():
|
||||||
yield app
|
yield app
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def flask_client():
|
def flask_client():
|
||||||
app = create_app()
|
|
||||||
|
|
||||||
# use in-memory database
|
|
||||||
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://"
|
|
||||||
app.config["TESTING"] = True
|
|
||||||
app.config["WTF_CSRF_ENABLED"] = False
|
|
||||||
app.config["SERVER_NAME"] = "sl.test"
|
|
||||||
|
|
||||||
client = app.test_client()
|
|
||||||
|
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
# replace db.session to that we can rollback all commits that can be made during a test
|
||||||
add_sl_domains()
|
# inspired from http://alexmic.net/flask-sqlalchemy-pytest/
|
||||||
|
connection = db.engine.connect()
|
||||||
|
transaction = connection.begin()
|
||||||
|
options = dict(bind=connection, binds={})
|
||||||
|
session = db.create_scoped_session(options=options)
|
||||||
|
db.session = session
|
||||||
|
|
||||||
|
try:
|
||||||
|
client = app.test_client()
|
||||||
yield client
|
yield client
|
||||||
|
finally:
|
||||||
|
# roll back all commits made during a test
|
||||||
|
transaction.rollback()
|
||||||
|
connection.close()
|
||||||
|
session.remove()
|
||||||
|
|
|
@ -16,7 +16,7 @@ DKIM_PRIVATE_KEY_PATH=local_data/dkim.key
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
RESET_DB=true
|
RESET_DB=true
|
||||||
DB_URI=sqlite:///db.sqlite
|
DB_URI=postgresql://db/test
|
||||||
|
|
||||||
# Flask
|
# Flask
|
||||||
FLASK_SECRET=secret
|
FLASK_SECRET=secret
|
||||||
|
|
Loading…
Reference in a new issue