|
| 1 | +import pytest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import typer |
| 4 | + |
| 5 | +from tgit.config import config |
| 6 | + |
| 7 | + |
| 8 | +class TestConfig: |
| 9 | + """Test cases for the config module""" |
| 10 | + |
| 11 | + @patch("tgit.config.interactive_settings") |
| 12 | + def test_config_interactive_flag(self, mock_interactive_settings): |
| 13 | + """Test config with interactive flag""" |
| 14 | + config(key=None, value=None, interactive=True) |
| 15 | + |
| 16 | + mock_interactive_settings.assert_called_once() |
| 17 | + |
| 18 | + @patch("tgit.config.interactive_settings") |
| 19 | + def test_config_no_args_defaults_to_interactive(self, mock_interactive_settings): |
| 20 | + """Test config with no arguments defaults to interactive mode""" |
| 21 | + config(key=None, value=None, interactive=False) |
| 22 | + |
| 23 | + mock_interactive_settings.assert_called_once() |
| 24 | + |
| 25 | + @patch("tgit.config.print") |
| 26 | + def test_config_missing_key_only(self, mock_print): |
| 27 | + """Test config with missing key only""" |
| 28 | + with pytest.raises(typer.Exit) as exc_info: |
| 29 | + config(key=None, value="test", interactive=False) |
| 30 | + |
| 31 | + assert exc_info.value.exit_code == 1 |
| 32 | + mock_print.assert_any_call("Both key and value are required when not using interactive mode") |
| 33 | + mock_print.assert_any_call("Use --interactive or -i for interactive configuration") |
| 34 | + |
| 35 | + @patch("tgit.config.print") |
| 36 | + def test_config_missing_value_only(self, mock_print): |
| 37 | + """Test config with missing value only""" |
| 38 | + with pytest.raises(typer.Exit) as exc_info: |
| 39 | + config(key="apiKey", value=None, interactive=False) |
| 40 | + |
| 41 | + assert exc_info.value.exit_code == 1 |
| 42 | + mock_print.assert_any_call("Both key and value are required when not using interactive mode") |
| 43 | + mock_print.assert_any_call("Use --interactive or -i for interactive configuration") |
| 44 | + |
| 45 | + @patch("tgit.config.print") |
| 46 | + def test_config_invalid_key(self, mock_print): |
| 47 | + """Test config with invalid key""" |
| 48 | + with pytest.raises(typer.Exit) as exc_info: |
| 49 | + config(key="invalid_key", value="test", interactive=False) |
| 50 | + |
| 51 | + assert exc_info.value.exit_code == 1 |
| 52 | + available_keys = ["apiKey", "apiUrl", "model", "show_command", "skip_confirm"] |
| 53 | + mock_print.assert_called_once_with(f"Key invalid_key is not valid. Available keys: {', '.join(available_keys)}") |
| 54 | + |
| 55 | + @patch("tgit.config.set_global_settings") |
| 56 | + @patch("tgit.config.print") |
| 57 | + def test_config_valid_string_key(self, mock_print, mock_set_global_settings): |
| 58 | + """Test config with valid string key""" |
| 59 | + config(key="apiKey", value="test_key", interactive=False) |
| 60 | + |
| 61 | + mock_set_global_settings.assert_called_once_with("apiKey", "test_key") |
| 62 | + mock_print.assert_called_once_with("[green]Setting apiKey updated successfully![/green]") |
| 63 | + |
| 64 | + @patch("tgit.config.set_global_settings") |
| 65 | + @patch("tgit.config.print") |
| 66 | + def test_config_valid_model_key(self, mock_print, mock_set_global_settings): |
| 67 | + """Test config with valid model key""" |
| 68 | + config(key="model", value="gpt-4", interactive=False) |
| 69 | + |
| 70 | + mock_set_global_settings.assert_called_once_with("model", "gpt-4") |
| 71 | + mock_print.assert_called_once_with("[green]Setting model updated successfully![/green]") |
| 72 | + |
| 73 | + @patch("tgit.config.set_global_settings") |
| 74 | + @patch("tgit.config.print") |
| 75 | + def test_config_boolean_true_values(self, mock_print, mock_set_global_settings): |
| 76 | + """Test config with boolean true values""" |
| 77 | + true_values = ["true", "1", "yes", "on", "TRUE", "YES", "ON"] |
| 78 | + |
| 79 | + for value in true_values: |
| 80 | + mock_set_global_settings.reset_mock() |
| 81 | + mock_print.reset_mock() |
| 82 | + |
| 83 | + config(key="show_command", value=value, interactive=False) |
| 84 | + |
| 85 | + mock_set_global_settings.assert_called_once_with("show_command", True) |
| 86 | + mock_print.assert_called_once_with("[green]Setting show_command updated successfully![/green]") |
| 87 | + |
| 88 | + @patch("tgit.config.set_global_settings") |
| 89 | + @patch("tgit.config.print") |
| 90 | + def test_config_boolean_false_values(self, mock_print, mock_set_global_settings): |
| 91 | + """Test config with boolean false values""" |
| 92 | + false_values = ["false", "0", "no", "off", "FALSE", "NO", "OFF"] |
| 93 | + |
| 94 | + for value in false_values: |
| 95 | + mock_set_global_settings.reset_mock() |
| 96 | + mock_print.reset_mock() |
| 97 | + |
| 98 | + config(key="skip_confirm", value=value, interactive=False) |
| 99 | + |
| 100 | + mock_set_global_settings.assert_called_once_with("skip_confirm", False) |
| 101 | + mock_print.assert_called_once_with("[green]Setting skip_confirm updated successfully![/green]") |
| 102 | + |
| 103 | + @patch("tgit.config.print") |
| 104 | + def test_config_invalid_boolean_value(self, mock_print): |
| 105 | + """Test config with invalid boolean value""" |
| 106 | + with pytest.raises(typer.Exit) as exc_info: |
| 107 | + config(key="show_command", value="invalid", interactive=False) |
| 108 | + |
| 109 | + assert exc_info.value.exit_code == 1 |
| 110 | + mock_print.assert_called_once_with("Invalid boolean value for show_command. Use true/false, 1/0, yes/no, or on/off") |
| 111 | + |
| 112 | + @patch("tgit.config.set_global_settings") |
| 113 | + @patch("tgit.config.print") |
| 114 | + def test_config_all_valid_keys(self, mock_print, mock_set_global_settings): |
| 115 | + """Test config with all valid keys""" |
| 116 | + valid_configs = [ |
| 117 | + ("apiKey", "test_key"), |
| 118 | + ("apiUrl", "https://api.openai.com"), |
| 119 | + ("model", "gpt-4"), |
| 120 | + ("show_command", "true"), |
| 121 | + ("skip_confirm", "false"), |
| 122 | + ] |
| 123 | + |
| 124 | + for key, value in valid_configs: |
| 125 | + mock_set_global_settings.reset_mock() |
| 126 | + mock_print.reset_mock() |
| 127 | + |
| 128 | + config(key=key, value=value, interactive=False) |
| 129 | + |
| 130 | + # Convert expected value for boolean keys |
| 131 | + expected_value = value |
| 132 | + if key in ["show_command", "skip_confirm"]: |
| 133 | + expected_value = value.lower() == "true" |
| 134 | + |
| 135 | + mock_set_global_settings.assert_called_once_with(key, expected_value) |
| 136 | + mock_print.assert_called_once_with(f"[green]Setting {key} updated successfully![/green]") |
| 137 | + |
| 138 | + @patch("tgit.config.set_global_settings") |
| 139 | + @patch("tgit.config.interactive_settings") |
| 140 | + def test_config_interactive_takes_precedence(self, mock_interactive_settings, mock_set_global_settings): |
| 141 | + """Test that interactive flag takes precedence over provided key/value""" |
| 142 | + config(key="apiKey", value="test", interactive=True) |
| 143 | + |
| 144 | + mock_interactive_settings.assert_called_once() |
| 145 | + mock_set_global_settings.assert_not_called() |
0 commit comments