Skip to content
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# catinabox - Intro to Testing and Test Automation in Python

**This branch includes all solutions!**

[![Build Status](https://travis-ci.org/keeppythonweird/catinabox.svg?branch=master)](https://travis-ci.org/keeppythonweird/catinabox)

Coverage status:
Expand Down Expand Up @@ -35,6 +38,4 @@ exploring these features of unit testing in general and pytest in particular:
7. [Parameterized tests](./steps/7-params.md)
8. [Refactoring for unit testability](./steps/8-refactor.md)

Solutions are visible by viewing [the solutions branch](https://github.com/keeppythonweird/catinabox/tree/solutions).

![cattery](pics/cattery.png)
35 changes: 20 additions & 15 deletions catinabox/catgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.gmtime(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()}
17 changes: 10 additions & 7 deletions tests/test_catactivities.py
Original file line number Diff line number Diff line change
@@ -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
38 changes: 33 additions & 5 deletions tests/test_catgenerator.py
Original file line number Diff line number Diff line change
@@ -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 == "1972-01-01 00: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"}
27 changes: 22 additions & 5 deletions tests/test_catmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,35 @@


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


def test__cat_years_to_hooman_years__less_than_one_year__succeeds():
assert True
cat_age = 0.1
hooman_age = catmath.cat_years_to_hooman_years(cat_age)
assert hooman_age == 0.5


def test__cat_years_to_hooman_years__0__returns_0():
assert True
hooman_age = catmath.cat_years_to_hooman_years(0)
assert hooman_age == 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


def test__is_cat_leap_year__typical_leap_year__is_leap_year():
assert catmath.is_cat_leap_year(2004) is True
37 changes: 19 additions & 18 deletions tests/test_cattery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
43 changes: 43 additions & 0 deletions tests/test_mccattery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import pytest

from catinabox import cattery, mccattery


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


###########################################################################
# add_cats
###########################################################################

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(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(cattery_client):
with pytest.raises(cattery.CatNotFound):
cattery_client.remove_cat("Fluffles")


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

from catinabox import safecatmath

Expand All @@ -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(-4)


def test__cat_years_to_hooman_years__older_than_1000__raises():
assert True
with pytest.raises(safecatmath.InvalidAge):
safecatmath.cat_years_to_hooman_years(1000.1)


def test__cat_years_to_hooman_years__string__raises():
assert True
with pytest.raises(safecatmath.InvalidAge):
safecatmath.cat_years_to_hooman_years("five")


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)