|
5 | 5 | from unittest.mock import Mock |
6 | 6 | from handler.stock import StockService |
7 | 7 | from usecase.base import AbstractStockUsecase |
8 | | -from domain.stock import CreateStock, Stock |
| 8 | +from domain.stock import CreateStock, Stock, StockInfo |
9 | 9 | from domain.portfolio import PortfolioInfo |
10 | 10 | from domain.enum import ActionType, StockType |
11 | 11 |
|
@@ -318,3 +318,126 @@ def test_internal_error(self, mock_stock_usecase, mock_context, valid_request): |
318 | 318 | mock_context.set_code.assert_called_once_with(grpc.StatusCode.INTERNAL) |
319 | 319 | mock_context.set_details.assert_called_once_with("Internal server error") |
320 | 320 | mock_stock_usecase.get_portfolio_info.assert_called_once_with(user_id=1) |
| 321 | + |
| 322 | + |
| 323 | +class TestStockServiceGetStockInfo: |
| 324 | + # Fixture to create a mock stock_usecase |
| 325 | + @pytest.fixture |
| 326 | + def mock_stock_usecase(self): |
| 327 | + usecase = Mock(spec=AbstractStockUsecase) |
| 328 | + usecase.get_stock_info.return_value = { |
| 329 | + StockType.STOCKS.value: [ |
| 330 | + StockInfo( |
| 331 | + symbol="AAPL", |
| 332 | + quantity=10, |
| 333 | + price=100.0, |
| 334 | + avg_cost=95.0, |
| 335 | + percentage=5.26, |
| 336 | + ), |
| 337 | + StockInfo( |
| 338 | + symbol="GOOGL", |
| 339 | + quantity=5, |
| 340 | + price=1500.0, |
| 341 | + avg_cost=1400.0, |
| 342 | + percentage=7.14, |
| 343 | + ), |
| 344 | + ], |
| 345 | + StockType.ETF.value: [ |
| 346 | + StockInfo( |
| 347 | + symbol="SPY", |
| 348 | + quantity=20, |
| 349 | + price=400.0, |
| 350 | + avg_cost=390.0, |
| 351 | + percentage=2.56, |
| 352 | + ), |
| 353 | + ], |
| 354 | + "CASH": [ |
| 355 | + StockInfo( |
| 356 | + symbol="USD", |
| 357 | + quantity=1, |
| 358 | + price=1000.0, |
| 359 | + avg_cost=1.0, |
| 360 | + percentage=0.0, |
| 361 | + ), |
| 362 | + ], |
| 363 | + } |
| 364 | + return usecase |
| 365 | + |
| 366 | + # Fixture to create a mock gRPC context |
| 367 | + @pytest.fixture |
| 368 | + def mock_context(self): |
| 369 | + context = Mock() |
| 370 | + context.set_code = Mock() |
| 371 | + context.set_details = Mock() |
| 372 | + return context |
| 373 | + |
| 374 | + # Fixture to create a valid gRPC request |
| 375 | + @pytest.fixture |
| 376 | + def valid_request(self): |
| 377 | + request = Mock() |
| 378 | + request.user_id = 1 |
| 379 | + return request |
| 380 | + |
| 381 | + def test_success(self, mock_stock_usecase, mock_context, valid_request): |
| 382 | + # Arrange |
| 383 | + service = StockService(mock_stock_usecase) |
| 384 | + |
| 385 | + expected_result = stock_pb2.GetStockInfoResp( |
| 386 | + stocks=[ |
| 387 | + stock_pb2.StockInfo( |
| 388 | + symbol="AAPL", |
| 389 | + quantity=10, |
| 390 | + price=100.0, |
| 391 | + avg_cost=95.0, |
| 392 | + percentage=5.26, |
| 393 | + ), |
| 394 | + stock_pb2.StockInfo( |
| 395 | + symbol="GOOGL", |
| 396 | + quantity=5, |
| 397 | + price=1500.0, |
| 398 | + avg_cost=1400.0, |
| 399 | + percentage=7.14, |
| 400 | + ), |
| 401 | + ], |
| 402 | + etf=[ |
| 403 | + stock_pb2.StockInfo( |
| 404 | + symbol="SPY", |
| 405 | + quantity=20, |
| 406 | + price=400.0, |
| 407 | + avg_cost=390.0, |
| 408 | + percentage=2.56, |
| 409 | + ), |
| 410 | + ], |
| 411 | + cash=[ |
| 412 | + stock_pb2.StockInfo( |
| 413 | + symbol="USD", |
| 414 | + quantity=1, |
| 415 | + price=1000.0, |
| 416 | + avg_cost=1.0, |
| 417 | + percentage=0.0, |
| 418 | + ), |
| 419 | + ], |
| 420 | + ) |
| 421 | + |
| 422 | + # Action |
| 423 | + response = service.GetStockInfo(valid_request, mock_context) |
| 424 | + |
| 425 | + # Assertion |
| 426 | + assert isinstance(response, stock_pb2.GetStockInfoResp) |
| 427 | + assert response == expected_result |
| 428 | + mock_stock_usecase.get_stock_info.assert_called_once_with(user_id=1) |
| 429 | + mock_context.set_code.assert_not_called() |
| 430 | + mock_context.set_details.assert_not_called() |
| 431 | + |
| 432 | + def test_internal_error(self, mock_stock_usecase, mock_context, valid_request): |
| 433 | + # Arrange |
| 434 | + service = StockService(mock_stock_usecase) |
| 435 | + mock_stock_usecase.get_stock_info.side_effect = Exception("Database error") # Simulate internal error |
| 436 | + |
| 437 | + # Act/Assertion |
| 438 | + with pytest.raises(grpc.RpcError) as exc_info: |
| 439 | + service.GetStockInfo(valid_request, mock_context) |
| 440 | + assert str(exc_info.value) == "Internal server error" |
| 441 | + mock_context.set_code.assert_called_once_with(grpc.StatusCode.INTERNAL) |
| 442 | + mock_context.set_details.assert_called_once_with("Internal server error") |
| 443 | + mock_stock_usecase.get_stock_info.assert_called_once_with(user_id=1) |
0 commit comments