Skip to content

Commit 51d9f86

Browse files
author
boraxpr
committed
bite 32
1 parent c5ff761 commit 51d9f86

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

32/inventory.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import copy
2+
items = [{'id': 1, 'name': 'laptop', 'value': 1000},
3+
{'id': 2, 'name': 'chair', 'value': 300},
4+
{'id': 3, 'name': 'book', 'value': 20}]
5+
6+
7+
def duplicate_items(items):
8+
items_copy = copy.deepcopy(items)
9+
return items_copy[:]
10+
11+
# duplicate_items(items)

32/test_inventory.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from inventory import items, duplicate_items
2+
3+
4+
def test_change_copy_only():
5+
items_copy = duplicate_items(items)
6+
assert items == items_copy
7+
8+
# modify the copy
9+
items_copy[0]['name'] = 'macbook'
10+
items_copy[1]['id'] = 4
11+
items_copy[2]['value'] = 30
12+
13+
# only copy should have been updated, check original items values
14+
assert items[0]['name'] == 'laptop'
15+
assert items[1]['id'] == 2
16+
assert items[2]['value'] == 20
17+
18+
19+
test_change_copy_only()

0 commit comments

Comments
 (0)