-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
100 lines (67 loc) · 3.26 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
93
94
95
96
97
98
99
100
import unittest
from decimal import Decimal
from implementation import Cart
def get_catalog():
return {
'Lechuga corazón romana': Decimal('1.00'),
'Patas de pulpo cocido': Decimal('8.95'),
'Salmón marinado Hacendado': Decimal('2.35'),
}
class TestCartTotalPrice(unittest.TestCase):
def test_returns_zero_when_cart_empty(self):
cart = Cart(catalog=get_catalog())
self.assertEqual(cart.total_price(), Decimal('0.00'))
def test_returns_sum_of_prices_when_quantity_greater_than_one(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana', quantity=2)
self.assertEqual(cart.total_price(), Decimal('2.00'))
def test_returns_sum_of_prices_when_multiple_products_in_cart(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana')
cart.add('Salmón marinado Hacendado')
self.assertEqual(cart.total_price(), Decimal('3.35'))
class TestCartAdd(unittest.TestCase):
def test_adds_qty_one_when_product_in_catalog_and_no_qty_given(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana')
self.assertEqual(cart.total_price(), Decimal('1.00'))
def test_adds_given_qty_when_product_in_catalog_and_qty_given(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana', quantity=3)
self.assertEqual(cart.total_price(), Decimal('3.00'))
def test_sums_qtys_when_adding_multiple_times(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana')
cart.add('Lechuga corazón romana')
self.assertEqual(cart.total_price(), Decimal('2.00'))
def test_raises_valueerror_when_product_not_in_catalog(self):
cart = Cart(catalog=get_catalog())
with self.assertRaises(ValueError):
cart.add('no existo')
class TestCartRemove(unittest.TestCase):
def test_removes_qty_one_when_product_in_cart_and_no_qty_given(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana', quantity=2)
cart.remove('Lechuga corazón romana')
self.assertEqual(cart.total_price(), Decimal('1.00'))
def test_removes_product_when_last_remaining_quantity(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana')
cart.remove('Lechuga corazón romana')
self.assertEqual(cart.total_price(), Decimal('0.00'))
def test_removes_given_qty_when_product_in_cart_and_qty_given(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana', quantity=3)
cart.remove('Lechuga corazón romana', quantity=2)
self.assertEqual(cart.total_price(), Decimal('1.00'))
def test_raises_valueerror_when_product_not_in_cart(self):
cart = Cart(catalog=get_catalog())
with self.assertRaises(ValueError):
cart.remove('Lechuga corazón romana')
def test_raises_valueerror_when_given_qty_greater_than_in_cart(self):
cart = Cart(catalog=get_catalog())
cart.add('Lechuga corazón romana', quantity=2)
with self.assertRaises(ValueError):
cart.remove('Lechuga corazón romana', quantity=3)
if __name__ == '__main__':
unittest.main()