XYHLF / tests /test_portfolio.py
Mbonea's picture
initial commit
9d4bd7c
import pytest
import httpx
pytest_plugins = ["pytest_asyncio"]
@pytest.mark.asyncio
async def test_create_portfolio(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Test successful creation
response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio",
"description": "Test portfolio description"
}, params={"user_id":33 })
assert response.status_code == 200
data = response.json()
assert data["success"] == True
assert data["message"] == "Portfolio created successfully"
assert data["data"]["name"] == "Test Portfolio"
assert data["data"]["description"] == "Test portfolio description"
assert "id" in data["data"]
assert "user_id" in data["data"]
@pytest.mark.asyncio
async def test_get_portfolio(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create a portfolio first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Test successful retrieval
response = await async_client.get(f"http://localhost:8001/portfolio/{portfolio_id}")
assert response.status_code == 200
data = response.json()
assert data["success"] == True
assert data["message"] == "Portfolio retrieved successfully"
# Test not found
response = await async_client.get("http://localhost:8001/portfolio/99999")
assert response.status_code == 404
data = response.json()
assert data["success"] == False
assert "Portfolio not found" in data["detail"]
@pytest.mark.asyncio
async def test_add_stock(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create portfolio first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Import a test stock
await async_client.post("http://localhost:8001/stocks/import/CRDB")
# Test successful addition
response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/stocks", json={
"stock_id": 1,
"quantity": 100,
"purchase_price": 1000,
"purchase_date": "2023-01-01"
})
assert response.status_code == 200
data = response.json()
assert data["success"] == True
assert data["message"] == "Stock added to portfolio successfully"
assert data["data"]["quantity"] == 100
# Test invalid portfolio
response = await async_client.post("http://localhost:8001/portfolio/99999/stocks", json={
"stock_id": 1,
"quantity": 100,
"purchase_price": 1000,
"purchase_date": "2023-01-01"
})
assert response.status_code == 404
assert "Portfolio not found" in response.json()["detail"]
# Test invalid stock
response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/stocks", json={
"stock_id": 99999,
"quantity": 100,
"purchase_price": 1000,
"purchase_date": "2023-01-01"
})
assert response.status_code == 404
assert "Stock not found" in response.json()["detail"]
@pytest.mark.asyncio
async def test_add_utt(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create portfolio first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Import UTT funds
await async_client.post("http://localhost:8001/utt/import-all")
# Test successful addition
response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/utts", json={
"utt_fund_id": 1,
"units": 100,
"purchase_price": 1000,
"purchase_date": "2023-01-01"
})
assert response.status_code == 200
data = response.json()
assert data["success"] == True
assert data["message"] == "UTT added to portfolio successfully"
assert data["data"]["units"] == 100
assert "id" in data["data"]
@pytest.mark.asyncio
async def test_add_bond(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create portfolio first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Import bonds
await async_client.post("http://localhost:8001/bonds/import-all")
# Test successful addition
response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/bonds", json={
"bond_id": 1,
"face_value": 10000,
"purchase_date": "2023-01-01",
"maturity_date": "2024-01-01"
})
assert response.status_code == 200
data = response.json()
assert "face_value" in data
assert data["face_value"] == 10000
@pytest.mark.asyncio
async def test_add_calendar_event(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create portfolio first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Test successful addition
response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/calendar", json={
"title": "Test Event",
"description": "Test event description",
"event_date": "2023-01-01",
"event_type": "dividend"
})
assert response.status_code == 200
data = response.json()
assert "title" in data
assert data["title"] == "Test Event"
@pytest.mark.asyncio
async def test_remove_items(client, initialize_tests):
async with httpx.AsyncClient() as async_client:
# Create portfolio and add items first
create_response = await async_client.post("http://localhost:8001/portfolio/", json={
"name": "Test Portfolio"
}, params={"user_id": 33})
portfolio_id = create_response.json()["data"]["id"]
# Add a stock first
await async_client.post("http://localhost:8001/stocks/import/CRDB")
stock_response = await async_client.post(f"http://localhost:8001/portfolio/{portfolio_id}/stocks", json={
"stock_id": 1,
"quantity": 100,
"purchase_price": 1000,
"purchase_date": "2023-01-01"
})
stock_id = stock_response.json()["data"]["id"]
# Test remove stock
response = await async_client.delete(f"http://localhost:8001/portfolio/{portfolio_id}/stocks/{stock_id}")
assert response.status_code == 200
assert response.json()["message"] == "Stock removed from portfolio"
# Test remove with invalid IDs
response = await async_client.delete(f"http://localhost:8001/portfolio/{portfolio_id}/stocks/99999")
assert response.status_code == 404
assert "Stock not found in portfolio" in response.json()["detail"]