-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
92 lines (63 loc) · 2.18 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import unittest
from unittest import TestCase, mock
import vcr
from shapes import Rectangle, Cylinder
from twitter import list_twitter_repos
class RectangleTestCase(TestCase):
def setUp(self):
self.rectangle = Rectangle(width=7, height=8)
def test_rectangle_area(self):
"""
Test that we can calculate the area of a rectangle
"""
area = self.rectangle.area()
self.assertEqual(area, 56)
@mock.patch('shapes.tweet')
def test_rectangle_broadcast(self, mock_tweet):
"""
Tests that we call tweet with a formatted message
"""
self.rectangle.broadcast()
mock_tweet.assert_called_with('My rectangle is 7 by 8')
class CylinderTestCase(TestCase):
def test_cylinder_area_of_base(self):
"""
Test that we can calculate the area of a cylinder's base
"""
cylinder = Cylinder(radius=2, height=7)
area = cylinder.area_of_base()
self.assertAlmostEqual(area, 12.6, places=1)
@mock.patch('shapes.Cylinder.area_of_base')
def test_cylinder_volume(self, mock_area):
"""
Test that we can calculate the volume of a cylinder
"""
mock_area.return_value = 5
cylinder = Cylinder(radius=2, height=7)
self.assertEqual(cylinder.volume(), 35)
class TwitterTestCase(TestCase):
def test_list_twitter_repos(self):
"""
Test that we can get a list of Twitter's repos
"""
with vcr.use_cassette('vcr/list_twitter_repos.yaml'):
repos = list_twitter_repos()
self.assertEqual(repos[0], 'kestrel')
def test_list_twitter_repos_error(self):
"""
Test that we fail gracefully if the GitHub API is down
"""
with vcr.use_cassette('vcr/list_twitter_repos_error.yaml'):
repos = list_twitter_repos()
self.assertEqual(repos, 'The GitHub API is currently unavailable')
if __name__ == '__main__':
unittest.main()
# This test will be picked up by Nose, but not by running
# this file as a script, or with Django
def test_rectangle_area_again():
"""
Test that we can calculate the area of a rectangle
"""
rectangle = Rectangle(width=8, height=8)
area = rectangle.area()
assert area == 64