Skip to content

Commit 483c784

Browse files
committed
Add docstrings to the tests
1 parent 20a15f9 commit 483c784

12 files changed

Lines changed: 136 additions & 6 deletions

pyproject.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@ max-statements = 95
125125
"T201", # allow print statements
126126
]
127127
"tests/*" = [
128-
"D", # no docstrings necessary here
128+
"D413", # allow plain notes in the module docstrings
129129
"PLC0415", # allow imports in tests functions
130130
"PLR2004", # allow magic values
131131
"S101", # allow assert statements
132132
]
133+
"tests/mock_*.py" = [
134+
# the mock objects only mimic a foreign API, docstrings would add nothing
135+
"D101", # missing docstring in public class
136+
"D102", # missing docstring in public method
137+
"D103", # missing docstring in public function
138+
"D105", # missing docstring in magic method
139+
"D107", # missing docstring in __init__
140+
]
133141

134142
[tool.codespell]
135143
skip = '.git,.tox,.venv,*.de.html,*.de.rst,build,dist,local'

tests/mock_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""This module serves as a mock object for the DB-API 2 module"""
1+
"""Mock object for the DB-API 2 module."""
22

33
import sys
44

tests/mock_pg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""This module serves as a mock object for the pg API module"""
1+
"""Mock object for the pg API module."""
22

33
import sys
44

tests/test_persistent_db.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121

2222

2323
def 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])
3031
def 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])
3739
def 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

5154
def 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

6670
def 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

139144
def 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

156162
def 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

175182
def 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

186194
def 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

239248
def 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

256266
def 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

265276
def 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

270281
def 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

277289
def 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

293306
def 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

305319
def 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

tests/test_persistent_pg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121

2222
def test_version():
23+
"""Check that the module and class versions are in sync."""
2324
from dbutils import __version__, persistent_pg
2425
assert persistent_pg.__version__ == __version__
2526
assert PersistentPg.version == __version__
2627

2728

2829
@pytest.mark.parametrize("closeable", [False, True])
2930
def test_close(closeable):
31+
"""Check that closing is only allowed when the connection is closeable."""
3032
persist = PersistentPg(closeable=closeable)
3133
db = persist.connection()
3234
assert db._con.db
@@ -42,6 +44,7 @@ def test_close(closeable):
4244

4345

4446
def test_threads():
47+
"""Check that every thread keeps its own persistent connection."""
4548
num_threads = 3
4649
persist = PersistentPg()
4750
query_queue, result_queue = [], []
@@ -112,6 +115,7 @@ def run_queries(idx):
112115

113116

114117
def test_maxusage():
118+
"""Check that the connection is reset when used too often."""
115119
persist = PersistentPg(20)
116120
db = persist.connection()
117121
assert db._maxusage == 20
@@ -125,6 +129,7 @@ def test_maxusage():
125129

126130

127131
def test_setsession():
132+
"""Check that the session is prepared after every reopening."""
128133
persist = PersistentPg(3, ('set datestyle',))
129134
db = persist.connection()
130135
assert db._maxusage == 3
@@ -138,6 +143,7 @@ def test_setsession():
138143

139144

140145
def test_failed_transaction():
146+
"""Check that a failed transaction is reported and recovered from."""
141147
persist = PersistentPg()
142148
db = persist.connection()
143149
db._con.close()
@@ -155,6 +161,7 @@ def test_failed_transaction():
155161

156162

157163
def test_context_manager():
164+
"""Check that the connection can be used as a context manager."""
158165
persist = PersistentPg()
159166
with persist.connection() as db:
160167
db.query('select test')

0 commit comments

Comments
 (0)