Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tutorial wip #202

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions catinabox/catgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import requests
import time


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


def get_name():
result = requests.get(NAME_GENERATOR_API_ENDPOINT)
name = result.json()[0]
return name


def get_birthday():
current_time = int(time.time())
birthday = random.randint(
current_time - (SECONDS_IN_YEAR * MAX_YEARS_OLD),
current_time)
birthday_datetime = time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(birthday))
return birthday_datetime


def cat_generator():
while True:
try:
result = requests.get(NAME_GENERATOR_API_ENDPOINT)
name = result.json()[0]
name = get_name()
except requests.exceptions.RequestException as e:
raise CouldNotGetNameError(e)

current_time = int(time.time())
birthday = random.randint(
current_time - (SECONDS_IN_YEAR * MAX_YEARS_OLD),
current_time)
birthday_datetime = time.strftime('%Y-%m-%d %H:%M:%S',
time.localtime(birthday))
birthday_datetime = get_birthday()

yield {"name": name,
"birthday": birthday_datetime}
16 changes: 9 additions & 7 deletions tests/test_catactivities.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# import pytest
# import time
import pytest

# from catinabox import catactivities
from catinabox import catactivities


def test__cat_nap__satisfying_nap(mocker):
# mock_sleep = mocker.patch.object(time, "sleep", autospec=True)
assert True
mock_sleep = mocker.patch('time.sleep', autospec=True)
catactivities.cat_nap(301)
mock_sleep.assert_called_with(301)


def test__cat_nap__not_satisfying(mocker):
# mock_sleep = mocker.patch.object(time, "sleep", autospec=True)
assert True
mock_sleep = mocker.patch('time.sleep', autospec=True)
with pytest.raises(catactivities.NapWillNotBeSatisfying):
catactivities.cat_nap(299)
assert mock_sleep.call_count == 0
32 changes: 27 additions & 5 deletions tests/test_catgenerator.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
# import pytest
# from catinabox.catgenerator import get_name
# from catinabox.catgenerator import get_birthday
from catinabox import catgenerator

# from catinabox import catgenerator

def test_get_name(mocker):
mock_get = mocker.patch('requests.get', autospec=True)
mock_get.return_value.json.return_value = (['tim'],)
assert catgenerator.get_name() == ['tim']

# Write tests for the refactored `catinabox.catgenerator`

def test__():
pass
def test_get_birthday(mocker):
mock_time = mocker.patch('time.time', autospec=True)
mock_time.side_effect = (catgenerator.SECONDS_IN_YEAR * 35,)
mock_bday = mocker.patch('random.randint')
mock_bday.return_value = catgenerator.SECONDS_IN_YEAR * 2
assert catgenerator.get_birthday() == "1972-01-01 00:00:00"
mock_bday.assert_called_with(catgenerator.SECONDS_IN_YEAR * 5,
catgenerator.SECONDS_IN_YEAR * 35)


def test_cat_generator(mocker):
mock_name = mocker.patch.object(catgenerator, 'get_name')
mock_name.side_effect = ["David", "Moe"]
mock_bday = mocker.patch.object(catgenerator, 'get_birthday')
mock_bday.return_value = 'birthday'
catgen = catgenerator.cat_generator()
assert next(catgen) == {"name": "David",
"birthday": "birthday"}
assert next(catgen) == {"name": "Moe",
"birthday": "birthday"}
22 changes: 17 additions & 5 deletions tests/test_catmath.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import pytest

from catinabox import catmath


def test__cat_years_to_hooman_years__middle_age__succeeds():
assert True
assert catmath.cat_years_to_hooman_years(7) == 35


def test__cat_years_to_hooman_years__less_than_one_year__succeeds():
assert True
assert catmath.cat_years_to_hooman_years(0.5) == 2.5


def test__cat_years_to_hooman_years__0__returns_0():
assert True
assert catmath.cat_years_to_hooman_years(0) == 0


# BONUS MATERIAL FOR STEP 2

def test__is_cat_leap_year__succeeds():
assert catmath.is_cat_leap_year(2016) is True
testdata = [
(4, True),
(1757, False),
(2004, True),
(1900, False),
(2000, True)
]


@pytest.mark.parametrize('year, expected', testdata)
def test__is_cat_leap_year__succeeds(year, expected):
assert catmath.is_cat_leap_year(year) is expected
22 changes: 13 additions & 9 deletions tests/test_cattery.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import pytest

from catinabox import cattery
from catinabox import cattery, mccattery


###########################################################################
# add_cats
###########################################################################
@pytest.fixture(params=[cattery, mccattery])
def cattery_client(request):
return request.param.Cattery()

def test__add_cats__succeeds():
c = cattery.Cattery()

def test__add_cats__succeeds(cattery_client):
c = cattery_client
c.add_cats(["Fluffy", "Snookums"])
assert c.cats == ["Fluffy", "Snookums"]
assert c.num_cats == 2
Expand All @@ -18,22 +22,22 @@ def test__add_cats__succeeds():
# remove_cat
###########################################################################

def test__remove_cat__succeeds():
c = cattery.Cattery()
def test__remove_cat__succeeds(cattery_client):
c = cattery_client
c.add_cats(["Fluffy", "Junior"])
c.remove_cat("Fluffy")
assert c.cats == ["Junior"]
assert c.num_cats == 1


def test__remove_cat__no_cats__fails():
c = cattery.Cattery()
def test__remove_cat__no_cats__fails(cattery_client):
c = cattery_client
with pytest.raises(cattery.CatNotFound):
c.remove_cat("Fluffles")


def test__remove_cat__cat_not_in_cattery__fails():
c = cattery.Cattery()
def test__remove_cat__cat_not_in_cattery__fails(cattery_client):
c = cattery_client
c.add_cats(["Fluffy"])
with pytest.raises(cattery.CatNotFound):
c.remove_cat("Snookums")
26 changes: 21 additions & 5 deletions tests/test_safecatmath.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# import pytest
import pytest

from catinabox import safecatmath

Expand All @@ -19,17 +20,32 @@ def test__cat_years_to_hooman_years__0__returns_0():


def test__cat_years_to_hooman_years__less_0__raises():
assert True
with pytest.raises(safecatmath.InvalidAge):
cat_age = -1
assert safecatmath.cat_years_to_hooman_years(cat_age)


def test__cat_years_to_hooman_years__older_than_1000__raises():
assert True
with pytest.raises(safecatmath.InvalidAge):
cat_age = 1001
assert safecatmath.cat_years_to_hooman_years(cat_age)


def test__cat_years_to_hooman_years__string__raises():
assert True
with pytest.raises(safecatmath.InvalidAge):
cat_age = 'five'
assert safecatmath.cat_years_to_hooman_years(cat_age)


def test__cat_years_to_hooman_years__nan__raises():
# hooman_age = float('nan')
assert True
with pytest.raises(safecatmath.InvalidAge):
cat_age = float('nan')
assert safecatmath.cat_years_to_hooman_years(cat_age)


@pytest.mark.parametrize('cat_age', (
-3, 1002, 'six', [4, 2], {3}, (7, 5)
))
def test__cat_years_to_hooman_years__invalid(cat_age):
with pytest.raises(safecatmath.InvalidAge):
assert safecatmath.cat_years_to_hooman_years(cat_age)