This repository has been archived by the owner on Aug 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.py
67 lines (51 loc) · 2.23 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
import unittest
from freezegun import freeze_time
try:
from mock import patch
except ImportError:
from unittest.mock import patch
from requests import Response
from dumbfunctions import count_csv_rows, site_is_up, square, todays_day
class TestDumbMathFunctions(unittest.TestCase):
def test_squares_ints(self):
"""When passed an integer, it should square it"""
self.assertEqual(square(2), 4)
def test_squares_floats(self):
"""When passed a float, it should square it"""
self.assertEqual(square(1.5), 2.25)
def test_string_fail(self):
"""Trying to square a string raises a TypeError"""
with self.assertRaises(TypeError):
square('string')
class TestDumbRequestsFunctions(unittest.TestCase):
@patch('dumbfunctions.requests.get')
def test_site_requested(self, mock_requests):
"""Should make one GET request to passed URL"""
mock_requests.return_value.status_code = 200
site_is_up('http://www.google.com')
mock_requests.assert_called_with('http://www.google.com')
self.assertEqual(mock_requests.call_count, 1)
self.assertTrue(site_is_up)
@patch('dumbfunctions.requests')
def test_request_fails(self, mock_requests):
"""Should return False if URL errors"""
mock_requests.get.return_value = Response()
mock_requests.get.return_value.status_code = 500
self.assertEqual(site_is_up('not-a-url.aaaaaa'), False)
@patch('dumbfunctions.requests')
def test_request_ok(self, mock_requests):
"""Should return True if URL returns OK status code"""
mock_requests.get.return_value = Response()
mock_requests.get.return_value.status_code = 200
self.assertEqual(site_is_up('not-a-url.aaaaaa'), True)
@freeze_time('2017-05-03')
class TestDumbDayFunction(unittest.TestCase):
def test_todays_day(self):
"""Should return the current day of the week"""
self.assertEqual(todays_day(), 'Today is Wednesday')
class TestDumbCsvFunction(unittest.TestCase):
def test_returns_row_count(self):
"""Should count the number of rows in a CSV"""
self.assertEqual(count_csv_rows('fixtures/csv_input.csv'), 2)
if __name__ == '__main__':
unittest.main()