Skip to content

Commit

Permalink
adding equal and making sure false and true are correct (#28)
Browse files Browse the repository at this point in the history
* waiting for a real True
* waiting for a real False
* refactored out to_be_equal
  • Loading branch information
studioj authored Aug 22, 2019
1 parent e6290af commit 9e7c590
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 18 deletions.
31 changes: 31 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Bug report
about: Create a report to help us improve
title: "[Issue["
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**TraceBacks**
If applicable, add TraceBacks to help explain your problem.

**Desktop (please complete the following information):**
- OS: [e.g. iOS]
- Version [e.g. 22]

**Additional context**
Add any other context about the problem here.
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest an idea for this project
title: "[feature-request]"
labels: enhancement
assignees: ''

---

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
black==19.3b0
docutils==0.15.2
pytest==5.0.1
pytest==5.1.1
twine==1.13.0
mock==3.0.5
54 changes: 54 additions & 0 deletions tests/test_wait_for_it_to_be_equal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import unittest

from mock import patch, MagicMock

import wait_for_it_to


class TestWaitForItToBeEqual(unittest.TestCase):
def test_wait_for_it_to_be_equal_immediately_returns_when_func_evals_to_the_same_String(self):
foo = MagicMock()
foo.return_value = "some_value"
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
wait_for_it_to.be_equal(foo, "some_value")
mocked_sleep.assert_not_called()

def test_to_be_equal_sleeps_once_when_the_func_returns_only_after_the_second_try(self):
foo = MagicMock()
foo.side_effect = [0, 1]
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
wait_for_it_to.be_equal(foo, 1)
mocked_sleep.assert_called_with(0.01)

def test_to_be_equal_calls_the_passed_function_object(self):
foo = MagicMock()
foo.return_value = True
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
wait_for_it_to.be_equal(foo, True)
foo.assert_called_once()

def test_to_be_equal_raises_timeout_error_when_timeout_has_passed(self):
foo = MagicMock()
foo.return_value = False
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
with patch("wait_for_it_to.time.time") as mocked_time:
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_equal, foo, True)

def test_i_can_set_a_custom_timeout_for_to_be_equal(self):
foo = MagicMock()
foo.return_value = "False"
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
with patch("wait_for_it_to.time.time") as mocked_time:
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_equal, foo, "True", 5)
self.assertEqual(5, mocked_sleep.call_count)

def test_default_timeout_for_to_be_equal_is_10_seconds(self):
foo = MagicMock()
foo.return_value = False
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
with patch("wait_for_it_to.time.time") as mocked_time:
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_equal, foo, True)
self.assertEqual(10, mocked_sleep.call_count)
8 changes: 8 additions & 0 deletions tests/test_wait_for_it_to_be_false.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ def test_default_timeout_for_to_be_false_is_10_seconds(self):
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_false, foo)
self.assertEqual(10, mocked_sleep.call_count)

def test_to_be_false_raises_timeout_error_when_func_returns_none(self):
foo = MagicMock()
foo.return_value = None
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
with patch("wait_for_it_to.time.time") as mocked_time:
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_true, foo)
11 changes: 10 additions & 1 deletion tests/test_wait_for_it_to_be_true.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ def test_to_be_true_sleeps_once_when_the_func_returns_only_after_the_second_try(
wait_for_it_to.be_true(foo)
mocked_sleep.assert_called_with(0.01)

def test_to_be_true_calls_the_passed_function_obejct(self):
def test_to_be_true_calls_the_passed_function_object(self):
foo = MagicMock()
foo.return_value = True
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
wait_for_it_to.be_true(foo)
foo.assert_called_once()
Expand Down Expand Up @@ -54,3 +55,11 @@ def test_default_timeout_for_to_be_true_is_10_seconds(self):
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_true, foo)
self.assertEqual(10, mocked_sleep.call_count)

def test_to_be_true_raises_timeout_error_when_func_returns_a_string(self):
foo = MagicMock()
foo.return_value = "a string which isnt equal to True"
with patch("wait_for_it_to.time.sleep") as mocked_sleep:
with patch("wait_for_it_to.time.time") as mocked_time:
mocked_time.side_effect = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
self.assertRaises(TimeoutError, wait_for_it_to.be_true, foo)
39 changes: 24 additions & 15 deletions wait_for_it_to/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def be_true(func, timeout=10):
"""
waits until func evaluates to True
waits until func returns True
raises an exception when the timeout expires
:param timeout: a timeout in seconds
Expand All @@ -18,20 +18,12 @@ def be_true(func, timeout=10):
>>>wait_for_it_to.be_true(foo, timeout=5)
"""
start = time.time()
while True:
result = func()
if result:
return True
if time.time() > start + timeout:
msg = "expected something that evaluates to True, but got %s instead" % str(result)
raise TimeoutError(msg)
time.sleep(0.01)
be_equal(func, True, timeout)


def be_false(func, timeout=10):
"""
waits until func evaluates to False
waits until func returns False
raises an exception when the timeout expires
:param timeout: a timeout in seconds
Expand All @@ -43,12 +35,29 @@ def be_false(func, timeout=10):
>>>wait_for_it_to.be_false(foo)
>>>wait_for_it_to.be_false(foo, timeout=5)
"""
be_equal(func, False, timeout)


def be_equal(func, expected_value, timeout=10):
"""
waits until func is equal to expected_value
raises an exception when the timeout expires
:param expected_value: any value func should evaluate to
:param timeout: a timeout in seconds
:param func: an executable object
>>>def foo():
>>> return "any object"
>>>
>>>wait_for_it_to.be_equal(foo, "any object")
>>>wait_for_it_to.be_equal(foo, "any object", timeout=5)
"""
start = time.time()
while True:
result = func()
if not result:
return False
result = func()
while result != expected_value:
if time.time() > start + timeout:
msg = "expected something that evaluates to True, but got %s instead" % str(result)
raise TimeoutError(msg)
time.sleep(0.01)
result = func()
2 changes: 1 addition & 1 deletion wait_for_it_to/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version_info__ = (0, 0, 9)
__version_info__ = (0, 0, 10)
__version__ = ".".join(map(str, __version_info__))

0 comments on commit 9e7c590

Please sign in to comment.