Skip to content

Commit 1350e40

Browse files
committed
add test_catgenerator tests and refactored catgenerator
1 parent a7ad914 commit 1350e40

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

catinabox/catgenerator.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import requests
33
import time
44

5-
65
NAME_GENERATOR_API_ENDPOINT = "http://namey.muffinlabs.com/name.json"
76
# ignoring leap years for now
87
SECONDS_IN_YEAR = 365 * 24 * 60 * 60
@@ -18,20 +17,30 @@ def __init__(self, e):
1817
)
1918

2019

20+
def get_name():
21+
result = requests.get(NAME_GENERATOR_API_ENDPOINT)
22+
name = result.json()[0]
23+
return name
24+
25+
26+
def get_birthday():
27+
current_time = int(time.time())
28+
birthday = random.randint(
29+
current_time - (SECONDS_IN_YEAR * MAX_YEARS_OLD),
30+
current_time)
31+
birthday_datetime = time.strftime('%Y-%m-%d %H:%M:%S',
32+
time.localtime(birthday))
33+
return birthday_datetime
34+
35+
2136
def cat_generator():
2237
while True:
2338
try:
24-
result = requests.get(NAME_GENERATOR_API_ENDPOINT)
25-
name = result.json()[0]
39+
name = get_name()
2640
except requests.exceptions.RequestException as e:
2741
raise CouldNotGetNameError(e)
2842

29-
current_time = int(time.time())
30-
birthday = random.randint(
31-
current_time - (SECONDS_IN_YEAR * MAX_YEARS_OLD),
32-
current_time)
33-
birthday_datetime = time.strftime('%Y-%m-%d %H:%M:%S',
34-
time.localtime(birthday))
43+
birthday_datetime = get_birthday()
3544

36-
yield {"name": name,
45+
yield {"name" : name,
3746
"birthday": birthday_datetime}

tests/test_catgenerator.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
1-
# import pytest
1+
from catinabox.catgenerator import get_name, get_birthday
2+
from catinabox import catgenerator
23

3-
# from catinabox import catgenerator
44

5+
def test_get_name(mocker):
6+
mock_get = mocker.patch('requests.get', autospec=True)
7+
mock_get.return_value.json.return_value = (['tim'],)
8+
assert get_name() == ['tim']
59

6-
# Write tests for the refactored `catinabox.catgenerator`
710

8-
def test__():
9-
pass
11+
def test_get_birthday(mocker):
12+
mock_time = mocker.patch('time.time', autospec=True)
13+
mock_time.side_effect = (catgenerator.SECONDS_IN_YEAR * 35,)
14+
mock_bday = mocker.patch('random.randint')
15+
mock_bday.return_value = catgenerator.SECONDS_IN_YEAR * 2
16+
assert catgenerator.get_birthday() == "1972-01-01 11:00:00"
17+
mock_bday.assert_called_with(catgenerator.SECONDS_IN_YEAR * 5,
18+
catgenerator.SECONDS_IN_YEAR * 35)
19+
20+
21+
def test_cat_generator(mocker):
22+
mock_name = mocker.patch.object(get_name, autospec=True)
23+
mock_name.side_effect = ["David", "Moe"]
24+
mock_bday = mocker.patch.object(get_birthday)
25+
mock_bday.return_value = 'birthday'
26+
catgen = catgenerator.cat_generator()
27+
assert next(catgen) == {"name" : "David",
28+
"birthday": "birthday"}
29+
assert next(catgen) == {"name" : "Moe",
30+
"birthday": "birthday"}

0 commit comments

Comments
 (0)