Skip to content

Commit e793e18

Browse files
test: Add comprehensive doctests for stock_price function
1 parent a71618f commit e793e18

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

web_programming/current_stock_price.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,27 @@
2222

2323
def stock_price(symbol: str = "AAPL") -> str:
2424
"""
25-
>>> stock_price("EEEE")
26-
'No <fin-streamer> tag with the specified data-testid attribute found.'
27-
>>> isinstance(float(stock_price("GOOG")),float)
28-
True
25+
Get current stock price from Yahoo Finance.
26+
27+
Args:
28+
symbol: Stock ticker symbol (e.g., 'AAPL', 'GOOG')
29+
30+
Returns:
31+
Current stock price as string, or error message if not found
32+
33+
Examples:
34+
>>> stock_price("EEEE") # Invalid symbol
35+
'No <fin-streamer> tag with the specified data-testid attribute found.'
36+
>>> isinstance(float(stock_price("GOOG")), float) # Valid symbol
37+
True
38+
>>> stock_price("") # Empty symbol
39+
'No <fin-streamer> tag with the specified data-testid attribute found.'
40+
>>> stock_price("INVALID_SYMBOL_123") # Another invalid symbol
41+
'No <fin-streamer> tag with the specified data-testid attribute found.'
42+
>>> # Test output format for valid symbols
43+
>>> result = stock_price("AAPL")
44+
>>> result.replace(".", "").replace(",", "").isdigit() or "No <fin-streamer>" in result
45+
True
2946
"""
3047
url = f"https://finance.yahoo.com/quote/{symbol}?p={symbol}"
3148
yahoo_finance_source = httpx.get(
@@ -45,4 +62,4 @@ def stock_price(symbol: str = "AAPL") -> str:
4562
testmod()
4663

4764
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
48-
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")
65+
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")

0 commit comments

Comments
 (0)