Skip to content

Commit 2ca4a2d

Browse files
added unit tests to the circle exercise
1 parent 050556d commit 2ca4a2d

8 files changed

Lines changed: 348 additions & 8 deletions

File tree

‎source/class_schedule/lesson04.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Exercises:
3636

3737
../exercises/exceptions_lab.rst
3838
../exercises/except_exercise.rst
39-
../exercises/unit_testing.rst
39+
../exercises/unit_testing/unit_testing.rst
4040
../exercises/dict_lab.rst
4141
../exercises/mailroom/mailroom_with_dicts.rst
4242
../exercises/trigrams/trigrams.rst

‎source/class_schedule/lesson08.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Exercises:
3838
.. toctree::
3939
:maxdepth: 1
4040

41-
../exercises/circle_class.rst
41+
../exercises/circle/circle_class.rst
4242

4343
Optional Exercise:
4444
------------------

‎source/exercises/circle/circle.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
circle.py
3+
4+
a class to make the ultimate circle -- and sphere!
5+
6+
"""
7+
8+
class Circle:
9+
"""
10+
fill in this class -- including docstrings!
11+
"""
12+
pass
13+
14+
15+
class Sphere(Circle):
16+
"""
17+
fill in this class -- including docstrings!
18+
"""
19+
pass
Lines changed: 321 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,321 @@
1+
#!/usr/bin/env python
2+
3+
from circle import Circle, Sphere
4+
5+
import pytest
6+
7+
from math import pi
8+
import random
9+
10+
11+
def test_init():
12+
"""
13+
tests that you can initilize a Circle
14+
"""
15+
Circle(3)
16+
17+
18+
def test_radius():
19+
"""
20+
test that the radius is properly set and can be retrieved
21+
"""
22+
c = Circle(3)
23+
24+
assert c.radius == 3
25+
26+
27+
def test_no_radius():
28+
"""
29+
test that you get an Exception if you don't provide a radius
30+
"""
31+
with pytest.raises(TypeError):
32+
c = Circle()
33+
34+
35+
def test_set_radius():
36+
"""
37+
test that changing the radius "stays"
38+
"""
39+
c = Circle(3)
40+
c.radius = 5
41+
assert c.radius == 5
42+
43+
44+
def test_diam():
45+
"""
46+
can you retrieve the diameter?
47+
"""
48+
c = Circle(3)
49+
50+
assert c.diameter == 6
51+
52+
53+
def test_radius_change():
54+
"""
55+
does the diameter change after changing the radius?
56+
"""
57+
c = Circle(3)
58+
c.radius = 4
59+
assert c.diameter == 8
60+
61+
62+
def test_set_diameter():
63+
"""
64+
can you set the diameter?
65+
"""
66+
c = Circle(4)
67+
c.diameter = 10
68+
69+
assert c.radius == 5
70+
assert c.diameter == 10
71+
72+
73+
def test_set_diameter_float():
74+
"""
75+
is floating point computation used for computing the diameter?
76+
"""
77+
c = Circle(4)
78+
c.diameter = 11
79+
80+
assert c.radius == 5.5
81+
assert c.diameter == 11
82+
83+
84+
def test_area():
85+
"""
86+
is the area computed correctly?
87+
"""
88+
c = Circle(2)
89+
90+
assert c.area == pi*4
91+
92+
93+
def test_set_area():
94+
"""
95+
you should not be able to set the area
96+
"""
97+
c = Circle(2)
98+
with pytest.raises(AttributeError):
99+
c.area = 30
100+
101+
102+
def test_from_diameter():
103+
"""
104+
can we create a Circle from the diameter?
105+
"""
106+
c = Circle.from_diameter(4)
107+
108+
assert isinstance(c, Circle)
109+
assert c.radius == 2
110+
assert c.diameter == 4
111+
112+
113+
def test_repr():
114+
"""
115+
does it have a correct repr()?
116+
"""
117+
c = Circle(6.0)
118+
119+
assert repr(c) == 'Circle(6.0)'
120+
121+
122+
def test_str():
123+
"""
124+
does it have a correct str()?
125+
"""
126+
c = Circle(3.0)
127+
128+
assert str(c) == 'Circle with radius: 3'
129+
130+
131+
def test_addition():
132+
"""
133+
do two circles add up correctly
134+
"""
135+
c1 = Circle(2)
136+
c2 = Circle(3)
137+
c3 = c1 + c2
138+
139+
assert c3.radius == 5
140+
141+
142+
def test_multiplication():
143+
"""
144+
Can you scale a circle my multiplying it?
145+
"""
146+
c1 = Circle(2)
147+
c3 = c1 * 4
148+
149+
assert c3.radius == 8
150+
151+
152+
def test_equal():
153+
"""
154+
Does equality work?
155+
"""
156+
c1 = Circle(3)
157+
c2 = Circle(3.0)
158+
159+
assert c1 == c2
160+
assert c1 <= c2
161+
assert c1 >= c2
162+
163+
164+
def test_not_equal():
165+
"""
166+
does not equal work?
167+
"""
168+
c1 = Circle(2.9)
169+
c2 = Circle(3.0)
170+
171+
assert c1 != c2
172+
173+
174+
def test_greater():
175+
"""
176+
comparison: greater than
177+
"""
178+
c1 = Circle(2)
179+
c2 = Circle(3)
180+
181+
assert c2 > c1
182+
assert c2 >= c1
183+
184+
185+
def test_less():
186+
"""
187+
comparison: less than
188+
"""
189+
c1 = Circle(2)
190+
c2 = Circle(3)
191+
192+
assert c1 < c2
193+
assert c1 <= c2
194+
195+
196+
def test_reverse_multiply():
197+
"""
198+
multiplication in the other order
199+
"""
200+
c = Circle(3)
201+
202+
c2 = 3 * c
203+
204+
assert c2.radius == 9.0
205+
206+
207+
def test_plus_equal():
208+
"""
209+
in-place addition
210+
"""
211+
c = Circle(3)
212+
c2 = c
213+
214+
c += Circle(2)
215+
216+
assert c.radius == 5
217+
assert c is c2
218+
assert c2.radius == 5
219+
220+
221+
def test_times_equal():
222+
"""
223+
in-place multiplication
224+
"""
225+
c = Circle(3)
226+
c2 = c
227+
228+
c *= 2
229+
230+
assert c.radius == 6
231+
assert c is c2
232+
assert c2.radius == 6
233+
234+
235+
def test_sort():
236+
"""
237+
can circles be sorted?
238+
"""
239+
a_list = [Circle(20), Circle(10), Circle(15), Circle(5)]
240+
241+
a_list.sort()
242+
243+
assert a_list[0] == Circle(5)
244+
assert a_list[3] == Circle(20)
245+
assert a_list[0] < a_list[1] < a_list[2] < a_list[3]
246+
247+
248+
#############################
249+
# Tests for the Sphere Object
250+
#############################
251+
def test_sphere_vol():
252+
"""
253+
Is the volume correct?
254+
"""
255+
s = Sphere(4)
256+
257+
print(s.volume())
258+
assert s.volume() == 268.082573106329
259+
260+
261+
def test_sphere_change_radius():
262+
"""
263+
create from diameter, change the radius
264+
265+
NOTE: if you subclassed Circlel then this all should "just work"!
266+
"""
267+
s = Sphere.from_diameter(8)
268+
269+
assert s.radius == 4
270+
271+
s.radius = 3
272+
assert s.diameter == 6
273+
274+
275+
def test_sphere_diameter():
276+
s = Sphere.from_diameter(8)
277+
278+
# check that the classmethod got properly inherited
279+
assert type(s) == Sphere
280+
print(s.volume())
281+
assert s.volume() == 268.082573106329
282+
283+
284+
def test_sphere_area():
285+
"""
286+
sphere's don't really have an area in the same way
287+
"""
288+
s = Sphere(4)
289+
290+
with pytest.raises(NotImplementedError):
291+
s.area()
292+
293+
294+
def test_sphere_repr():
295+
s = Sphere(12)
296+
297+
assert repr(s) == "Sphere(12)"
298+
assert eval(repr(s)) == s
299+
300+
301+
def test_sphere_str():
302+
s = Sphere(12)
303+
304+
assert str(s) == "Sphere with radius: 12"
305+
assert eval(repr(s)) == s
306+
307+
308+
def test_sphere_sort():
309+
list_o_spheres = [Sphere(random.randint(1, 20)) for i in range(10)]
310+
311+
print(list_o_spheres)
312+
313+
list_o_spheres.sort()
314+
315+
print(list_o_spheres)
316+
317+
for s1, s2 in zip(list_o_spheres, list_o_spheres[1:]):
318+
assert s1 <= s2
319+
320+
321+

‎source/exercises/index.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Lesson 6: Unit Testing and Advanced Argument Passing
7171
.. toctree::
7272
:maxdepth: 1
7373

74-
unit_testing.rst
74+
unit_testing/unit_testing.rst
7575
args_kwargs_lab.rst
7676
mailroom/mailroom_with_tests.rst
7777

@@ -93,7 +93,7 @@ Lesson 8: Properties and Magic Methods
9393
.. toctree::
9494
:maxdepth: 1
9595

96-
circle_class.rst
96+
circle/circle_class.rst
9797

9898
Lesson 9: Multiple Inheritance
9999
------------------------------

source/exercises/unit_testing.rst renamed to source/exercises/unit_testing/unit_testing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ Test Driven Development
4141

4242
Download these files, or find them in the exercise repo:
4343

44-
:download:`test_walnut_party.py <unit_testing/test_walnut_party.py>`
44+
:download:`test_walnut_party.py <test_walnut_party.py>`
4545

4646
and:
4747

48-
:download:`walnut_party.py <unit_testing/walnut_party.py>`
48+
:download:`walnut_party.py <walnut_party.py>`
4949

5050
(This is the adapted from the codingbat site: http://codingbat.com/prob/p195669)
5151

0 commit comments

Comments
 (0)