|
17 | 17 |
|
18 | 18 | # First-Party |
19 | 19 | from cforge.commands.settings.profiles import ( |
| 20 | + profiles_create, |
20 | 21 | profiles_current, |
21 | 22 | profiles_get, |
22 | 23 | profiles_list, |
@@ -539,3 +540,88 @@ def test_profiles_current_no_environment(self, mock_console, mock_settings) -> N |
539 | 540 | assert any("Current Profile" in call for call in print_calls) |
540 | 541 | assert any("current@example.com" in call for call in print_calls) |
541 | 542 | assert not any("Environment:" in call for call in print_calls) |
| 543 | + |
| 544 | + |
| 545 | +class TestProfilesCreate: |
| 546 | + """Tests for profiles create command.""" |
| 547 | + |
| 548 | + def test_profiles_create_success(self, mock_console, mock_settings) -> None: |
| 549 | + """Test successfully creating a new profile.""" |
| 550 | + with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console): |
| 551 | + with patch("cforge.commands.settings.profiles.prompt_for_schema") as mock_prompt: |
| 552 | + with patch("cforge.commands.settings.profiles.typer.confirm", return_value=False): |
| 553 | + # Mock the prompt to return profile data |
| 554 | + mock_prompt.return_value = { |
| 555 | + "id": "test-profile-id", |
| 556 | + "name": "Test Profile", |
| 557 | + "email": "test@example.com", |
| 558 | + "api_url": "https://api.test.com", |
| 559 | + "is_active": False, |
| 560 | + "created_at": datetime.now(), |
| 561 | + } |
| 562 | + |
| 563 | + profiles_create() |
| 564 | + |
| 565 | + # Verify success message |
| 566 | + print_calls = [str(call) for call in mock_console.print.call_args_list] |
| 567 | + assert any("Profile created successfully" in call for call in print_calls) |
| 568 | + assert any("Test Profile" in call for call in print_calls) |
| 569 | + |
| 570 | + def test_profiles_create_and_enable(self, mock_console, mock_settings) -> None: |
| 571 | + """Test creating a profile and enabling it.""" |
| 572 | + with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console): |
| 573 | + with patch("cforge.commands.settings.profiles.prompt_for_schema") as mock_prompt: |
| 574 | + with patch("cforge.commands.settings.profiles.typer.confirm", return_value=True): |
| 575 | + with patch("cforge.commands.settings.profiles.set_active_profile", return_value=True) as set_active_profile_mock: |
| 576 | + with patch("cforge.commands.settings.profiles.get_settings") as mock_get_settings: |
| 577 | + mock_get_settings.cache_clear = Mock() |
| 578 | + |
| 579 | + # Mock the prompt to return profile data |
| 580 | + mock_prompt.return_value = { |
| 581 | + "id": "test-profile-id", |
| 582 | + "name": "Test Profile", |
| 583 | + "email": "test@example.com", |
| 584 | + "api_url": "https://api.test.com", |
| 585 | + "is_active": False, |
| 586 | + "created_at": datetime.now(), |
| 587 | + } |
| 588 | + |
| 589 | + profiles_create() |
| 590 | + |
| 591 | + # Verify success and enable messages |
| 592 | + print_calls = [str(call) for call in mock_console.print.call_args_list] |
| 593 | + assert any("Profile created successfully" in call for call in print_calls) |
| 594 | + assert any("Profile enabled" in call for call in print_calls) |
| 595 | + set_active_profile_mock.assert_called_with("test-profile-id") |
| 596 | + |
| 597 | + def test_profiles_create_error(self, mock_console, mock_settings) -> None: |
| 598 | + """Test creating profile with an error.""" |
| 599 | + with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console): |
| 600 | + with patch("cforge.commands.settings.profiles.prompt_for_schema", side_effect=Exception("Test error")): |
| 601 | + with pytest.raises(typer.Exit) as exc_info: |
| 602 | + profiles_create() |
| 603 | + |
| 604 | + assert exc_info.value.exit_code == 1 |
| 605 | + assert any("Error creating profile" in str(call) for call in mock_console.print.call_args_list) |
| 606 | + |
| 607 | + def test_profiles_create_enable_fails(self, mock_console, mock_settings) -> None: |
| 608 | + """Test creating profile but enabling fails.""" |
| 609 | + with patch("cforge.commands.settings.profiles.get_console", return_value=mock_console): |
| 610 | + with patch("cforge.commands.settings.profiles.prompt_for_schema") as mock_prompt: |
| 611 | + with patch("cforge.commands.settings.profiles.typer.confirm", return_value=True): |
| 612 | + with patch("cforge.commands.settings.profiles.set_active_profile", return_value=False): |
| 613 | + # Mock the prompt to return profile data |
| 614 | + mock_prompt.return_value = { |
| 615 | + "id": "test-profile-id", |
| 616 | + "name": "Test Profile", |
| 617 | + "email": "test@example.com", |
| 618 | + "api_url": "https://api.test.com", |
| 619 | + "is_active": False, |
| 620 | + "created_at": datetime.now(), |
| 621 | + } |
| 622 | + |
| 623 | + profiles_create() |
| 624 | + |
| 625 | + # Verify failure message |
| 626 | + print_calls = [str(call) for call in mock_console.print.call_args_list] |
| 627 | + assert any("Failed to enable profile" in call for call in print_calls) |
0 commit comments