|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 | import responses |
11 | | -from sahmk.cli import main, _build_parser, _resolve_api_key, _print_json |
| 11 | +from sahmk.cli import main, _build_parser, _resolve_api_key, _print_json, _run_stream |
12 | 12 | from sahmk.client import SahmkError |
13 | 13 |
|
14 | 14 |
|
@@ -453,8 +453,169 @@ def test_compact_output(self, capsys, sample_quote_response): |
453 | 453 | ) |
454 | 454 |
|
455 | 455 | exit_code = main(["--api-key", "test_key", "--compact", "quote", "2222"]) |
456 | | - |
| 456 | + |
457 | 457 | assert exit_code == 0 |
458 | 458 | captured = capsys.readouterr() |
459 | 459 | # Compact output should not have indentation |
460 | 460 | assert "\n " not in captured.out |
| 461 | + |
| 462 | + |
| 463 | +class TestMainCompanyCommand: |
| 464 | + """Tests for main function with company command.""" |
| 465 | + |
| 466 | + @responses.activate |
| 467 | + def test_company_success(self, capsys, sample_company_response, monkeypatch): |
| 468 | + """Test successful company command.""" |
| 469 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 470 | + responses.add( |
| 471 | + responses.GET, |
| 472 | + "https://app.sahmk.sa/api/v1/company/2222/", |
| 473 | + json=sample_company_response, |
| 474 | + status=200, |
| 475 | + ) |
| 476 | + |
| 477 | + exit_code = main(["company", "2222"]) |
| 478 | + |
| 479 | + assert exit_code == 0 |
| 480 | + captured = capsys.readouterr() |
| 481 | + assert "2222" in captured.out |
| 482 | + assert "Saudi Arabian Oil Company" in captured.out |
| 483 | + |
| 484 | + |
| 485 | +class TestMainFinancialsCommand: |
| 486 | + """Tests for main function with financials command.""" |
| 487 | + |
| 488 | + @responses.activate |
| 489 | + def test_financials_success(self, capsys, sample_financials_response, monkeypatch): |
| 490 | + """Test successful financials command.""" |
| 491 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 492 | + responses.add( |
| 493 | + responses.GET, |
| 494 | + "https://app.sahmk.sa/api/v1/financials/2222/", |
| 495 | + json=sample_financials_response, |
| 496 | + status=200, |
| 497 | + ) |
| 498 | + |
| 499 | + exit_code = main(["financials", "2222"]) |
| 500 | + |
| 501 | + assert exit_code == 0 |
| 502 | + captured = capsys.readouterr() |
| 503 | + assert "income_statement" in captured.out |
| 504 | + |
| 505 | + |
| 506 | +class TestMainDividendsCommand: |
| 507 | + """Tests for main function with dividends command.""" |
| 508 | + |
| 509 | + @responses.activate |
| 510 | + def test_dividends_success(self, capsys, sample_dividends_response, monkeypatch): |
| 511 | + """Test successful dividends command.""" |
| 512 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 513 | + responses.add( |
| 514 | + responses.GET, |
| 515 | + "https://app.sahmk.sa/api/v1/dividends/2222/", |
| 516 | + json=sample_dividends_response, |
| 517 | + status=200, |
| 518 | + ) |
| 519 | + |
| 520 | + exit_code = main(["dividends", "2222"]) |
| 521 | + |
| 522 | + assert exit_code == 0 |
| 523 | + captured = capsys.readouterr() |
| 524 | + assert "dividend_yield" in captured.out |
| 525 | + |
| 526 | + |
| 527 | +class TestMainEventsCommand: |
| 528 | + """Tests for main function with events command.""" |
| 529 | + |
| 530 | + @responses.activate |
| 531 | + def test_events_success(self, capsys, sample_events_response, monkeypatch): |
| 532 | + """Test successful events command.""" |
| 533 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 534 | + responses.add( |
| 535 | + responses.GET, |
| 536 | + "https://app.sahmk.sa/api/v1/events/", |
| 537 | + json=sample_events_response, |
| 538 | + status=200, |
| 539 | + ) |
| 540 | + |
| 541 | + exit_code = main(["events"]) |
| 542 | + |
| 543 | + assert exit_code == 0 |
| 544 | + captured = capsys.readouterr() |
| 545 | + assert "events" in captured.out |
| 546 | + |
| 547 | + @responses.activate |
| 548 | + def test_events_with_symbol(self, capsys, sample_events_response, monkeypatch): |
| 549 | + """Test events command with --symbol filter.""" |
| 550 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 551 | + responses.add( |
| 552 | + responses.GET, |
| 553 | + "https://app.sahmk.sa/api/v1/events/", |
| 554 | + json=sample_events_response, |
| 555 | + status=200, |
| 556 | + ) |
| 557 | + |
| 558 | + exit_code = main(["events", "--symbol", "2222"]) |
| 559 | + |
| 560 | + assert exit_code == 0 |
| 561 | + request = responses.calls[0].request |
| 562 | + assert "symbol=2222" in request.url |
| 563 | + |
| 564 | + @responses.activate |
| 565 | + def test_events_with_limit(self, capsys, sample_events_response, monkeypatch): |
| 566 | + """Test events command with --limit.""" |
| 567 | + monkeypatch.setenv("SAHMK_API_KEY", "test_key") |
| 568 | + responses.add( |
| 569 | + responses.GET, |
| 570 | + "https://app.sahmk.sa/api/v1/events/", |
| 571 | + json=sample_events_response, |
| 572 | + status=200, |
| 573 | + ) |
| 574 | + |
| 575 | + exit_code = main(["events", "--limit", "5"]) |
| 576 | + |
| 577 | + assert exit_code == 0 |
| 578 | + request = responses.calls[0].request |
| 579 | + assert "limit=5" in request.url |
| 580 | + |
| 581 | + |
| 582 | +class TestParserNewCommands: |
| 583 | + """Tests for parsing new CLI commands.""" |
| 584 | + |
| 585 | + def test_parser_company_command(self): |
| 586 | + parser = _build_parser() |
| 587 | + args = parser.parse_args(["company", "2222"]) |
| 588 | + assert args.command == "company" |
| 589 | + assert args.symbol == "2222" |
| 590 | + |
| 591 | + def test_parser_financials_command(self): |
| 592 | + parser = _build_parser() |
| 593 | + args = parser.parse_args(["financials", "2222"]) |
| 594 | + assert args.command == "financials" |
| 595 | + assert args.symbol == "2222" |
| 596 | + |
| 597 | + def test_parser_dividends_command(self): |
| 598 | + parser = _build_parser() |
| 599 | + args = parser.parse_args(["dividends", "2222"]) |
| 600 | + assert args.command == "dividends" |
| 601 | + assert args.symbol == "2222" |
| 602 | + |
| 603 | + def test_parser_events_command(self): |
| 604 | + parser = _build_parser() |
| 605 | + args = parser.parse_args(["events", "--symbol", "2222", "--limit", "10"]) |
| 606 | + assert args.command == "events" |
| 607 | + assert args.symbol == "2222" |
| 608 | + assert args.limit == 10 |
| 609 | + |
| 610 | + def test_parser_events_no_args(self): |
| 611 | + parser = _build_parser() |
| 612 | + args = parser.parse_args(["events"]) |
| 613 | + assert args.command == "events" |
| 614 | + assert args.symbol is None |
| 615 | + assert args.limit is None |
| 616 | + |
| 617 | + def test_parser_stream_command(self): |
| 618 | + parser = _build_parser() |
| 619 | + args = parser.parse_args(["stream", "2222,1120"]) |
| 620 | + assert args.command == "stream" |
| 621 | + assert args.symbols == "2222,1120" |
0 commit comments