2121
2222
2323def test_version ():
24+ """Check that the module and class versions are in sync."""
2425 from dbutils import __version__ , persistent_db
2526 assert persistent_db .__version__ == __version__
2627 assert PersistentDB .version == __version__
2728
2829
2930@pytest .mark .parametrize ("threadsafety" , [None , 0 ])
3031def test_no_threadsafety (dbapi , threadsafety ): # noqa: F811
32+ """Check that a database module that is not thread-safe is rejected."""
3133 dbapi .threadsafety = threadsafety
3234 with pytest .raises (NotSupportedError ):
3335 PersistentDB (dbapi )
3436
3537
3638@pytest .mark .parametrize ("closeable" , [False , True ])
3739def test_close (dbapi , closeable ): # noqa: F811
40+ """Check that closing is only allowed when the connection is closeable."""
3841 persist = PersistentDB (dbapi , closeable = closeable )
3942 db = persist .connection ()
4043 assert db ._con .valid is True
@@ -49,6 +52,7 @@ def test_close(dbapi, closeable): # noqa: F811
4952
5053
5154def test_connection (dbapi ): # noqa: F811
55+ """Check that the same thread always gets the same connection."""
5256 persist = PersistentDB (dbapi )
5357 db = persist .connection ()
5458 db_con = db ._con
@@ -64,6 +68,7 @@ def test_connection(dbapi): # noqa: F811
6468
6569
6670def test_threads (dbapi ): # noqa: F811
71+ """Check that every thread keeps its own persistent connection."""
6772 num_threads = 3
6873 persist = PersistentDB (dbapi , closeable = True )
6974 query_queue , result_queue = [], []
@@ -137,6 +142,7 @@ def run_queries(idx):
137142
138143
139144def test_maxusage (dbapi ): # noqa: F811
145+ """Check that the connection is reset when used too often."""
140146 persist = PersistentDB (dbapi , 20 )
141147 db = persist .connection ()
142148 assert db ._maxusage == 20
@@ -154,6 +160,7 @@ def test_maxusage(dbapi): # noqa: F811
154160
155161
156162def test_setsession (dbapi ): # noqa: F811
163+ """Check that the session is prepared after every reopening."""
157164 persist = PersistentDB (dbapi , 3 , ('set datestyle' ,))
158165 db = persist .connection ()
159166 assert db ._maxusage == 3
@@ -173,6 +180,7 @@ def test_setsession(dbapi): # noqa: F811
173180
174181
175182def test_threadlocal (dbapi ): # noqa: F811
183+ """Check that the class for thread-local data can be replaced."""
176184 persist = PersistentDB (dbapi )
177185 assert isinstance (persist .thread , local )
178186
@@ -184,6 +192,7 @@ class Threadlocal:
184192
185193
186194def test_ping_check (dbapi ): # noqa: F811
195+ """Check that connections are pinged as configured."""
187196 con_cls = dbapi .Connection
188197 con_cls .has_ping = True
189198 con_cls .num_pings = 0
@@ -237,6 +246,7 @@ def test_ping_check(dbapi): # noqa: F811
237246
238247
239248def test_failed_transaction (dbapi ): # noqa: F811
249+ """Check that a failed transaction is reported and recovered from."""
240250 persist = PersistentDB (dbapi )
241251 db = persist .connection ()
242252 cursor = db .cursor ()
@@ -254,6 +264,7 @@ def test_failed_transaction(dbapi): # noqa: F811
254264
255265
256266def test_context_manager (dbapi ): # noqa: F811
267+ """Check that connection and cursor can be used as context managers."""
257268 persist = PersistentDB (dbapi )
258269 with persist .connection () as db :
259270 with db .cursor () as cursor :
@@ -263,18 +274,20 @@ def test_context_manager(dbapi): # noqa: F811
263274
264275
265276def timeout_is_not_fatal (error ):
266- """A deliberate server side timeout does not break the connection ."""
277+ """Treat a deliberate server side timeout as not fatal ."""
267278 return not error .args or error .args [0 ] != 3024
268279
269280
270281def test_isfatal_default (dbapi ): # noqa: F811
282+ """Check that no error check is configured by default."""
271283 persist = PersistentDB (dbapi )
272284 assert persist ._isfatal is None
273285 db = persist .connection ()
274286 assert db ._isfatal is None
275287
276288
277289def test_isfatal (dbapi ): # noqa: F811
290+ """Check that isfatal is passed on and can veto the failover."""
278291 persist = PersistentDB (dbapi , isfatal = timeout_is_not_fatal )
279292 assert persist ._isfatal is timeout_is_not_fatal
280293 db = persist .connection ()
@@ -291,6 +304,7 @@ def test_isfatal(dbapi): # noqa: F811
291304
292305
293306def test_no_failover (dbapi ): # noqa: F811
307+ """Check that no_failover() suspends the failover mechanism."""
294308 persist = PersistentDB (dbapi )
295309 db = persist .connection ()
296310 dbapi .Connection .num_timeouts = 0
@@ -303,6 +317,7 @@ def test_no_failover(dbapi): # noqa: F811
303317
304318
305319def test_dbapi_connection (dbapi ): # noqa: F811
320+ """Check that the underlying DB-API 2 objects are accessible."""
306321 persist = PersistentDB (dbapi )
307322 db = persist .connection ()
308323 con = db .dbapi_connection
0 commit comments