Skip to content

Commit

Permalink
Move mocked_sleep fixture to tests/conftest.py and rename to mock_sle…
Browse files Browse the repository at this point in the history
…ep. Rename tests/fixtures.py to helpers.py.
  • Loading branch information
dgilland committed Sep 23, 2022
1 parent 0daba54 commit 96f6e48
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 55 deletions.
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from unittest import mock

import pytest


@pytest.fixture
def mock_sleep():
with mock.patch("time.sleep") as mocked:
yield mocked
File renamed without changes.
32 changes: 16 additions & 16 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pydash as _

from . import fixtures
from . import helpers


parametrize = pytest.mark.parametrize
Expand Down Expand Up @@ -140,7 +140,7 @@ def test_find(case, expected):


def test_find_class_object():
obj = fixtures.Object(a=1, b=2)
obj = helpers.Object(a=1, b=2)
assert _.find([None, {}, obj], {"b": 2}) == obj


Expand All @@ -152,7 +152,7 @@ def test_find_namedtuple():

@parametrize(
"case,expected",
[(({"abc": 1, "xyz": 2, "c": 3}.values(), fixtures.Filter(lambda x: x < 2)), 1)],
[(({"abc": 1, "xyz": 2, "c": 3}.values(), helpers.Filter(lambda x: x < 2)), 1)],
)
def test_find_using_callable_class(case, expected):
assert _.find(*case) == expected
Expand Down Expand Up @@ -215,9 +215,9 @@ def test_flat_map_depth(case, expected):
@parametrize(
"case,expected",
[
(([1, 2, 3], fixtures.noop), [1, 2, 3]),
(([1, 2, 3], helpers.noop), [1, 2, 3]),
(([1, 2, 3], lambda value: value < 2), [1, 2, 3]),
(({"one": 1, "two": 2, "three": 3}, fixtures.noop), {"one": 1, "two": 2, "three": 3}),
(({"one": 1, "two": 2, "three": 3}, helpers.noop), {"one": 1, "two": 2, "three": 3}),
],
)
def test_for_each(case, expected):
Expand All @@ -227,9 +227,9 @@ def test_for_each(case, expected):
@parametrize(
"case,expected",
[
(([1, 2, 3], fixtures.noop), [1, 2, 3]),
(([1, 2, 3], helpers.noop), [1, 2, 3]),
(([1, 2, 3], lambda value: value < 2), [1, 2, 3]),
(({"one": 1, "two": 2, "three": 3}, fixtures.noop), {"one": 1, "two": 2, "three": 3}),
(({"one": 1, "two": 2, "three": 3}, helpers.noop), {"one": 1, "two": 2, "three": 3}),
],
)
def test_for_each_right(case, expected):
Expand Down Expand Up @@ -783,8 +783,8 @@ def test_pluck(case, expected):
"case,expected",
[
(([1, 2, 3], None), 1),
(([1, 2, 3], fixtures.reduce_iteratee0), 6),
(({"a": 1, "b": 2, "c": 3}, fixtures.reduce_iteratee1, {}), {"a": 3, "b": 6, "c": 9}),
(([1, 2, 3], helpers.reduce_iteratee0), 6),
(({"a": 1, "b": 2, "c": 3}, helpers.reduce_iteratee1, {}), {"a": 3, "b": 6, "c": 9}),
],
)
def test_reduce_(case, expected):
Expand All @@ -807,9 +807,9 @@ def test_reduce_raise(case, exception):
"case,expected",
[
(([1, 2, 3], None), 3),
(([1, 2, 3], fixtures.reduce_iteratee0), 6),
(([[0, 1], [2, 3], [4, 5]], fixtures.reduce_right_iteratee0), [4, 5, 2, 3, 0, 1]),
(({"a": 1, "b": 2, "c": 3}, fixtures.reduce_iteratee1, {}), {"a": 3, "b": 6, "c": 9}),
(([1, 2, 3], helpers.reduce_iteratee0), 6),
(([[0, 1], [2, 3], [4, 5]], helpers.reduce_right_iteratee0), [4, 5, 2, 3, 0, 1]),
(({"a": 1, "b": 2, "c": 3}, helpers.reduce_iteratee1, {}), {"a": 3, "b": 6, "c": 9}),
],
)
def test_reduce_right(case, expected):
Expand All @@ -832,8 +832,8 @@ def test_reduce_right_exception(case, exception):
"case,expected",
[
(([1, 2, 3], None), [1, 1]),
(([1, 2, 3], fixtures.reduce_iteratee0), [3, 6]),
(([1, 2, 3, 4, 5], fixtures.reduce_iteratee0, 0), [1, 3, 6, 10, 15]),
(([1, 2, 3], helpers.reduce_iteratee0), [3, 6]),
(([1, 2, 3, 4, 5], helpers.reduce_iteratee0, 0), [1, 3, 6, 10, 15]),
],
)
def test_reductions(case, expected):
Expand All @@ -844,9 +844,9 @@ def test_reductions(case, expected):
"case,expected",
[
(([1, 2, 3], None), [3, 3]),
(([1, 2, 3], fixtures.reduce_iteratee0), [5, 6]),
(([1, 2, 3], helpers.reduce_iteratee0), [5, 6]),
(
([[0, 1], [2, 3], [4, 5]], fixtures.reduce_right_iteratee0),
([[0, 1], [2, 3], [4, 5]], helpers.reduce_right_iteratee0),
[[4, 5, 2, 3], [4, 5, 2, 3, 0, 1]],
),
],
Expand Down
6 changes: 0 additions & 6 deletions tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
parametrize = pytest.mark.parametrize


