Skip to content

Commit 521c483

Browse files
committed
Clean up tests and imports
1 parent 483c784 commit 521c483

10 files changed

Lines changed: 70 additions & 70 deletions

‎dbutils/steady_db.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@
9494

9595
from . import __version__
9696

97+
__all__ = [
98+
'connect', 'SteadyDBConnection', 'SteadyDBCursor',
99+
'SteadyDBError', 'InvalidCursorError',
100+
]
101+
97102

98103
class SteadyDBError(Exception):
99104
"""General SteadyDB error."""

‎dbutils/steady_pg.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@
7575

7676
from . import __version__
7777

78+
__all__ = [
79+
'SteadyPgConnection', 'SteadyPgError', 'InvalidConnectionError',
80+
]
81+
7882

7983
class SteadyPgError(Exception):
8084
"""General SteadyPg error."""

‎tests/__init__.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""The DBUtils tests package."""
22

3-
# make sure the mock pg module is installed
4-
from . import mock_pg as pg # noqa: F401
3+
import sys
4+
5+
from . import mock_pg
6+
7+
# make the mock pg module importable as "pg"
8+
sys.modules['pg'] = mock_pg

‎tests/conftest.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""Fixtures for the DBUtils tests."""
2+
3+
import pytest
4+
5+
from . import mock_db
6+
7+
8+
@pytest.fixture
9+
def dbapi():
10+
"""Get the mock DB-API 2 module."""
11+
mock_db.threadsafety = 2
12+
return mock_db

‎tests/mock_db.py‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
"""Mock object for the DB-API 2 module."""
22

3-
import sys
4-
5-
import pytest
6-
7-
__all__ = ['dbapi']
8-
9-
103
threadsafety = 2
114

125

13-
@pytest.fixture
14-
def dbapi():
15-
"""Get mock DB API 2 module."""
16-
mock_db = sys.modules[__name__]
17-
mock_db.threadsafety = 2
18-
return mock_db
19-
20-
216
class Error(Exception):
227
pass
238

‎tests/mock_pg.py‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
"""Mock object for the pg API module."""
22

3-
import sys
4-
5-
sys.modules['pg'] = sys.modules[__name__]
6-
73

84
class Error(Exception):
95
pass

‎tests/test_persistent_db.py‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
from dbutils.persistent_db import NotSupportedError, PersistentDB, local
1919

20-
from .mock_db import dbapi # noqa: F401
21-
2220

2321
def test_version():
2422
"""Check that the module and class versions are in sync."""
@@ -28,15 +26,15 @@ def test_version():
2826

2927

3028
@pytest.mark.parametrize("threadsafety", [None, 0])
31-
def test_no_threadsafety(dbapi, threadsafety): # noqa: F811
29+
def test_no_threadsafety(dbapi, threadsafety):
3230
"""Check that a database module that is not thread-safe is rejected."""
3331
dbapi.threadsafety = threadsafety
3432
with pytest.raises(NotSupportedError):
3533
PersistentDB(dbapi)
3634

3735

3836
@pytest.mark.parametrize("closeable", [False, True])
39-
def test_close(dbapi, closeable): # noqa: F811
37+
def test_close(dbapi, closeable):
4038
"""Check that closing is only allowed when the connection is closeable."""
4139
persist = PersistentDB(dbapi, closeable=closeable)
4240
db = persist.connection()
@@ -51,7 +49,7 @@ def test_close(dbapi, closeable): # noqa: F811
5149
assert db._con.valid is False
5250

5351

54-
def test_connection(dbapi): # noqa: F811
52+
def test_connection(dbapi):
5553
"""Check that the same thread always gets the same connection."""
5654
persist = PersistentDB(dbapi)
5755
db = persist.connection()
@@ -67,7 +65,7 @@ def test_connection(dbapi): # noqa: F811
6765
db.close()
6866

6967

70-
def test_threads(dbapi): # noqa: F811
68+
def test_threads(dbapi):
7169
"""Check that every thread keeps its own persistent connection."""
7270
num_threads = 3
7371
persist = PersistentDB(dbapi, closeable=True)
@@ -141,7 +139,7 @@ def run_queries(idx):
141139
query_queue[i].put(None, timeout=1)
142140

