Skip to content

Commit eaa65b1

Browse files
committed
Add code samples
1 parent dd9cbfd commit eaa65b1

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

Diff for: LICENSE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Keep Python Weird, Holly Becker
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

Diff for: README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This tutorial was developed for [PyLadies Vancouver](http://www.pyladies.com/loc
1818

1919
Instructions for following the tutorial are in the [docs](docs) folder.
2020
Code to modify and play with is in the [code](code) folder.
21+
Tests can be run with `python -m unittest` or `python -m unittest code.test_cattery` from this directory.
2122

2223

2324
## Prerequisites
@@ -87,6 +88,8 @@ We're also showing two workflows.
8788
* Windows user
8889
* 8 years creating & resolving merge conflicts
8990

91+
Some code from [catinabox Python testing tutorial](https://github.com/keeppythonweird/catinabox).
92+
9093
## License
9194

92-
The content of this project is licensed [CC-BY-SA](https://creativecommons.org/licenses/by-sa/4.0/).
95+
The content of this project is licensed [CC-BY-SA](https://creativecommons.org/licenses/by-sa/4.0/), and the example source code is licensed under the MIT license.

Diff for: code/__init__.py

Whitespace-only changes.

Diff for: code/cattery.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
"""Cat collection classes."""
3+
from __future__ import absolute_import, unicode_literals, print_function
4+
5+
# Exceptions
6+
7+
class CatNotFound(Exception):
8+
"""The requested cat was not found in the cattery."""
9+
10+
def __init__(self, name):
11+
super(CatNotFound, self).__init__(
12+
"Cat with name {!r} not found in cattery.".format(name)
13+
)
14+
self.name = name
15+
16+
17+
# Classes
18+
19+
class Cattery(object):
20+
"""A collection of cats."""
21+
22+
def __init__(self):
23+
self._cats = []
24+
25+
@property
26+
def num_cats(self):
27+
return len(self._cats)
28+
29+
@property
30+
def cats(self):
31+
return self._cats
32+
33+
def add_cats(self, names):
34+
"""Add cats with the specified names to the cattery.
35+
36+
:param names: A list of the names of cats to add to the cattery.
37+
"""
38+
self._cats.extend(names)
39+
40+
def remove_cat(self, name):
41+
"""Remove the specified cat from the cattery.
42+
43+
The first cat found in the cattery with the specified name will be
44+
removed.
45+
46+
:param name: The name of the cat to remove.
47+
"""
48+
cats = [cat for cat in self._cats if cat == name]
49+
if len(cats) == 0:
50+
raise CatNotFound(name)
51+
self._cats.remove(cats[0])

Diff for: code/test_cattery.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
"""Test cattery.py file."""
3+
4+
import unittest
5+
6+
from . import cattery
7+
8+
9+
class TestCattery(unittest.TestCase):
10+
"""Test Cattery class."""
11+
12+
def setUp(self):
13+
self.cattery = cattery.Cattery()
14+
15+
# add_cats
16+
17+
def test__add_cats__succeeds(self):
18+
"""It should successfully add cats."""
19+
self.cattery.add_cats(["Fluffy", "Snookums"])
20+
assert self.cattery.cats == ["Fluffy", "Snookums"]
21+
assert self.cattery.num_cats == 2
22+
23+
# remove_cat
24+
25+
def test__remove_cat__succeeds(self):
26+
"""It should remove a cat."""
27+
self.cattery.add_cats(["Fluffy", "Junior"])
28+
self.cattery.remove_cat("Fluffy")
29+
assert self.cattery.cats == ["Junior"]
30+
assert self.cattery.num_cats == 1
31+
32+
def test__remove_cat__no_cats__fails(self):
33+
"""It should fail to remove non-existent cats."""
34+
with self.assertRaises(cattery.CatNotFound):
35+
self.cattery.remove_cat("Fluffles")
36+
37+
def test__remove_cat__cat_not_in_cattery__fails(self):
38+
"""It should fail to remove cats not in the cattery."""
39+
self.cattery.add_cats(["Fluffy"])
40+
with self.assertRaises(cattery.CatNotFound):
41+
self.cattery.remove_cat("Snookums")
42+
43+
if __name__ == '__main__':
44+
unittest.main()

0 commit comments

Comments
 (0)