Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3097aa9

Browse files
committed
update test_item
1 parent 2e295f1 commit 3097aa9

File tree

2 files changed

+5
-64
lines changed

2 files changed

+5
-64
lines changed

examples/testing/nose/simple/simple_example.py

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 5 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,18 @@
11
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
563

574

585
@pytest.fixture
596
def a():
60-
return Item("apple", 10)
7+
return shopping_cart.Item("apple", 10)
618

629
@pytest.fixture
6310
def b():
64-
return Item("banana", 20)
11+
return shopping_cart.Item("banana", 20)
6512

6613
@pytest.fixture
6714
def c():
68-
return Item("apple", 20)
15+
return shopping_cart.Item("apple", 20)
6916

7017
def test_add(a, c):
7118
# modifies a
@@ -88,4 +35,4 @@ def test_invalid_add(a, b):
8835

8936
def test_invalid_name():
9037
with pytest.raises(ValueError, match="invalid item name"):
91-
d = Item("dog")
38+
d = shopping_cart.Item("dog")

0 commit comments

Comments
 (0)