|
| 1 | +import unittest |
| 2 | + |
| 3 | + |
| 4 | +class BookAppTestCase(unittest.TestCase): |
| 5 | + """shared functionality""" |
| 6 | + |
| 7 | + def setUp(self): |
| 8 | + from bookdb import database |
| 9 | + self.db = database |
| 10 | + |
| 11 | + |
| 12 | +class BookDBTestCase(BookAppTestCase): |
| 13 | + """tests for the bookdb code""" |
| 14 | + |
| 15 | + def makeOne(self): |
| 16 | + from bookdb import BookDB |
| 17 | + return BookDB() |
| 18 | + |
| 19 | + def test_all_titles_returned(self): |
| 20 | + actual_titles = self.makeOne().titles() |
| 21 | + self.assertEqual(len(actual_titles), len(self.db)) |
| 22 | + |
| 23 | + def test_all_titles_correct(self): |
| 24 | + actual_titles = self.makeOne().titles() |
| 25 | + for actual_title in actual_titles: |
| 26 | + self.assertTrue(actual_title['id'] in self.db) |
| 27 | + actual = actual_title['title'] |
| 28 | + expected = self.db[actual_title['id']]['title'] |
| 29 | + self.assertEqual(actual, expected) |
| 30 | + |
| 31 | + def test_title_info_complete(self): |
| 32 | + use_id, expected = self.db.items()[0] |
| 33 | + actual = self.makeOne().title_info(use_id) |
| 34 | + # demonstrate all actual keys are expected |
| 35 | + for key in actual: |
| 36 | + self.assertTrue(key in expected) |
| 37 | + # demonstrate all expected keys are present in actual |
| 38 | + for key in expected: |
| 39 | + self.assertTrue(key in actual) |
| 40 | + |
| 41 | + def test_title_info_correct(self): |
| 42 | + for book_id, expected in self.db.items(): |
| 43 | + actual = self.makeOne().title_info(book_id) |
| 44 | + self.assertEqual(actual, expected) |
| 45 | + |
| 46 | + |
| 47 | +class ResolvePathTestCase(BookAppTestCase): |
| 48 | + """tests for the resolve_path function""" |
| 49 | + |
| 50 | + def call_function_under_test(self, path): |
| 51 | + from bookapp import resolve_path |
| 52 | + return resolve_path(path) |
| 53 | + |
| 54 | + def test_root_returns_books_function(self): |
| 55 | + """verify that the correct function is returned by the root path""" |
| 56 | + from bookapp import books as expected |
| 57 | + path = '/' |
| 58 | + actual, args = self.call_function_under_test(path) |
| 59 | + self.assertTrue(actual is expected) |
| 60 | + |
| 61 | + def test_root_returns_no_args(self): |
| 62 | + """verify that no args are returned for the root path""" |
| 63 | + path = '/' |
| 64 | + func, actual = self.call_function_under_test(path) |
| 65 | + self.assertTrue(not actual) |
| 66 | + |
| 67 | + def test_book_path_returns_book_function(self): |
| 68 | + from bookapp import book as expected |
| 69 | + book_id = self.db.keys()[0] |
| 70 | + path = '/book/{0}'.format(book_id) |
| 71 | + actual, args = self.call_function_under_test(path) |
| 72 | + self.assertTrue(actual is expected) |
| 73 | + |
| 74 | + def test_book_path_returns_bookid_in_args(self): |
| 75 | + expected = self.db.keys()[0] |
| 76 | + path = '/book/{0}'.format(expected) |
| 77 | + func, actual = self.call_function_under_test(path) |
| 78 | + self.assertTrue(expected in actual) |
| 79 | + |
| 80 | + def test_bad_path_raises_name_error(self): |
| 81 | + path = '/not/valid/path' |
| 82 | + self.assertRaises(NameError, self.call_function_under_test, path) |
| 83 | + |
| 84 | + |
| 85 | +class BooksTestCase(BookAppTestCase): |
| 86 | + """tests for the books function""" |
| 87 | + |
| 88 | + def call_function_under_test(self): |
| 89 | + from bookapp import books |
| 90 | + return books() |
| 91 | + |
| 92 | + def test_all_book_titles_in_result(self): |
| 93 | + actual = self.call_function_under_test() |
| 94 | + for book_id, info in self.db.items(): |
| 95 | + expected = info['title'] |
| 96 | + self.assertTrue(expected in actual) |
| 97 | + |
| 98 | + def test_all_book_ids_in_result(self): |
| 99 | + actual = self.call_function_under_test() |
| 100 | + for expected in self.db: |
| 101 | + self.assertTrue(expected in actual) |
| 102 | + |
| 103 | + |
| 104 | +class BookTestCase(BookAppTestCase): |
| 105 | + """tests for the book function""" |
| 106 | + |
| 107 | + def call_function_under_test(self, id): |
| 108 | + from bookapp import book |
| 109 | + return book(id) |
| 110 | + |
| 111 | + def test_all_ids_have_results(self): |
| 112 | + for book_id in self.db: |
| 113 | + actual = self.call_function_under_test(book_id) |
| 114 | + self.assertTrue(actual) |
| 115 | + |
| 116 | + def test_id_returns_correct_results(self): |
| 117 | + for book_id, book_info in self.db.items(): |
| 118 | + actual = self.call_function_under_test(book_id) |
| 119 | + for expected in book_info.values(): |
| 120 | + self.assertTrue(expected in actual) |
| 121 | + |
| 122 | + def test_bad_id_raises_name_error(self): |
| 123 | + bad_id = "sponge" |
| 124 | + self.assertRaises(NameError, self.call_function_under_test, bad_id) |
| 125 | + |
| 126 | + |
| 127 | +if __name__ == '__main__': |
| 128 | + unittest.main() |
0 commit comments