diff --git a/catinabox/catgenerator.py b/catinabox/catgenerator.py index b9b1df0..0ad9a54 100644 --- a/catinabox/catgenerator.py +++ b/catinabox/catgenerator.py @@ -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 @@ -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} diff --git a/tests/test_catactivities.py b/tests/test_catactivities.py index e2051b1..6a572cf 100644 --- a/tests/test_catactivities.py +++ b/tests/test_catactivities.py @@ -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 diff --git a/tests/test_catgenerator.py b/tests/test_catgenerator.py index dd7ed44..b661978 100644 --- a/tests/test_catgenerator.py +++ b/tests/test_catgenerator.py @@ -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"} diff --git a/tests/test_catmath.py b/tests/test_catmath.py index ced6358..676f6a6 100644 --- a/tests/test_catmath.py +++ b/tests/test_catmath.py @@ -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 diff --git a/tests/test_cattery.py b/tests/test_cattery.py index b36692c..13b7d86 100644 --- a/tests/test_cattery.py +++ b/tests/test_cattery.py @@ -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 @@ -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") diff --git a/tests/test_safecatmath.py b/tests/test_safecatmath.py index 3f85a35..57b3025 100644 --- a/tests/test_safecatmath.py +++ b/tests/test_safecatmath.py @@ -1,4 +1,5 @@ # import pytest +import pytest from catinabox import safecatmath @@ -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)