|
| 1 | +import os |
| 2 | +import logging |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | +import pytest |
| 5 | + |
| 6 | +from conductor.asyncio_client.configuration.configuration import Configuration |
| 7 | + |
| 8 | + |
| 9 | +def test_initialization_env_vars_override_params(monkeypatch): |
| 10 | + monkeypatch.setenv("CONDUCTOR_SERVER_URL", "https://env.com/api") |
| 11 | + monkeypatch.setenv("CONDUCTOR_AUTH_KEY", "env_key") |
| 12 | + |
| 13 | + config = Configuration(server_url="https://param.com/api", auth_key="param_key") |
| 14 | + assert config.server_url == "https://param.com/api" |
| 15 | + assert config.auth_key == "param_key" |
| 16 | + |
| 17 | + |
| 18 | +def test_initialization_empty_server_url(): |
| 19 | + config = Configuration(server_url="") |
| 20 | + assert config.server_url == "http://localhost:8080/api" |
| 21 | + |
| 22 | + |
| 23 | +def test_ui_host_default(): |
| 24 | + config = Configuration(server_url="https://test.com/api") |
| 25 | + assert config.ui_host == "https://test.com" |
| 26 | + |
| 27 | + |
| 28 | +def test_ui_host_env_var(monkeypatch): |
| 29 | + monkeypatch.setenv("CONDUCTOR_UI_SERVER_URL", "https://ui.com") |
| 30 | + config = Configuration() |
| 31 | + assert config.ui_host == "https://ui.com" |
| 32 | + |
| 33 | + |
| 34 | +def test_get_env_int_valid(): |
| 35 | + config = Configuration() |
| 36 | + with patch.dict(os.environ, {"TEST_INT": "42"}): |
| 37 | + result = config._get_env_int("TEST_INT", 10) |
| 38 | + assert result == 42 |
| 39 | + |
| 40 | + |
| 41 | +def test_get_env_int_invalid(): |
| 42 | + config = Configuration() |
| 43 | + with patch.dict(os.environ, {"TEST_INT": "invalid"}): |
| 44 | + result = config._get_env_int("TEST_INT", 10) |
| 45 | + assert result == 10 |
| 46 | + |
| 47 | + |
| 48 | +def test_get_env_int_missing(): |
| 49 | + config = Configuration() |
| 50 | + with patch.dict(os.environ, {}, clear=True): |
| 51 | + result = config._get_env_int("TEST_INT", 10) |
| 52 | + assert result == 10 |
| 53 | + |
| 54 | + |
| 55 | +def test_get_env_float_valid(): |
| 56 | + config = Configuration() |
| 57 | + with patch.dict(os.environ, {"TEST_FLOAT": "3.14"}): |
| 58 | + result = config._get_env_float("TEST_FLOAT", 1.0) |
| 59 | + assert result == 3.14 |
| 60 | + |
| 61 | + |
| 62 | +def test_get_env_float_invalid(): |
| 63 | + config = Configuration() |
| 64 | + with patch.dict(os.environ, {"TEST_FLOAT": "invalid"}): |
| 65 | + result = config._get_env_float("TEST_FLOAT", 1.0) |
| 66 | + assert result == 1.0 |
| 67 | + |
| 68 | + |
| 69 | +def test_get_worker_property_value_task_specific(monkeypatch): |
| 70 | + monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL", "500") |
| 71 | + config = Configuration() |
| 72 | + result = config.get_worker_property_value("polling_interval", "mytask") |
| 73 | + assert result == 500.0 |
| 74 | + |
| 75 | + |
| 76 | +def test_get_worker_property_value_global(monkeypatch): |
| 77 | + monkeypatch.setenv("CONDUCTOR_WORKER_POLLING_INTERVAL", "600") |
| 78 | + config = Configuration() |
| 79 | + result = config.get_worker_property_value("polling_interval", "mytask") |
| 80 | + assert result == 600.0 |
| 81 | + |
| 82 | + |
| 83 | +def test_convert_property_value_polling_interval(): |
| 84 | + config = Configuration() |
| 85 | + result = config._convert_property_value("polling_interval", "250") |
| 86 | + assert result == 250.0 |
| 87 | + |
| 88 | + |
| 89 | +def test_convert_property_value_string(): |
| 90 | + config = Configuration() |
| 91 | + result = config._convert_property_value("domain", "test_domain") |
| 92 | + assert result == "test_domain" |
| 93 | + |
| 94 | + |
| 95 | +def test_set_worker_property(): |
| 96 | + config = Configuration() |
| 97 | + config.set_worker_property("mytask", "polling_interval", 300) |
| 98 | + assert config._worker_properties["mytask"]["polling_interval"] == 300 |
| 99 | + |
| 100 | + |
| 101 | +def test_set_worker_property_multiple(): |
| 102 | + config = Configuration() |
| 103 | + config.set_worker_property("mytask", "polling_interval", 300) |
| 104 | + config.set_worker_property("mytask", "domain", "test_domain") |
| 105 | + assert config._worker_properties["mytask"]["polling_interval"] == 300 |
| 106 | + assert config._worker_properties["mytask"]["domain"] == "test_domain" |
| 107 | + |
| 108 | + |
| 109 | +def test_get_worker_property(): |
| 110 | + config = Configuration() |
| 111 | + config.set_worker_property("mytask", "polling_interval", 300) |
| 112 | + result = config.get_worker_property("mytask", "polling_interval") |
| 113 | + assert result == 300 |
| 114 | + |
| 115 | + |
| 116 | +def test_get_worker_property_not_found(): |
| 117 | + config = Configuration() |
| 118 | + result = config.get_worker_property("mytask", "polling_interval") |
| 119 | + assert result is None |
| 120 | + |
| 121 | + |
| 122 | +def test_get_polling_interval_with_task_type(monkeypatch): |
| 123 | + monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_POLLING_INTERVAL", "400") |
| 124 | + config = Configuration() |
| 125 | + result = config.get_polling_interval("mytask") |
| 126 | + assert result == 400.0 |
| 127 | + |
| 128 | + |
| 129 | +def test_get_domain_with_task_type(monkeypatch): |
| 130 | + monkeypatch.setenv("CONDUCTOR_WORKER_MYTASK_DOMAIN", "task_domain") |
| 131 | + config = Configuration() |
| 132 | + result = config.get_domain("mytask") |
| 133 | + assert result == "task_domain" |
| 134 | + |
| 135 | + |
| 136 | +def test_host_property(): |
| 137 | + config = Configuration(server_url="https://test.com/api") |
| 138 | + assert config.host == "https://test.com/api" |
| 139 | + |
| 140 | + |
| 141 | +def test_host_setter(): |
| 142 | + config = Configuration() |
| 143 | + config.host = "https://new.com/api" |
| 144 | + assert config.host == "https://new.com/api" |
| 145 | + |
| 146 | + |
| 147 | +def test_debug_property(): |
| 148 | + config = Configuration(debug=True) |
| 149 | + assert config.debug is True |
| 150 | + |
| 151 | + |
| 152 | +def test_debug_setter(): |
| 153 | + config = Configuration() |
| 154 | + config.debug = True |
| 155 | + assert config.debug is True |
| 156 | + |
| 157 | + |
| 158 | +def test_api_key_property(): |
| 159 | + config = Configuration() |
| 160 | + config.api_key = {"test": "value"} |
| 161 | + assert config.api_key == {"test": "value"} |
| 162 | + |
| 163 | + |
| 164 | +def test_api_key_prefix_property(): |
| 165 | + config = Configuration() |
| 166 | + config.api_key_prefix = {"test": "prefix"} |
| 167 | + assert config.api_key_prefix == {"test": "prefix"} |
| 168 | + |
| 169 | + |
| 170 | +def test_username_property(): |
| 171 | + config = Configuration() |
| 172 | + config.username = "testuser" |
| 173 | + assert config.username == "testuser" |
| 174 | + |
| 175 | + |
| 176 | +def test_password_property(): |
| 177 | + config = Configuration() |
| 178 | + config.password = "testpass" |
| 179 | + assert config.password == "testpass" |
| 180 | + |
| 181 | + |
| 182 | +def test_access_token_property(): |
| 183 | + config = Configuration() |
| 184 | + config.access_token = "testtoken" |
| 185 | + assert config.access_token == "testtoken" |
| 186 | + |
| 187 | + |
| 188 | +def test_verify_ssl_property(): |
| 189 | + config = Configuration() |
| 190 | + config.verify_ssl = False |
| 191 | + assert config.verify_ssl is False |
| 192 | + |
| 193 | + |
| 194 | +def test_ssl_ca_cert_property(): |
| 195 | + config = Configuration() |
| 196 | + config.ssl_ca_cert = "/path/to/cert" |
| 197 | + assert config.ssl_ca_cert == "/path/to/cert" |
| 198 | + |
| 199 | + |
| 200 | +def test_retries_property(): |
| 201 | + config = Configuration() |
| 202 | + config.retries = 5 |
| 203 | + assert config.retries == 5 |
| 204 | + |
| 205 | + |
| 206 | +def test_logger_format_property(): |
| 207 | + config = Configuration() |
| 208 | + config.logger_format = "%(message)s" |
| 209 | + assert config.logger_format == "%(message)s" |
| 210 | + |
| 211 | + |
| 212 | +def test_log_level_property(): |
| 213 | + config = Configuration(debug=True) |
| 214 | + assert config.log_level == logging.DEBUG |
| 215 | + |
| 216 | + |
| 217 | +def test_apply_logging_config(): |
| 218 | + config = Configuration() |
| 219 | + config.apply_logging_config() |
| 220 | + assert config.is_logger_config_applied is True |
| 221 | + |
| 222 | + |
| 223 | +def test_apply_logging_config_custom(): |
| 224 | + config = Configuration() |
| 225 | + config.apply_logging_config(log_format="%(message)s", level=logging.ERROR) |
| 226 | + assert config.is_logger_config_applied is True |
| 227 | + |
| 228 | + |
| 229 | +def test_apply_logging_config_already_applied(): |
| 230 | + config = Configuration() |
| 231 | + config.apply_logging_config() |
| 232 | + config.apply_logging_config() |
| 233 | + assert config.is_logger_config_applied is True |
| 234 | + |
| 235 | + |
| 236 | +def test_get_logging_formatted_name(): |
| 237 | + result = Configuration.get_logging_formatted_name("test_logger") |
| 238 | + assert result.startswith("[pid:") |
| 239 | + assert result.endswith("] test_logger") |
| 240 | + |
| 241 | + |
| 242 | +def test_ui_host_property(): |
| 243 | + config = Configuration(server_url="https://test.com/api") |
| 244 | + assert config.ui_host == "https://test.com" |
| 245 | + |
| 246 | + |
| 247 | +def test_getattr_delegation(): |
| 248 | + config = Configuration() |
| 249 | + mock_config = MagicMock() |
| 250 | + config._http_config = mock_config |
| 251 | + mock_config.test_attr = "test_value" |
| 252 | + |
| 253 | + result = config.test_attr |
| 254 | + assert result == "test_value" |
| 255 | + |
| 256 | + |
| 257 | +def test_getattr_no_http_config(): |
| 258 | + config = Configuration() |
| 259 | + config._http_config = None |
| 260 | + |
| 261 | + with pytest.raises(AttributeError): |
| 262 | + _ = config.nonexistent_attr |
| 263 | + |
| 264 | + |
| 265 | +def test_auth_setup_with_credentials(): |
| 266 | + config = Configuration(auth_key="key", auth_secret="secret") |
| 267 | + assert "api_key" in config.api_key |
| 268 | + assert config.api_key["api_key"] == "key" |
| 269 | + |
| 270 | + |
| 271 | +def test_worker_properties_dict_initialization(): |
| 272 | + config = Configuration() |
| 273 | + assert isinstance(config._worker_properties, dict) |
| 274 | + assert len(config._worker_properties) == 0 |
| 275 | + |
| 276 | + |
| 277 | +def test_get_worker_property_value_unknown_property(): |
| 278 | + config = Configuration() |
| 279 | + result = config.get_worker_property_value("unknown_property", "mytask") |
| 280 | + assert result is None |
| 281 | + |
| 282 | + |
| 283 | +def test_host_property_no_http_config(): |
| 284 | + config = Configuration() |
| 285 | + config._http_config = None |
| 286 | + config._host = "test_host" |
| 287 | + assert config.host == "test_host" |
| 288 | + |
| 289 | + |
| 290 | +def test_debug_setter_false(): |
| 291 | + config = Configuration(debug=True) |
| 292 | + config.debug = False |
| 293 | + assert config.debug is False |
0 commit comments