Skip to content

Commit 7c543d7

Browse files
committed
decouple tests from unittest.TestCase base class
use a regular pytest naming convention and create a standard fixtures package Change-Id: I9616ec468f433649ee0de0b2475b9133d5a7873d
1 parent 4babaff commit 7c543d7

21 files changed

+198
-212
lines changed

dogpile/testing/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .assertions import assert_raises_message as assert_raises_message
2+
from .assertions import eq_ as eq_
3+
from .assertions import is_ as is_
4+
from .assertions import ne_ as ne_
5+
from .assertions import winsleep as winsleep

dogpile/testing/assertions.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import re
2+
import sys
3+
import time
4+
5+
6+
def eq_(a, b, msg=None):
7+
"""Assert a == b, with repr messaging on failure."""
8+
assert a == b, msg or "%r != %r" % (a, b)
9+
10+
11+
def is_(a, b, msg=None):
12+
"""Assert a is b, with repr messaging on failure."""
13+
assert a is b, msg or "%r is not %r" % (a, b)
14+
15+
16+
def ne_(a, b, msg=None):
17+
"""Assert a != b, with repr messaging on failure."""
18+
assert a != b, msg or "%r == %r" % (a, b)
19+
20+
21+
def assert_raises_message(except_cls, msg, callable_, *args, **kwargs):
22+
try:
23+
callable_(*args, **kwargs)
24+
assert False, "Callable did not raise an exception"
25+
except except_cls as e:
26+
assert re.search(msg, str(e)), "%r !~ %s" % (msg, e)
27+
28+
29+
def winsleep():
30+
# sleep a for an amount of time
31+
# sufficient for windows time.time()
32+
# to change
33+
if sys.platform.startswith("win"):
34+
time.sleep(0.001)
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# mypy: ignore-errors
2+
13
import collections
24
import itertools
35
import json
46
import random
57
from threading import Lock
68
from threading import Thread
79
import time
8-
from unittest import TestCase
910
import uuid
1011

1112
import pytest
@@ -17,15 +18,15 @@
1718
from dogpile.cache.api import CantDeserializeException
1819
from dogpile.cache.api import NO_VALUE
1920
from dogpile.cache.region import _backend_loader
20-
from . import assert_raises_message
21-
from . import eq_
21+
from .assertions import assert_raises_message
22+
from .assertions import eq_
2223

2324

2425
def gen_some_key():
2526
return f"some_key_{random.randint(1, 100000)}"
2627

2728

28-
class _GenericBackendFixture(object):
29+
class _GenericBackendFixture:
2930
@classmethod
3031
def setup_class(cls):
3132
backend_cls = _backend_loader.load(cls.backend)
@@ -36,7 +37,7 @@ def setup_class(cls):
3637
pytest.skip("Backend %s not installed" % cls.backend)
3738
cls._check_backend_available(backend)
3839

39-
def tearDown(self):
40+
def teardown_method(self, method):
4041
some_key = gen_some_key()
4142
if self._region_inst:
4243
for key in self._keys:
@@ -96,7 +97,7 @@ def _backend(self):
9697
return self._backend_inst
9798

9899

99-
class _GenericBackendTest(_GenericBackendFixture, TestCase):
100+
class _GenericBackendTestSuite(_GenericBackendFixture):
100101
def test_backend_get_nothing(self):
101102
backend = self._backend()
102103
some_key = gen_some_key()
@@ -402,7 +403,7 @@ def raise_cant_deserialize_exception(v):
402403
raise CantDeserializeException()
403404

404405

