| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os.path |
| 4 |
import typing as t |
| 5 |
import unittest.mock |
| 6 |
|
| 7 |
import pytest |
| 8 |
import sqlalchemy as sa |
| 9 |
import sqlalchemy.pool |
| 10 |
from flask import Flask |
| 11 |
|
| 12 |
from flask_sqlalchemy import SQLAlchemy |
| 13 |
|
| 14 |
|
| 15 |
def test_default_engine(app: Flask, db: SQLAlchemy) -> None: |
| 16 |
with app.app_context(): |
| 17 |
assert db.engine is db.engines[None] |
| 18 |
|
| 19 |
with pytest.raises(RuntimeError): |
| 20 |
assert db.engine |
| 21 |
|
| 22 |
|
| 23 |
@pytest.mark.usefixtures("app_ctx") |
| 24 |
def test_engine_per_bind(app: Flask, model_class: t.Any) -> None: |
| 25 |
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"} |
| 26 |
db = SQLAlchemy(app, model_class=model_class) |
| 27 |
assert db.engines["a"] is not db.engine |
| 28 |
|
| 29 |
|
| 30 |
@pytest.mark.usefixtures("app_ctx") |
| 31 |
def test_config_engine_options(app: Flask, model_class: t.Any) -> None: |
| 32 |
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"echo": True} |
| 33 |
db = SQLAlchemy(app, model_class=model_class) |
| 34 |
assert db.engine.echo |
| 35 |
|
| 36 |
|
| 37 |
@pytest.mark.usefixtures("app_ctx") |
| 38 |
def test_init_engine_options(app: Flask, model_class: t.Any) -> None: |
| 39 |
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"echo": False} |
| 40 |
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"} |
| 41 |
db = SQLAlchemy(app, engine_options={"echo": True}, model_class=model_class) |
| 42 |
# init is default |
| 43 |
assert db.engines["a"].echo |
| 44 |
# config overrides init |
| 45 |
assert not db.engine.echo |
| 46 |
|
| 47 |
|
| 48 |
@pytest.mark.usefixtures("app_ctx") |
| 49 |
def test_config_echo(app: Flask, model_class: t.Any) -> None: |
| 50 |
app.config["SQLALCHEMY_ECHO"] = True |
| 51 |
db = SQLAlchemy(app, model_class=model_class) |
| 52 |
assert db.engine.echo |
| 53 |
assert db.engine.pool.echo |
| 54 |
|
| 55 |
|
| 56 |
@pytest.mark.usefixtures("app_ctx") |
| 57 |
@pytest.mark.parametrize( |
| 58 |
"value", |
| 59 |
[ |
| 60 |
"sqlite://", |
| 61 |
sa.engine.URL.create("sqlite"), |
| 62 |
{"url": "sqlite://"}, |
| 63 |
{"url": sa.engine.URL.create("sqlite")}, |
| 64 |
], |
| 65 |
) |
| 66 |
def test_url_type(app: Flask, model_class: t.Any, value: str | sa.engine.URL) -> None: |
| 67 |
app.config["SQLALCHEMY_BINDS"] = {"a": value} |
| 68 |
db = SQLAlchemy(app, model_class=model_class) |
| 69 |
assert str(db.engines["a"].url) == "sqlite://" |
| 70 |
|
| 71 |
|
| 72 |
def test_no_binds_error(app: Flask, model_class: t.Any) -> None: |
| 73 |
del app.config["SQLALCHEMY_DATABASE_URI"] |
| 74 |
|
| 75 |
with pytest.raises(RuntimeError) as info: |
| 76 |
SQLAlchemy(app, model_class=model_class) |
| 77 |
|
| 78 |
e = "Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set." |
| 79 |
assert str(info.value) == e |
| 80 |
|
| 81 |
|
| 82 |
@pytest.mark.usefixtures("app_ctx") |
| 83 |
def test_no_default_url(app: Flask, model_class: t.Any) -> None: |
| 84 |
del app.config["SQLALCHEMY_DATABASE_URI"] |
| 85 |
app.config["SQLALCHEMY_BINDS"] = {"a": "sqlite://"} |
| 86 |
db = SQLAlchemy(app, model_class=model_class, engine_options={"echo": True}) |
| 87 |
assert None not in db.engines |
| 88 |
assert "a" in db.engines |
| 89 |
|
| 90 |
|
| 91 |
@pytest.mark.usefixtures("app_ctx") |
| 92 |
def test_sqlite_relative_path(app: Flask, model_class: t.Any) -> None: |
| 93 |
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///test.db" |
| 94 |
db = SQLAlchemy(app, model_class=model_class) |
| 95 |
db.create_all() |
| 96 |
assert not isinstance(db.engine.pool, sa.pool.StaticPool) |
| 97 |
db_path = db.engine.url.database |
| 98 |
assert db_path.startswith(app.instance_path) # type: ignore[union-attr] |
| 99 |
assert os.path.exists(db_path) # type: ignore[arg-type] |
| 100 |
|
| 101 |
|
| 102 |
@pytest.mark.usefixtures("app_ctx") |
| 103 |
def test_sqlite_driver_level_uri(app: Flask, model_class: t.Any) -> None: |
| 104 |
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///file:test.db?uri=true" |
| 105 |
db = SQLAlchemy(app, model_class=model_class) |
| 106 |
db.create_all() |
| 107 |
db_path = db.engine.url.database |
| 108 |
assert db_path is not None |
| 109 |
assert db_path.startswith(f"file:{app.instance_path}") |
| 110 |
assert os.path.exists(db_path[5:]) |
| 111 |
|
| 112 |
|
| 113 |
@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True) |
| 114 |
def test_sqlite_memory_defaults( |
| 115 |
make_engine: unittest.mock.Mock, app: Flask, model_class: t.Any |
| 116 |
) -> None: |
| 117 |
SQLAlchemy(app, model_class=model_class) |
| 118 |
options = make_engine.call_args[0][2] |
| 119 |
assert options["poolclass"] is sa.pool.StaticPool |
| 120 |
assert options["connect_args"]["check_same_thread"] is False |
| 121 |
|
| 122 |
|
| 123 |
@unittest.mock.patch.object(SQLAlchemy, "_make_engine", autospec=True) |
| 124 |
def test_mysql_defaults( |
| 125 |
make_engine: unittest.mock.Mock, app: Flask, model_class: t.Any |
| 126 |
) -> None: |
| 127 |
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql:///test" |
| 128 |
SQLAlchemy(app, model_class=model_class) |
| 129 |
options = make_engine.call_args[0][2] |
| 130 |
assert options["pool_recycle"] == 7200 |
| 131 |
assert options["url"].query["charset"] == "utf8mb4" |