99
1010
1111class BaseDBApiTestSuit :
12+ def _test_isolation_level_read_only (self , connection : dbapi .Connection , isolation_level : str , read_only : bool ):
13+ connection .cursor ().execute (
14+ dbapi .YdbQuery ("CREATE TABLE foo(id Int64 NOT NULL, PRIMARY KEY (id))" , is_ddl = True )
15+ )
16+ connection .set_isolation_level (isolation_level )
17+
18+ cursor = connection .cursor ()
19+
20+ connection .begin ()
21+
22+ query = dbapi .YdbQuery ("UPSERT INTO foo(id) VALUES (1)" )
23+ if read_only :
24+ with pytest .raises (dbapi .DatabaseError ):
25+ cursor .execute (query )
26+ else :
27+ cursor .execute (query )
28+
29+ connection .rollback ()
30+
31+ connection .cursor ().execute (dbapi .YdbQuery ("DROP TABLE foo" , is_ddl = True ))
32+ connection .cursor ().close ()
33+
1234 def _test_connection (self , connection : dbapi .Connection ):
1335 connection .commit ()
1436 connection .rollback ()
@@ -100,14 +122,28 @@ def _test_errors(self, connection: dbapi.Connection):
100122
101123
102124class TestSyncConnection (BaseDBApiTestSuit ):
103- @pytest .fixture ( scope = "class" )
125+ @pytest .fixture
104126 def sync_connection (self ) -> dbapi .Connection :
105127 conn = dbapi .YdbDBApi ().connect (host = "localhost" , port = "2136" , database = "/local" )
106128 try :
107129 yield conn
108130 finally :
109131 conn .close ()
110132
133+ @pytest .mark .parametrize (
134+ "isolation_level, read_only" ,
135+ [
136+ (dbapi .IsolationLevel .SERIALIZABLE , False ),
137+ (dbapi .IsolationLevel .AUTOCOMMIT , False ),
138+ (dbapi .IsolationLevel .ONLINE_READONLY , True ),
139+ (dbapi .IsolationLevel .ONLINE_READONLY_INCONSISTENT , True ),
140+ (dbapi .IsolationLevel .STALE_READONLY , True ),
141+ (dbapi .IsolationLevel .SNAPSHOT_READONLY , True ),
142+ ],
143+ )
144+ def test_isolation_level_read_only (self , isolation_level : str , read_only : bool , sync_connection : dbapi .Connection ):
145+ self ._test_isolation_level_read_only (sync_connection , isolation_level , read_only )
146+
111147 def test_connection (self , sync_connection : dbapi .Connection ):
112148 self ._test_connection (sync_connection )
113149
@@ -118,9 +154,8 @@ def test_errors(self, sync_connection: dbapi.Connection):
118154 return self ._test_errors (sync_connection )
119155
120156
121- @pytest .mark .asyncio (scope = "class" )
122157class TestAsyncConnection (BaseDBApiTestSuit ):
123- @pytest_asyncio .fixture ( scope = "class" )
158+ @pytest_asyncio .fixture
124159 async def async_connection (self ) -> dbapi .AsyncConnection :
125160 def connect ():
126161 return dbapi .YdbDBApi ().async_connect (host = "localhost" , port = "2136" , database = "/local" )
@@ -131,11 +166,31 @@ def connect():
131166 finally :
132167 await util .greenlet_spawn (conn .close )
133168
169+ @pytest .mark .asyncio
170+ @pytest .mark .parametrize (
171+ "isolation_level, read_only" ,
172+ [
173+ (dbapi .IsolationLevel .SERIALIZABLE , False ),
174+ (dbapi .IsolationLevel .AUTOCOMMIT , False ),
175+ (dbapi .IsolationLevel .ONLINE_READONLY , True ),
176+ (dbapi .IsolationLevel .ONLINE_READONLY_INCONSISTENT , True ),
177+ (dbapi .IsolationLevel .STALE_READONLY , True ),
178+ (dbapi .IsolationLevel .SNAPSHOT_READONLY , True ),
179+ ],
180+ )
181+ async def test_isolation_level_read_only (
182+ self , isolation_level : str , read_only : bool , async_connection : dbapi .AsyncConnection
183+ ):
184+ await util .greenlet_spawn (self ._test_isolation_level_read_only , async_connection , isolation_level , read_only )
185+
186+ @pytest .mark .asyncio
134187 async def test_connection (self , async_connection : dbapi .AsyncConnection ):
135188 await util .greenlet_spawn (self ._test_connection , async_connection )
136189
190+ @pytest .mark .asyncio
137191 async def test_cursor_raw_query (self , async_connection : dbapi .AsyncConnection ):
138192 await util .greenlet_spawn (self ._test_cursor_raw_query , async_connection )
139193
194+ @pytest .mark .asyncio
140195 async def test_errors (self , async_connection : dbapi .AsyncConnection ):
141196 await util .greenlet_spawn (self ._test_errors , async_connection )
0 commit comments