From e147bc1b7ae004c5708e4e91c91d71bc97c504cf Mon Sep 17 00:00:00 2001 From: Anilitica Date: Tue, 25 May 2021 18:15:58 -0400 Subject: [PATCH 1/6] more coverage --- tests/test_catmath.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/test_catmath.py b/tests/test_catmath.py index ced6358..5ce2290 100644 --- a/tests/test_catmath.py +++ b/tests/test_catmath.py @@ -2,18 +2,30 @@ def test__cat_years_to_hooman_years__middle_age__succeeds(): - assert True + assert catmath.cat_years_to_hooman_years(10) == 50 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 + +def test__is_cat_leap_year__not_divisible_by_4__isnt_leap_year(): + assert catmath.is_cat_leap_year(1757) is False + + +def test__is_cat_leap_year__divisible_by_100__isnt_leap_year(): + assert catmath.is_cat_leap_year(1900) is False + + +def test__is_cat_leap_year__centurial_leap_year__is_leap_year(): + assert catmath.is_cat_leap_year(2000) is True + From 80320343633ea79462492f7f3752dedf7f83c8bd Mon Sep 17 00:00:00 2001 From: Anilitica Date: Tue, 25 May 2021 18:58:16 -0400 Subject: [PATCH 2/6] tests for safecatmath --- tests/test_catmath.py | 2 +- tests/test_safecatmath.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/test_catmath.py b/tests/test_catmath.py index 5ce2290..729f63f 100644 --- a/tests/test_catmath.py +++ b/tests/test_catmath.py @@ -18,6 +18,7 @@ def test__cat_years_to_hooman_years__0__returns_0(): def test__is_cat_leap_year__succeeds(): assert catmath.is_cat_leap_year(2016) is True + def test__is_cat_leap_year__not_divisible_by_4__isnt_leap_year(): assert catmath.is_cat_leap_year(1757) is False @@ -28,4 +29,3 @@ def test__is_cat_leap_year__divisible_by_100__isnt_leap_year(): def test__is_cat_leap_year__centurial_leap_year__is_leap_year(): assert catmath.is_cat_leap_year(2000) is True - diff --git a/tests/test_safecatmath.py b/tests/test_safecatmath.py index 3f85a35..d8ab58c 100644 --- a/tests/test_safecatmath.py +++ b/tests/test_safecatmath.py @@ -1,4 +1,4 @@ -# import pytest +import pytest from catinabox import safecatmath @@ -19,17 +19,21 @@ 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): + safecatmath.cat_years_to_hooman_years(-5) def test__cat_years_to_hooman_years__older_than_1000__raises(): - assert True + with pytest.raises(safecatmath.InvalidAge): + safecatmath.cat_years_to_hooman_years(2100) def test__cat_years_to_hooman_years__string__raises(): - assert True + with pytest.raises(safecatmath.InvalidAge): + safecatmath.cat_years_to_hooman_years('two') def test__cat_years_to_hooman_years__nan__raises(): - # hooman_age = float('nan') - assert True + hooman_age = float('nan') + with pytest.raises(safecatmath.InvalidAge): + safecatmath.cat_years_to_hooman_years(hooman_age) From 10cca5b710ad22aa6347b077459642dfa4774ff9 Mon Sep 17 00:00:00 2001 From: Anilitica Date: Wed, 26 May 2021 21:42:00 -0400 Subject: [PATCH 3/6] Fixture refactoring! --- tests/test_cattery.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/test_cattery.py b/tests/test_cattery.py index b36692c..85a5e18 100644 --- a/tests/test_cattery.py +++ b/tests/test_cattery.py @@ -3,37 +3,38 @@ from catinabox import cattery +@pytest.fixture +def cattery_client(): + return cattery.Cattery() + + ########################################################################### # add_cats ########################################################################### -def test__add_cats__succeeds(): - c = cattery.Cattery() - c.add_cats(["Fluffy", "Snookums"]) - assert c.cats == ["Fluffy", "Snookums"] - assert c.num_cats == 2 +def test__add_cats__succeeds(cattery_client): + cattery_client.add_cats(["Fluffy", "Snookums"]) + assert cattery_client.cats == ["Fluffy", "Snookums"] + assert cattery_client.num_cats == 2 ########################################################################### # remove_cat ########################################################################### -def test__remove_cat__succeeds(): - c = cattery.Cattery() - c.add_cats(["Fluffy", "Junior"]) - c.remove_cat("Fluffy") - assert c.cats == ["Junior"] - assert c.num_cats == 1 +def test__remove_cat__succeeds(cattery_client): + cattery_client.add_cats(["Fluffy", "Junior"]) + cattery_client.remove_cat("Fluffy") + assert cattery_client.cats == ["Junior"] + assert cattery_client.num_cats == 1 -def test__remove_cat__no_cats__fails(): - c = cattery.Cattery() +def test__remove_cat__no_cats__fails(cattery_client): with pytest.raises(cattery.CatNotFound): - c.remove_cat("Fluffles") + cattery_client.remove_cat("Fluffles") -def test__remove_cat__cat_not_in_cattery__fails(): - c = cattery.Cattery() - c.add_cats(["Fluffy"]) +def test__remove_cat__cat_not_in_cattery__fails(cattery_client): + cattery_client.add_cats(["Fluffy"]) with pytest.raises(cattery.CatNotFound): - c.remove_cat("Snookums") + cattery_client.remove_cat("Snookums") From b5e3bdc0f5e61910d5a53ea93b667cdc6d42a6d9 Mon Sep 17 00:00:00 2001 From: Anilitica Date: Wed, 26 May 2021 22:06:59 -0400 Subject: [PATCH 4/6] Mocking and patching --- tests/test_catactivities.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/tests/test_catactivities.py b/tests/test_catactivities.py index e2051b1..2d6f50c 100644 --- a/tests/test_catactivities.py +++ b/tests/test_catactivities.py @@ -1,14 +1,17 @@ -# import pytest -# import time +import pytest +import time -# 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.object(time, "sleep", autospec=True) + catactivities.cat_nap(500) + mock_sleep.assert_called_with(500) def test__cat_nap__not_satisfying(mocker): - # mock_sleep = mocker.patch.object(time, "sleep", autospec=True) - assert True + mock_sleep = mocker.patch.object(time, "sleep", autospec=True) + with pytest.raises(catactivities.NapWillNotBeSatisfying): + catactivities.cat_nap(5) + assert mock_sleep.call_count == 0 From 95621638fdef59caecbc85c4cb31973813831e55 Mon Sep 17 00:00:00 2001 From: Anilitica Date: Wed, 26 May 2021 23:03:01 -0400 Subject: [PATCH 5/6] Parametrizing input values, invalid input and fixture --- tests/test_catmath.py | 11 +++++++++++ tests/test_cattery.py | 11 +++++++---- tests/test_safecatmath.py | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/tests/test_catmath.py b/tests/test_catmath.py index 729f63f..87509c0 100644 --- a/tests/test_catmath.py +++ b/tests/test_catmath.py @@ -1,6 +1,16 @@ from catinabox import catmath +import pytest +@pytest.mark.parametrize("age, expected", [ + (10, 50), + (0.5, 2.5), + (0, 0) +]) +def test_cat_to_hooman(age, expected): + assert catmath.cat_years_to_hooman_years(age) == expected + +""" def test__cat_years_to_hooman_years__middle_age__succeeds(): assert catmath.cat_years_to_hooman_years(10) == 50 @@ -11,6 +21,7 @@ def test__cat_years_to_hooman_years__less_than_one_year__succeeds(): def test__cat_years_to_hooman_years__0__returns_0(): assert catmath.cat_years_to_hooman_years(0) == 0 +""" # BONUS MATERIAL FOR STEP 2 diff --git a/tests/test_cattery.py b/tests/test_cattery.py index 85a5e18..e6ddf82 100644 --- a/tests/test_cattery.py +++ b/tests/test_cattery.py @@ -1,11 +1,14 @@ import pytest -from catinabox import cattery +from catinabox import cattery, mccattery -@pytest.fixture -def cattery_client(): - return cattery.Cattery() +@pytest.fixture(params=[ + cattery.Cattery, + mccattery.McCattery +]) +def cattery_client(request): + return request.param() ########################################################################### diff --git a/tests/test_safecatmath.py b/tests/test_safecatmath.py index d8ab58c..f62d973 100644 --- a/tests/test_safecatmath.py +++ b/tests/test_safecatmath.py @@ -3,6 +3,15 @@ from catinabox import safecatmath +@pytest.mark.parametrize("age, expected", [ + (7, 35), + (0.1, 0.5), + (0, 0) +]) +def test__cat_years_to_hooman_years(age, expected): + assert safecatmath.cat_years_to_hooman_years(age) == expected + +""" def test__cat_years_to_hooman_years__middle_age__succeeds(): hooman_age = safecatmath.cat_years_to_hooman_years(7) assert hooman_age == 35 @@ -16,8 +25,20 @@ def test__cat_years_to_hooman_years__less_than_one_year__succeeds(): def test__cat_years_to_hooman_years__0__returns_0(): hooman_age = safecatmath.cat_years_to_hooman_years(0) assert hooman_age == 0 +""" +@pytest.mark.parametrize("age", [ + -5, + 2100, + "two", + float('nan') +]) +def test__cat_years_to_hooman_years__invalid_input__raises(age): + with pytest.raises(safecatmath.InvalidAge): + safecatmath.cat_years_to_hooman_years(age) + +""" def test__cat_years_to_hooman_years__less_0__raises(): with pytest.raises(safecatmath.InvalidAge): safecatmath.cat_years_to_hooman_years(-5) @@ -37,3 +58,4 @@ def test__cat_years_to_hooman_years__nan__raises(): hooman_age = float('nan') with pytest.raises(safecatmath.InvalidAge): safecatmath.cat_years_to_hooman_years(hooman_age) +""" From 53223df50f8d4d629ca5ef6e150524ec3f94cc95 Mon Sep 17 00:00:00 2001 From: Anilitica Date: Wed, 26 May 2021 23:47:29 -0400 Subject: [PATCH 6/6] Refactoring for easier unit testing --- catinabox/catgenerator.py | 35 ++++++++++++++++++++--------------- tests/test_catgenerator.py | 38 +++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/catinabox/catgenerator.py b/catinabox/catgenerator.py index b9b1df0..fa63ad2 100644 --- a/catinabox/catgenerator.py +++ b/catinabox/catgenerator.py @@ -18,20 +18,25 @@ def __init__(self, e): ) +def get_name(): + try: + result = requests.get(NAME_GENERATOR_API_ENDPOINT) + return result.json()[0] + except requests.exceptions.RequestException as e: + raise CouldNotGetNameError(e) + + +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] - 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)) - - yield {"name": name, - "birthday": birthday_datetime} + yield {"name": get_name(), + "birthday": get_birthday()} diff --git a/tests/test_catgenerator.py b/tests/test_catgenerator.py index dd7ed44..8289c35 100644 --- a/tests/test_catgenerator.py +++ b/tests/test_catgenerator.py @@ -1,9 +1,37 @@ -# import pytest +import pytest +import requests -# from catinabox import catgenerator +from catinabox import catgenerator -# Write tests for the refactored `catinabox.catgenerator` +def test__get_name(mocker): + mocker.patch("requests.get").return_value.json.return_value = ["Francis"] + cat_name = catgenerator.get_name() + assert cat_name == "Francis" -def test__(): - pass + +def test__get_name__requests_error__raises(mocker): + mocker.patch("requests.get", side_effect=requests.exceptions.Timeout) + with pytest.raises(catgenerator.CouldNotGetNameError): + catgenerator.get_name() + + +def test__get_birthday(mocker): + mocker.patch("time.time", return_value=catgenerator.SECONDS_IN_YEAR * 35) + randint = mocker.patch("random.randint", + return_value=catgenerator.SECONDS_IN_YEAR * 2) + birthday = catgenerator.get_birthday() + assert birthday == "1971-12-31 21:00:00" + randint.assert_called_with(catgenerator.SECONDS_IN_YEAR * 5, + catgenerator.SECONDS_IN_YEAR * 35) + + +def test__cat_generator(mocker): + mocker.patch.object(catgenerator, "get_name", return_value="Moe") + mocker.patch.object(catgenerator, "get_birthday", return_value="birthday") + cat_generator = catgenerator.cat_generator() + + assert next(cat_generator) == {"name": "Moe", + "birthday": "birthday"} + assert next(cat_generator) == {"name": "Moe", + "birthday": "birthday"}