~/cs2613/labs/L16
Exercise 2.2. Instead of using the REPL for this one, write functions =parse_row=, =compute_cost=, and =update_dict= so that the following tests pass.
def test_cost():
d = parse_row(row)
cost = compute_cost (d)
assert cost == pytest.approx(3220.000,abs=0.00000001)
def test_d():
d = parse_row(row)
update_dict(d)
assert d == {'name': 'AA', 'shares': 75, 'price':32.2, 'date': (6, 11, 2007),
'account': 12345}
read_portfolio
report.py
, and make sure they pass.def test_read():
portfolio = read_portfolio('Data/portfolio.csv')
assert portfolio == \
[('AA', 100, 32.2), ('IBM', 50, 91.1),
('CAT', 150, 83.44), ('MSFT', 200, 51.23),
('GE', 95, 40.37), ('MSFT', 50, 65.1), ('IBM', 100, 70.44)]
def test_total():
portfolio = read_portfolio('Data/portfolio.csv')
total = 0.0
for name, shares, price in portfolio:
total += shares*price
assert total == pytest.approx(44671.15,abs=0.001)
report2.py
Embed the following tests in report2.py
, and make sure they pass.def test_read():
portfolio = read_portfolio('Data/portfolio.csv')
assert portfolio == \
[{'name': 'AA', 'shares': 100, 'price': 32.2}, {'name': 'IBM', 'shares': 50, 'price': 91.1},
{'name': 'CAT', 'shares': 150, 'price': 83.44}, {'name': 'MSFT', 'shares': 200, 'price': 51.23},
{'name': 'GE', 'shares': 95, 'price': 40.37}, {'name': 'MSFT', 'shares': 50, 'price': 65.1},
{'name': 'IBM', 'shares': 100, 'price': 70.44}]
def test_total():
portfolio = read_portfolio('Data/portfolio.csv')
total = 0.0
for s in portfolio:
total += s['shares']*s['price']
assert total == pytest.approx(44671.15,abs=0.001)
Exercise 2.17. For this one build a script embedding the following tests.
Use list()
to fix the following test
def test_items():
assert prices.items() == \
[('GOOG', 490.1), ('AA', 23.45), ('IBM', 91.1), ('MSFT', 34.23)]
list()
being used for in this section?def test_zip():
assert pricelist == \
[(490.1, 'GOOG'), (23.45, 'AA'), (91.1, 'IBM'), (34.23, 'MSFT')]
def test_min_max():
assert min(pricelist) == (23.45, 'AA')
assert max(pricelist) == (490.1, 'GOOG')
Exercise 2.20. Start with the following skeleton, and convert the given examples into pytest tests.
import pytest
from report2 import read_portfolio
prices = {
'GOOG' : 490.1,
'AA' : 23.45,
'CAT': 35.46,
'IBM' : 91.1,
'MSFT' : 34.23,
'GE': 13.48,
}