405-
class _GenericSerializerTest(TestCase):
406+
class _GenericSerializerTestSuite:
406407
# Inheriting from this class will make test cases
407408
# use these serialization arguments
408409
region_args = {
@@ -452,7 +453,7 @@ def test_uses_deserializer(self):
452453
# TODO: test set_multi, get_multi
453454

454455

455-
class _GenericMutexTest(_GenericBackendFixture, TestCase):
456+
class _GenericMutexTestSuite(_GenericBackendFixture):
456457
def test_mutex(self):
457458
backend = self._backend()
458459
mutex = backend.get_mutex("foo")

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ target-version = ['py38']
6969
[tool.pytest.ini_options]
7070
addopts = "--tb native -v -r fxX -p no:logging -p no:warnings -m 'not time_intensive'"
7171
python_files = "tests/*test_*.py"
72+
python_classes = "*Test"
73+
7274
filterwarnings = [
7375
"error"
7476
]

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ exclude = .venv,.git,.tox,dist,docs/*,*egg,build
1515
import-order-style = google
1616
application-import-names = dogpile,tests
1717
per-file-ignores =
18+
**/__init__.py:F401
1819
tests/*:FA100
1920

2021

tests/cache/__init__.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +0,0 @@
1-
import re
2-
import sys
3-
import time
4-
5-
6-
def eq_(a, b, msg=None):
7-
"""Assert a == b, with repr messaging on failure."""
8-
assert a == b, msg or "%r != %r" % (a, b)
9-
10-
11-
def is_(a, b, msg=None):
12-
"""Assert a is b, with repr messaging on failure."""
13-
assert a is b, msg or "%r is not %r" % (a, b)
14-
15-
16-
def ne_(a, b, msg=None):
17-
"""Assert a != b, with repr messaging on failure."""
18-
assert a != b, msg or "%r == %r" % (a, b)
19-
20-
21-
def assert_raises_message(except_cls, msg, callable_, *args, **kwargs):
22-
try:
23-
callable_(*args, **kwargs)
24-
assert False, "Callable did not raise an exception"
25-
except except_cls as e:
26-
assert re.search(msg, str(e)), "%r !~ %s" % (msg, e)
27-
28-
29-
def winsleep():
30-
# sleep a for an amount of time
31-
# sufficient for windows time.time()
32-
# to change
33-
if sys.platform.startswith("win"):
34-
time.sleep(0.001)

tests/cache/plugins/test_mako_cache.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from unittest import mock
2-
from unittest import TestCase
32

43
from mako.cache import register_plugin
54
from mako.template import Template
65
import pytest
76

8-
from .. import eq_
7+
from dogpile.testing import eq_
98

109

1110
try:
@@ -18,7 +17,7 @@
1817
)
1918

2019

21-
class TestMakoPlugin(TestCase):
20+
class MakoPluginTest:
2221
def _mock_fixture(self):
2322
reg = mock.MagicMock()
2423
reg.get_or_create.return_value = "hello world"

tests/cache/test_dbm_backend.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
from dogpile.cache.backends.file import AbstractFileLock
55
from dogpile.cache.proxy import ProxyBackend
6+
from dogpile.testing import assert_raises_message
7+
from dogpile.testing.fixtures import _GenericBackendTestSuite
8+
from dogpile.testing.fixtures import _GenericMutexTestSuite
9+
from dogpile.testing.fixtures import _GenericSerializerTestSuite
610
from dogpile.util.readwrite_lock import ReadWriteMutex
7-
from . import assert_raises_message
8-
from ._fixtures import _GenericBackendTest
9-
from ._fixtures import _GenericMutexTest
10-
from ._fixtures import _GenericSerializerTest
1111

1212
try:
1313
import fcntl # noqa
@@ -40,21 +40,21 @@ def release_write_lock(self):
4040

4141
if has_fcntl:
4242

43-
class DBMBackendTest(_GenericBackendTest):
43+
class DBMBackendTest(_GenericBackendTestSuite):
4444
backend = "dogpile.cache.dbm"
4545

4646
config_args = {"arguments": {"filename": test_fname}}
4747

4848

49-
class DBMBackendConditionTest(_GenericBackendTest):
49+
class DBMBackendConditionTest(_GenericBackendTestSuite):
5050
backend = "dogpile.cache.dbm"
5151

5252
config_args = {
5353
"arguments": {"filename": test_fname, "lock_factory": MutexLock}
5454
}
5555

5656

57-
class DBMBackendProxyTest(_GenericBackendTest):
57+
class DBMBackendProxyTest(_GenericBackendTestSuite):
5858
backend = "dogpile.cache.dbm"
5959

6060
config_args = {
@@ -64,12 +64,12 @@ class DBMBackendProxyTest(_GenericBackendTest):
6464

6565

6666
class DBMBackendSerializerTest(
67-
_GenericSerializerTest, DBMBackendConditionTest
67+
_GenericSerializerTestSuite, DBMBackendConditionTest
6868
):
6969
pass
7070

7171

72-
class DBMBackendNoLockTest(_GenericBackendTest):
72+
class DBMBackendNoLockTest(_GenericBackendTestSuite):
7373
backend = "dogpile.cache.dbm"
7474

7575
config_args = {
@@ -81,7 +81,7 @@ class DBMBackendNoLockTest(_GenericBackendTest):
8181
}
8282

8383

84-
class _DBMMutexTest(_GenericMutexTest):
84+
class _DBMMutexTestSuite(_GenericMutexTestSuite):
8585
backend = "dogpile.cache.dbm"
8686

8787
def test_release_assertion_thread(self):
@@ -107,11 +107,11 @@ def test_release_assertion_key(self):
107107

108108
if has_fcntl:
109109

110-
class DBMMutexFileTest(_DBMMutexTest):
110+
class DBMMutexFileTest(_DBMMutexTestSuite):
111111
config_args = {"arguments": {"filename": test_fname}}
112112

113113

114-
class DBMMutexConditionTest(_DBMMutexTest):
114+
class DBMMutexConditionTest(_DBMMutexTestSuite):
115115
config_args = {
116116
"arguments": {"filename": test_fname, "lock_factory": MutexLock}
117117
}

tests/cache/test_decorator.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import itertools
44
import time
5-
from unittest import TestCase
65

76
from dogpile.cache import util
87
from dogpile.cache.api import NO_VALUE
8+
from dogpile.testing import assert_raises_message
9+
from dogpile.testing import eq_
10+
from dogpile.testing import winsleep
11+
from dogpile.testing.fixtures import _GenericBackendFixture
912
from dogpile.util import compat
10-
from . import eq_
11-
from . import winsleep
12-
from ._fixtures import _GenericBackendFixture
1313

1414

15-
class DecoratorTest(_GenericBackendFixture, TestCase):
15+
class DecoratorTest(_GenericBackendFixture):
1616
backend = "dogpile.cache.memory"
1717

1818
def _fixture(
@@ -189,7 +189,7 @@ def generate_key_with_reversed_order(*args):
189189
eq_(go.get(3, 1), ["1 2", "2 1"])
190190

191191

192-
class KeyGenerationTest(TestCase):
192+
class KeyGenerationTest:
193193
def _keygen_decorator(self, namespace=None, **kw):
194194
canary = []
195195

@@ -229,7 +229,14 @@ def one(a, b):
229229
pass
230230

231231
gen = canary[0]
232-
self.assertRaises(ValueError, gen, 1, b=2)
232+
assert_raises_message(
233+
ValueError,
234+
"dogpile.cache's default key creation function "
235+
"does not accept keyword arguments",
236+
gen,
237+
1,
238+
b=2,
239+
)
233240

234241
def test_kwarg_kegen_keygen_fn(self):
235242
decorate, canary = self._kwarg_keygen_decorator()
@@ -471,7 +478,7 @@ def test_sha1_key_mangler_bytes_py3k(self):
471478
)
472479

473480

474-
class CacheDecoratorTest(_GenericBackendFixture, TestCase):
481+
class CacheDecoratorTest(_GenericBackendFixture):
475482
backend = "mock"
476483

477484
def test_cache_arg(self):
@@ -656,7 +663,7 @@ def func(a, b, c=True, *args, **kwargs):
656663
cached_func = reg.cache_on_arguments()(func)
657664
cached_signature = compat.inspect_getargspec(cached_func)
658665

659-
self.assertEqual(signature, cached_signature)
666+
eq_(signature, cached_signature)
660667

661668
def test_cache_multi_preserve_sig(self):
662669
reg = self._region()
@@ -668,4 +675,4 @@ def func(a, b, c=True, *args, **kwargs):
668675
cached_func = reg.cache_multi_on_arguments()(func)
669676
cached_signature = compat.inspect_getargspec(cached_func)
670677

671-
self.assertEqual(signature, cached_signature)
678+
eq_(signature, cached_signature)

tests/cache/test_mako.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
from unittest import TestCase
2-
3-
4-
class MakoTest(TestCase):
1+
class MakoTest:
52

63
"""Test entry point for Mako"""
74

0 commit comments

Comments
 (0)