File size: 7,942 Bytes
9d4bd7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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"]