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