|
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: |
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
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" |
|
|
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
await async_client.post("http://localhost:8001/stocks/import/CRDB") |
|
|
|
|
|
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 |
|
|
|
|
|
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"] |
|
|
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
await async_client.post("http://localhost:8001/utt/import-all") |
|
|
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
await async_client.post("http://localhost:8001/bonds/import-all") |
|
|
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
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_response = await async_client.post("http://localhost:8001/portfolio/", json={ |
|
"name": "Test Portfolio" |
|
}, params={"user_id": 33}) |
|
portfolio_id = create_response.json()["data"]["id"] |
|
|
|
|
|
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"] |
|
|
|
|
|
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" |
|
|
|
|
|
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"] |