diff options
author | Seth M Morton <seth.m.morton@gmail.com> | 2018-09-02 22:25:52 -0700 |
---|---|---|
committer | Seth M Morton <seth.m.morton@gmail.com> | 2018-09-02 22:25:52 -0700 |
commit | e64e88c3cb603e4e5819d28472624be2d62bac7b (patch) | |
tree | cd7c34cf78394f3a67be89659a6e8d95506c73fb | |
parent | fa89b4307578a18ee59154c99a45d0b1c68b29e4 (diff) | |
download | natsort-e64e88c3cb603e4e5819d28472624be2d62bac7b.tar.gz |
Refactor test_natsort_cmp.py.
All tests have been put into a class so that only one skipif clause
is needed (no tests are run on Python 3+).
Refactor test that ensures an error is raised to use pytest.raises.
-rw-r--r-- | test_natsort/test_natsort_cmp.py | 131 |
1 files changed, 55 insertions, 76 deletions
diff --git a/test_natsort/test_natsort_cmp.py b/test_natsort/test_natsort_cmp.py index ee860c3..66bcef8 100644 --- a/test_natsort/test_natsort_cmp.py +++ b/test_natsort/test_natsort_cmp.py @@ -4,16 +4,13 @@ Note that these tests are only relevant for Python version < 3. """ -import sys from functools import partial import pytest from hypothesis import given from hypothesis.strategies import floats, integers, lists from natsort import ns -from natsort.compat.py23 import py23_cmp - -PY_VERSION = float(sys.version[:3]) +from natsort.compat.py23 import py23_cmp, PY_VERSION if PY_VERSION < 3: from natsort import natcmp @@ -30,75 +27,57 @@ class Comparable(object): @pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -def test__classes_can_be_compared(): - one = Comparable("1") - two = Comparable("2") - another_two = Comparable("2") - ten = Comparable("10") - - assert ten > two == another_two > one - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -def test__keys_are_being_cached(mocker): - natcmp.cached_keys = {} - assert len(natcmp.cached_keys) == 0 - natcmp(0, 0) - assert len(natcmp.cached_keys) == 1 - natcmp(0, 0) - assert len(natcmp.cached_keys) == 1 - - with mocker.patch("natsort.compat.locale.dumb_sort", return_value=False): - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 2 - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 2 - - with mocker.patch("natsort.compat.locale.dumb_sort", return_value=True): - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 3 - natcmp(0, 0, alg=ns.L) - assert len(natcmp.cached_keys) == 3 - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -def test__illegal_algorithm_raises_error(): - try: - natcmp(0, 0, alg="Just random stuff") - assert False - - except ValueError: - assert True - - except Exception: - assert False - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -def test__classes_can_utilize_max_or_min(): - comparables = [Comparable(i) for i in range(10)] - - assert max(comparables) == comparables[-1] - assert min(comparables) == comparables[0] - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -@given(integers(), integers()) -def test__natcmp_works_the_same_for_integers_as_cmp(x, y): - assert py23_cmp(x, y) == natcmp(x, y) - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -@given(floats(allow_nan=False), floats(allow_nan=False)) -def test__natcmp_works_the_same_for_floats_as_cmp(x, y): - assert py23_cmp(x, y) == natcmp(x, y) - - -@pytest.mark.skipif(PY_VERSION >= 3.0, reason="cmp() deprecated in Python 3") -@given(lists(elements=integers())) -def test_sort_strings_with_numbers(a_list): - strings = [str(var) for var in a_list] - # noinspection PyArgumentList - natcmp_sorted = sorted(strings, cmp=partial(natcmp, alg=ns.SIGNED)) - - assert sorted(a_list) == [int(var) for var in natcmp_sorted] +class TestNatCmp: + + def test_classes_can_be_compared(self): + one = Comparable("1") + two = Comparable("2") + another_two = Comparable("2") + ten = Comparable("10") + assert ten > two == another_two > one + + def test_keys_are_being_cached(self, mocker): + natcmp.cached_keys = {} + assert len(natcmp.cached_keys) == 0 + natcmp(0, 0) + assert len(natcmp.cached_keys) == 1 + natcmp(0, 0) + assert len(natcmp.cached_keys) == 1 + + with mocker.patch("natsort.compat.locale.dumb_sort", return_value=False): + natcmp(0, 0, alg=ns.L) + assert len(natcmp.cached_keys) == 2 + natcmp(0, 0, alg=ns.L) + assert len(natcmp.cached_keys) == 2 + + with mocker.patch("natsort.compat.locale.dumb_sort", return_value=True): + natcmp(0, 0, alg=ns.L) + assert len(natcmp.cached_keys) == 3 + natcmp(0, 0, alg=ns.L) + assert len(natcmp.cached_keys) == 3 + + def test_illegal_algorithm_raises_error(self): + with pytest.raises(ValueError): + natcmp(0, 0, alg="Just random stuff") + + def test_classes_can_utilize_max_or_min(self): + comparables = [Comparable(i) for i in range(10)] + + assert max(comparables) == comparables[-1] + assert min(comparables) == comparables[0] + + @given(integers(), integers()) + def test_natcmp_works_the_same_for_integers_as_cmp(self, x, y): + assert py23_cmp(x, y) == natcmp(x, y) + + @given(floats(allow_nan=False), floats(allow_nan=False)) + def test_natcmp_works_the_same_for_floats_as_cmp(self, x, y): + assert py23_cmp(x, y) == natcmp(x, y) + + @given(lists(elements=integers())) + def test_sort_strings_with_numbers(self, a_list): + strings = [str(var) for var in a_list] + # noinspection PyArgumentList + natcmp_sorted = sorted(strings, cmp=partial(natcmp, alg=ns.SIGNED)) + + assert sorted(a_list) == [int(var) for var in natcmp_sorted] |