-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_lifespan.py
More file actions
71 lines (44 loc) · 1.9 KB
/
test_lifespan.py
File metadata and controls
71 lines (44 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from fastapi import FastAPI
from pytest import raises, fixture
_app = FastAPI()
@fixture(params=[_app, None])
def app(request):
# lifespan tests pass whether lifespan receives app or None
return request.param
async def test_it_returns_state(environ, app):
from fastsqla import lifespan
async with lifespan(app) as state:
assert "fastsqla_engine" in state
async def test_it_binds_an_sqla_engine_to_sessionmaker(environ, app):
from fastsqla import SessionFactory, lifespan
assert SessionFactory.kw["bind"] is None
async with lifespan(app):
engine = SessionFactory.kw["bind"]
assert engine is not None
assert str(engine.url) == environ["SQLALCHEMY_URL"]
assert SessionFactory.kw["bind"] is None
async def test_it_fails_on_a_missing_sqlalchemy_url(monkeypatch, app):
from fastsqla import lifespan
monkeypatch.delenv("SQLALCHEMY_URL", raising=False)
with raises(Exception) as raise_info:
async with lifespan(app):
pass
assert raise_info.value.args[0] == "Missing sqlalchemy_url in environ."
async def test_it_fails_on_not_async_engine(monkeypatch, app):
from fastsqla import lifespan
monkeypatch.setenv("SQLALCHEMY_URL", "sqlite:///:memory:")
with raises(Exception) as raise_info:
async with lifespan(app):
pass
assert "'pysqlite' is not async." in raise_info.value.args[0]
async def test_new_lifespan_with_connect_args(sqlalchemy_url, app):
from fastsqla import new_lifespan
lifespan = new_lifespan(sqlalchemy_url, connect_args={"autocommit": False})
async with lifespan(app):
pass
async def test_new_lifespan_fails_with_invalid_connect_args(sqlalchemy_url, app):
from fastsqla import new_lifespan
lifespan = new_lifespan(sqlalchemy_url, connect_args={"this is wrong": False})
with raises(TypeError):
async with lifespan(app):
pass