143141

144-
def test_maxusage(dbapi): # noqa: F811
142+
def test_maxusage(dbapi):
145143
"""Check that the connection is reset when used too often."""
146144
persist = PersistentDB(dbapi, 20)
147145
db = persist.connection()
@@ -159,7 +157,7 @@ def test_maxusage(dbapi): # noqa: F811
159157
assert db._con.num_queries == j
160158

161159

162-
def test_setsession(dbapi): # noqa: F811
160+
def test_setsession(dbapi):
163161
"""Check that the session is prepared after every reopening."""
164162
persist = PersistentDB(dbapi, 3, ('set datestyle',))
165163
db = persist.connection()
@@ -179,7 +177,7 @@ def test_setsession(dbapi): # noqa: F811
179177
assert db._con.session == ['datestyle']
180178

181179

182-
def test_threadlocal(dbapi): # noqa: F811
180+
def test_threadlocal(dbapi):
183181
"""Check that the class for thread-local data can be replaced."""
184182
persist = PersistentDB(dbapi)
185183
assert isinstance(persist.thread, local)
@@ -191,7 +189,7 @@ class Threadlocal:
191189
assert isinstance(persist.thread, Threadlocal)
192190

193191

194-
def test_ping_check(dbapi): # noqa: F811
192+
def test_ping_check(dbapi):
195193
"""Check that connections are pinged as configured."""
196194
con_cls = dbapi.Connection
197195
con_cls.has_ping = True
@@ -245,7 +243,7 @@ def test_ping_check(dbapi): # noqa: F811
245243
con_cls.num_pings = 0
246244

247245

248-
def test_failed_transaction(dbapi): # noqa: F811
246+
def test_failed_transaction(dbapi):
249247
"""Check that a failed transaction is reported and recovered from."""
250248
persist = PersistentDB(dbapi)
251249
db = persist.connection()
@@ -263,7 +261,7 @@ def test_failed_transaction(dbapi): # noqa: F811
263261
cursor.execute('select test')
264262

265263

266-
def test_context_manager(dbapi): # noqa: F811
264+
def test_context_manager(dbapi):
267265
"""Check that connection and cursor can be used as context managers."""
268266
persist = PersistentDB(dbapi)
269267
with persist.connection() as db:
@@ -278,15 +276,15 @@ def timeout_is_not_fatal(error):
278276
return not error.args or error.args[0] != 3024
279277

280278

281-
def test_isfatal_default(dbapi): # noqa: F811
279+
def test_isfatal_default(dbapi):
282280
"""Check that no error check is configured by default."""
283281
persist = PersistentDB(dbapi)
284282
assert persist._isfatal is None
285283
db = persist.connection()
286284
assert db._isfatal is None
287285

288286

289-
def test_isfatal(dbapi): # noqa: F811
287+
def test_isfatal(dbapi):
290288
"""Check that isfatal is passed on and can veto the failover."""
291289
persist = PersistentDB(dbapi, isfatal=timeout_is_not_fatal)
292290
assert persist._isfatal is timeout_is_not_fatal
@@ -303,7 +301,7 @@ def test_isfatal(dbapi): # noqa: F811
303301
assert cursor.fetchone() == 'test'
304302

305303

306-
def test_no_failover(dbapi): # noqa: F811
304+
def test_no_failover(dbapi):
307305
"""Check that no_failover() suspends the failover mechanism."""
308306
persist = PersistentDB(dbapi)
309307
db = persist.connection()
@@ -316,7 +314,7 @@ def test_no_failover(dbapi): # noqa: F811
316314
assert cursor.fetchone() == 'test'
317315

318316

319-
def test_dbapi_connection(dbapi): # noqa: F811
317+
def test_dbapi_connection(dbapi):
320318
"""Check that the underlying DB-API 2 objects are accessible."""
321319
persist = PersistentDB(dbapi)
322320
db = persist.connection()

0 commit comments

Comments
 (0)