@pytest.fixture
def mocked_sleep():
with mock.patch("time.sleep") as mocked:
yield mocked


@parametrize(
"case,expected",
[
Expand Down
28 changes: 14 additions & 14 deletions tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pydash as _

from . import fixtures
from . import helpers


parametrize = pytest.mark.parametrize
Expand Down Expand Up @@ -288,14 +288,14 @@ def test_find_last_key(case, expected):
"case,expected",
[
(
({"name": "fred", "employer": "slate"}, fixtures.for_in_iteratee0),
({"name": "fred", "employer": "slate"}, helpers.for_in_iteratee0),
({"name": "fredfred", "employer": "slateslate"},),
),
(
({"name": "fred", "employer": "slate"}, fixtures.for_in_iteratee1),
({"name": "fred", "employer": "slate"}, helpers.for_in_iteratee1),
({"name": "fredfred", "employer": "slate"}, {"name": "fred", "employer": "slateslate"}),
),
(([1, 2, 3], fixtures.for_in_iteratee2), ([False, True, 3],)),
(([1, 2, 3], helpers.for_in_iteratee2), ([False, True, 3],)),
],
)
def test_for_in(case, expected):
Expand All @@ -306,14 +306,14 @@ def test_for_in(case, expected):
"case,expected",
[
(
({"name": "fred", "employer": "slate"}, fixtures.for_in_iteratee0),
({"name": "fred", "employer": "slate"}, helpers.for_in_iteratee0),
({"name": "fredfred", "employer": "slateslate"},),
),
(
({"name": "fred", "employer": "slate"}, fixtures.for_in_iteratee1),
({"name": "fred", "employer": "slate"}, helpers.for_in_iteratee1),
({"name": "fredfred", "employer": "slate"}, {"name": "fred", "employer": "slateslate"}),
),
(([1, 2, 3], fixtures.for_in_iteratee2), ([1, True, "index:2"],)),
(([1, 2, 3], helpers.for_in_iteratee2), ([1, True, "index:2"],)),
],
)
def test_for_in_right(case, expected):
Expand Down Expand Up @@ -644,9 +644,9 @@ def test_parse_int(case, expected):
(({"a": 1, "b": 2, "c": 3}, ["a"], ["b"]), {"a": 1, "b": 2}),
(([1, 2, 3],), {}),
(([1, 2, 3], 0), {0: 1}),
((fixtures.Object(a=1, b=2, c=3), "a"), {"a": 1}),
((fixtures.ItemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((fixtures.IteritemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((helpers.Object(a=1, b=2, c=3), "a"), {"a": 1}),
((helpers.ItemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((helpers.IteritemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
(({"a": {"b": 1, "c": 2, "d": 3}}, "a.b", "a.d"), {"a": {"b": 1, "d": 3}}),
(
({"a": [{"b": 1}, {"c": 2}, {"d": 3}]}, "a[0]", "a[2]"),
Expand All @@ -665,9 +665,9 @@ def test_pick(case, expected):
(({"a": 1, "b": 2, "c": 3}, lambda value, key: key in ["a"]), {"a": 1}),
(([1, 2, 3],), {0: 1, 1: 2, 2: 3}),
(([1, 2, 3], [0]), {0: 1}),
((fixtures.Object(a=1, b=2, c=3), "a"), {"a": 1}),
((fixtures.ItemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((fixtures.IteritemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((helpers.Object(a=1, b=2, c=3), "a"), {"a": 1}),
((helpers.ItemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
((helpers.IteritemsObject({"a": 1, "b": 2, "c": 3}), "a"), {"a": 1}),
],
)
def test_pick_by(case, expected):
Expand Down Expand Up @@ -824,7 +824,7 @@ def test_to_string(case, expected):
([1, 2, 3, 4, 5], lambda acc, value, key: acc.append((key, value))),
[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)],
),
(([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], fixtures.transform_iteratee0), [1, 9, 25]),
(([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], helpers.transform_iteratee0), [1, 9, 25]),
(([1, 2, 3, 4, 5],), []),
],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_predicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pydash as _

from . import fixtures
from . import helpers


parametrize = pytest.mark.parametrize
Expand Down Expand Up @@ -250,7 +250,7 @@ def test_is_equal(case, expected):
(([1, 2], [1, 2, 3], None), False),
(([1, 2, 3], [1, 2], lambda a, b: None if isinstance(a, list) else True), False),
(([1, 2], [1, 2, 3], lambda a, b: None if isinstance(a, list) else True), False),
((["hello", "goodbye"], ["hi", "goodbye"], fixtures.is_equal_iteratee0), True),
((["hello", "goodbye"], ["hi", "goodbye"], helpers.is_equal_iteratee0), True),
],
)
def test_is_equal_with(case, expected):
Expand Down
28 changes: 11 additions & 17 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
parametrize = pytest.mark.parametrize


@pytest.fixture
def mocked_sleep():
with mock.patch("time.sleep") as mocked:
yield mocked


@parametrize("case,expected", [((lambda a, b: a / b, 4, 2), 2)])
def test_attempt(case, expected):
assert _.attempt(*case) == expected
Expand Down Expand Up @@ -472,25 +466,25 @@ def test_result(case, expected):
({"attempts": 5, "delay": 1.5, "max_delay": 8.0, "scale": 2.5}, 4, [1.5, 3.75, 8.0, 8.0]),
],
)
def test_retry(mocked_sleep, case, delay_count, delay_times):
def test_retry(mock_sleep, case, delay_count, delay_times):
@_.retry(**case)
def func():
raise Exception()

with pytest.raises(Exception):
func()

assert delay_count == mocked_sleep.call_count
assert delay_count == mock_sleep.call_count

delay_calls = [mock.call(time) for time in delay_times]
assert delay_calls == mocked_sleep.call_args_list
assert delay_calls == mock_sleep.call_args_list


@parametrize(
"case,delay_count",
[({"attempts": 3}, 0), ({"attempts": 3}, 1), ({"attempts": 3}, 2), ({"attempts": 5}, 3)],
)
def test_retry_success(mocked_sleep, case, delay_count):
def test_retry_success(mock_sleep, case, delay_count):
counter = {True: 0}

@_.retry(**case)
Expand All @@ -504,7 +498,7 @@ def func():

assert result is True
assert counter[True] == delay_count
assert delay_count == mocked_sleep.call_count
assert delay_count == mock_sleep.call_count


@parametrize(
Expand All @@ -515,7 +509,7 @@ def func():
({"jitter": 1.0, "delay": 3, "scale": 1.5, "attempts": 5}, [3, 4.5, 6.75, 10.125]),
],
)
def test_retry_jitter(mocked_sleep, case, unexpected_delay_times):
def test_retry_jitter(mock_sleep, case, unexpected_delay_times):
@_.retry(**case)
def func():
raise Exception()
Expand All @@ -525,8 +519,8 @@ def func():

unexpected_delay_calls = [mock.call(time) for time in unexpected_delay_times]

assert len(unexpected_delay_calls) == mocked_sleep.call_count
assert unexpected_delay_calls != mocked_sleep.call_args_list
assert len(unexpected_delay_calls) == mock_sleep.call_count
assert unexpected_delay_calls != mock_sleep.call_args_list


@parametrize(
Expand All @@ -537,18 +531,18 @@ def func():
({"attempts": 2, "exceptions": (RuntimeError,)}, Exception, 0),
],
)
def test_retry_exceptions(mocked_sleep, case, raise_exc, delay_count):
def test_retry_exceptions(mock_sleep, case, raise_exc, delay_count):
@_.retry(**case)
def func():
raise raise_exc()

with pytest.raises(raise_exc):
func()

assert delay_count == mocked_sleep.call_count
assert delay_count == mock_sleep.call_count


def test_retry_on_exception(mocked_sleep):
def test_retry_on_exception(mock_sleep):
attempts = 5
error_count = {True: 0}

Expand Down

0 comments on commit 96f6e48

Please sign in to comment.