A new endpoint has been added to the API. Your task is to write functional tests for it.
GET /stocks/{symbol}/history
- No authentication required
- Returns 5 months of price history for a given stock symbol
curl http://127.0.0.1:5000/stocks/AAPL/history{
"symbol": "AAPL",
"history": [
{ "date": "2024-01-01", "price": 149.18 },
{ "date": "2024-02-01", "price": 157.95 },
{ "date": "2024-03-01", "price": 166.73 },
{ "date": "2024-04-01", "price": 171.99 },
{ "date": "2024-05-01", "price": 175.50 }
]
}Open tests/functional/test_stock_history.py and implement the following 3 tests:
- Make a
GETrequest to/stocks/AAPL/history - Assert the response status code is
200 - Assert the response contains a
symbolfield equal to"AAPL" - Assert the response contains a
historyfield - Assert
historyis a list with more than 0 items - Assert each item in the list has a
dateandpricefield
- Make a
GETrequest to/stocks/INVALID/history - Assert the response status code is
404 - Assert the response contains an
errorfield
- Make a
GETrequest to/stocks/MSFT/historywith no API key - Assert the response status code is
200
Start the API server in one terminal:
python app.pyRun your tests in another terminal:
pytest tests/functional/test_stock_history.py -v- Use the
requestslibrary to make HTTP requests - A
GETrequest with no headers looks like:requests.get(f"{BASE_URL}/stocks/AAPL/history") - To check a field exists in a response:
assert "symbol" in response.json() - To check a list is not empty:
assert len(response.json()["history"]) > 0 - To check every item in a list has a field:
assert all("date" in item for item in history)