1
1
"""
2
2
Additional tests to reach target coverage of 97.22%
3
3
"""
4
- import pytest
4
+
5
5
from fastapi import FastAPI
6
6
7
7
8
8
def test_commit_on_exit_parameter ():
9
9
"""Test commit_on_exit parameter in middleware initialization"""
10
10
from sqlalchemy .ext .asyncio import create_async_engine
11
+
11
12
from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy
12
-
13
+
13
14
SQLAlchemyMiddleware , db = create_middleware_and_session_proxy ()
14
15
app = FastAPI ()
15
-
16
+
16
17
# Test commit_on_exit=True
17
18
custom_engine = create_async_engine ("sqlite+aiosqlite://" )
18
19
middleware = SQLAlchemyMiddleware (app , custom_engine = custom_engine , commit_on_exit = True )
19
20
assert middleware .commit_on_exit is True
20
-
21
+
21
22
# Test commit_on_exit=False (default)
22
23
middleware2 = SQLAlchemyMiddleware (app , custom_engine = custom_engine , commit_on_exit = False )
23
24
assert middleware2 .commit_on_exit is False
@@ -26,75 +27,79 @@ def test_commit_on_exit_parameter():
26
27
def test_exception_classes_simple ():
27
28
"""Test exception classes are properly defined"""
28
29
from fastapi_async_sqlalchemy .exceptions import MissingSessionError , SessionNotInitialisedError
29
-
30
+
30
31
# Test exception instantiation without parameters
31
32
missing_error = MissingSessionError ()
32
33
assert isinstance (missing_error , Exception )
33
-
34
+
34
35
init_error = SessionNotInitialisedError ()
35
36
assert isinstance (init_error , Exception )
36
37
37
38
38
39
def test_middleware_properties ():
39
40
"""Test middleware properties and methods"""
40
- from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy
41
- from sqlalchemy .ext .asyncio import create_async_engine
42
41
from fastapi import FastAPI
43
-
42
+ from sqlalchemy .ext .asyncio import create_async_engine
43
+
44
+ from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy
45
+
44
46
SQLAlchemyMiddleware , db = create_middleware_and_session_proxy ()
45
47
app = FastAPI ()
46
-
48
+
47
49
# Test middleware properties
48
50
custom_engine = create_async_engine ("sqlite+aiosqlite://" )
49
- middleware = SQLAlchemyMiddleware (
50
- app ,
51
- custom_engine = custom_engine ,
52
- commit_on_exit = True
53
- )
54
-
55
- assert hasattr (middleware , 'commit_on_exit' )
51
+ middleware = SQLAlchemyMiddleware (app , custom_engine = custom_engine , commit_on_exit = True )
52
+
53
+ assert hasattr (middleware , "commit_on_exit" )
56
54
assert middleware .commit_on_exit is True
57
55
58
56
59
57
def test_basic_imports ():
60
58
"""Test basic imports and module structure"""
61
59
# Test main module imports
62
60
from fastapi_async_sqlalchemy import SQLAlchemyMiddleware , db
61
+
63
62
assert SQLAlchemyMiddleware is not None
64
63
assert db is not None
65
-
64
+
66
65
# Test exception imports
67
66
from fastapi_async_sqlalchemy .exceptions import MissingSessionError , SessionNotInitialisedError
68
- assert MissingSessionError is not None
67
+
68
+ assert MissingSessionError is not None
69
69
assert SessionNotInitialisedError is not None
70
-
70
+
71
71
# Test middleware module imports
72
- from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy , DefaultAsyncSession
72
+ from fastapi_async_sqlalchemy .middleware import (
73
+ DefaultAsyncSession ,
74
+ create_middleware_and_session_proxy ,
75
+ )
76
+
73
77
assert create_middleware_and_session_proxy is not None
74
78
assert DefaultAsyncSession is not None
75
79
76
80
77
81
def test_middleware_factory_different_instances ():
78
82
"""Test creating multiple middleware/db instances"""
79
- from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy
80
83
from fastapi import FastAPI
81
84
from sqlalchemy .ext .asyncio import create_async_engine
82
-
85
+
86
+ from fastapi_async_sqlalchemy .middleware import create_middleware_and_session_proxy
87
+
83
88
# Create first instance
84
89
SQLAlchemyMiddleware1 , db1 = create_middleware_and_session_proxy ()
85
-
90
+
86
91
# Create second instance
87
92
SQLAlchemyMiddleware2 , db2 = create_middleware_and_session_proxy ()
88
-
93
+
89
94
# They should be different instances
90
95
assert SQLAlchemyMiddleware1 is not SQLAlchemyMiddleware2
91
96
assert db1 is not db2
92
-
97
+
93
98
# Test both instances work
94
99
app = FastAPI ()
95
100
engine = create_async_engine ("sqlite+aiosqlite://" )
96
-
101
+
97
102
middleware1 = SQLAlchemyMiddleware1 (app , custom_engine = engine )
98
103
middleware2 = SQLAlchemyMiddleware2 (app , custom_engine = engine )
99
-
100
- assert middleware1 is not middleware2
104
+
105
+ assert middleware1 is not middleware2
0 commit comments