1
exercism fetch python book-store

book_store_test.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
import unittest

from book_store import calculate_total


# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0

class BookStoreTests(unittest.TestCase):
    def test_only_a_single_book(self):
        self.assertAlmostEqual(calculate_total([1]), 8.00,
                               places=2)

    def test_two_of_the_same_book(self):
        self.assertAlmostEqual(calculate_total([2, 2]), 16.00,
                               places=2)

    def test_empty_basket(self):
        self.assertAlmostEqual(calculate_total([]), 0.00,
                               places=2)

    def test_two_different_books(self):
        self.assertAlmostEqual(calculate_total([1, 2]), 15.20,
                               places=2)

    def test_three_different_books(self):
        self.assertAlmostEqual(calculate_total([1, 2, 3]), 21.60,
                               places=2)

    def test_four_different_books(self):
        self.assertAlmostEqual(calculate_total([1, 2, 3, 4]), 25.60,
                               places=2)

    def test_five_different_books(self):
        self.assertAlmostEqual(
            calculate_total([1, 2, 3, 4, 5]), 30.00,
            places=2)

    def test_two_groups_of_4_is_cheaper_than_group_of_5_plus_group_of_3(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 5]), 51.20,
            places=2)

    def test_group_of_4_plus_group_of_2_is_cheaper_than_2_groups_of_3(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 4]), 40.80,
            places=2)

    def test_two_each_of_first_4_books_and_1_copy_each_of_rest(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5]), 55.60,
            places=2)

    def test_two_copies_of_each_book(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5]), 60.00,
            places=2)

    def test_three_copies_of_first_book_and_2_each_of_remaining(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1]),
            68.00,
            places=2)

    def test_three_each_of_first_2_books_and_2_each_of_remaining_books(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 1, 2]),
            75.20,
            places=2)

    def test_four_groups_of_4_are_cheaper_than_2_groups_each_of_5_and_3(self):
        self.assertAlmostEqual(
            calculate_total([1, 1, 2, 2, 3, 3, 4, 5, 1, 1, 2, 2, 3, 3, 4, 5]),
            102.40,
            places=2)


if __name__ == '__main__':
    unittest.main()