1
1
import pytest
2
-
3
- INVENTORY_TEXT = """
4
- apple, 0.60
5
- banana, 0.20
6
- grapefruit, 0.75
7
- grapes, 1.99
8
- kiwi, 0.50
9
- lemon, 0.20
10
- lime, 0.25
11
- mango, 1.50
12
- papaya, 2.95
13
- pineapple, 3.50
14
- blueberries, 1.99
15
- blackberries, 2.50
16
- peach, 0.50
17
- plum, 0.33
18
- clementine, 0.25
19
- cantaloupe, 3.25
20
- pear, 1.25
21
- quince, 0.45
22
- orange, 0.60
23
- """
24
-
25
- # this will be a global -- convention is all caps
26
- INVENTORY = {}
27
- for line in INVENTORY_TEXT .splitlines ():
28
- if line .strip () == "" :
29
- continue
30
- item , price = line .split ("," )
31
- INVENTORY [item ] = float (price )
32
-
33
- class Item :
34
- """ an item to buy """
35
-
36
- def __init__ (self , name , quantity = 1 ):
37
- """keep track of an item that is in our inventory"""
38
- if name not in INVENTORY :
39
- raise ValueError ("invalid item name" )
40
- self .name = name
41
- self .quantity = quantity
42
-
43
- def __repr__ (self ):
44
- return f"{ self .name } : { self .quantity } "
45
-
46
- def __eq__ (self , other ):
47
- """check if the items have the same name"""
48
- return self .name == other .name
49
-
50
- def __add__ (self , other ):
51
- """add two items together if they are the same type"""
52
- if self .name == other .name :
53
- return Item (self .name , self .quantity + other .quantity )
54
- else :
55
- raise ValueError ("names don't match" )
2
+ import shopping_cart
56
3
57
4
58
5
@pytest .fixture
59
6
def a ():
60
- return Item ("apple" , 10 )
7
+ return shopping_cart . Item ("apple" , 10 )
61
8
62
9
@pytest .fixture
63
10
def b ():
64
- return Item ("banana" , 20 )
11
+ return shopping_cart . Item ("banana" , 20 )
65
12
66
13
@pytest .fixture
67
14
def c ():
68
- return Item ("apple" , 20 )
15
+ return shopping_cart . Item ("apple" , 20 )
69
16
70
17
def test_add (a , c ):
71
18
# modifies a
@@ -88,4 +35,4 @@ def test_invalid_add(a, b):
88
35
89
36
def test_invalid_name ():
90
37
with pytest .raises (ValueError , match = "invalid item name" ):
91
- d = Item ("dog" )
38
+ d = shopping_cart . Item ("dog" )
0 commit comments