|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | # pylint: disable=wrong-import-position
|
3 |
| -"""Test configuration Settings class.""" |
| 3 | +"""Test configuration Settings class. |
| 4 | +
|
| 5 | +TODO: Add tests for: 480, 487, 531, 595, 602, 609, 612-617, 623, 626-631, 654, 662-664, 671-673, 686, 702, 710-712, 725, 740-741 |
| 6 | +""" |
4 | 7 |
|
5 |
| -# python stuff |
6 | 8 | import os
|
| 9 | + |
| 10 | +# python stuff |
| 11 | +import re |
7 | 12 | import sys
|
8 | 13 | import unittest
|
9 | 14 | from unittest.mock import patch
|
|
16 | 21 | PYTHON_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
|
17 | 22 | sys.path.append(PYTHON_ROOT) # noqa: E402
|
18 | 23 |
|
| 24 | +from openai_api.common.conf import ( # noqa: E402 |
| 25 | + Services, |
| 26 | + Settings, |
| 27 | + SettingsDefaults, |
| 28 | + empty_str_to_bool_default, |
| 29 | + empty_str_to_int_default, |
| 30 | + get_semantic_version, |
| 31 | +) |
| 32 | + |
19 | 33 | # our stuff
|
20 |
| -from openai_api.common.conf import Settings, SettingsDefaults # noqa: E402 |
| 34 | +from openai_api.common.exceptions import OpenAIAPIConfigurationError |
21 | 35 |
|
22 | 36 |
|
| 37 | +# pylint: disable=too-many-public-methods |
23 | 38 | class TestConfiguration(unittest.TestCase):
|
24 | 39 | """Test configuration."""
|
25 | 40 |
|
@@ -239,3 +254,68 @@ def test_initialize_with_values(self):
|
239 | 254 | # pylint: disable=no-member
|
240 | 255 | self.assertEqual(mock_settings.pinecone_api_key.get_secret_value(), "TEST_pinecone_api_key")
|
241 | 256 | self.assertEqual(mock_settings.shared_resource_identifier, "TEST_shared_resource_identifier")
|
| 257 | + |
| 258 | + def test_semantic_version(self): |
| 259 | + """Test that the semantic version conforms to a valid pattern.""" |
| 260 | + version = get_semantic_version() |
| 261 | + self.assertIsNotNone(version) |
| 262 | + pattern = r"^\d+\.\d+\.\d+$" |
| 263 | + match = re.match(pattern, version) |
| 264 | + self.assertIsNotNone(match, f"{version} is not a valid semantic version") |
| 265 | + |
| 266 | + def test_services(self): |
| 267 | + """Test that the services are valid.""" |
| 268 | + services = Services() |
| 269 | + self.assertIsNotNone(services) |
| 270 | + self.assertTrue(services.enabled(services.AWS_CLI)) |
| 271 | + with self.assertRaises(OpenAIAPIConfigurationError): |
| 272 | + services.raise_error_on_disabled(services.AWS_RDS) |
| 273 | + self.assertIsInstance(services.to_dict(), dict) |
| 274 | + self.assertIn(services.AWS_CLI[0], services.enabled_services()) |
| 275 | + |
| 276 | + def test_empty_str_to_bool_default(self): |
| 277 | + """Test that empty strings are converted to bool defaults.""" |
| 278 | + self.assertFalse(empty_str_to_bool_default("", False)) |
| 279 | + self.assertTrue(empty_str_to_bool_default("true", True)) |
| 280 | + |
| 281 | + def test_empty_str_to_int_default(self): |
| 282 | + """Test that empty strings are converted to int defaults.""" |
| 283 | + self.assertEqual(empty_str_to_int_default("", 0), 0) |
| 284 | + self.assertEqual(empty_str_to_int_default("1", 1), 1) |
| 285 | + |
| 286 | + def test_settings_aws_account_id(self): |
| 287 | + """Test that the AWS account ID is valid.""" |
| 288 | + mock_settings = Settings(init_info="test_settings_aws_account_id()") |
| 289 | + self.assertIsNotNone(mock_settings.aws_account_id) |
| 290 | + self.assertTrue(mock_settings.aws_account_id.isdigit()) |
| 291 | + |
| 292 | + def test_settings_aws_session(self): |
| 293 | + """Test that the AWS session is valid.""" |
| 294 | + mock_settings = Settings(init_info="test_settings_aws_session()") |
| 295 | + self.assertIsNotNone(mock_settings.aws_session) |
| 296 | + self.assertIsNotNone(mock_settings.aws_session.region_name) |
| 297 | + self.assertIsNotNone(mock_settings.aws_session.profile_name) |
| 298 | + |
| 299 | + def test_settings_dynamodb(self): |
| 300 | + """Test that the DynamoDB table is valid.""" |
| 301 | + mock_settings = Settings(init_info="test_settings_dynamodb()") |
| 302 | + # pylint: disable=pointless-statement |
| 303 | + with self.assertRaises(OpenAIAPIConfigurationError): |
| 304 | + mock_settings.aws_dynamodb_client |
| 305 | + |
| 306 | + def test_settings_aws_s3_bucket_name(self): |
| 307 | + """Test that the S3 bucket name is valid.""" |
| 308 | + mock_settings = Settings(init_info="test_settings_aws_s3_bucket_name()") |
| 309 | + self.assertIsNotNone(mock_settings.aws_s3_bucket_name) |
| 310 | + self.assertTrue(mock_settings.aws_s3_bucket_name.startswith(mock_settings.aws_account_id)) |
| 311 | + |
| 312 | + def test_settings_aws_apigateway_domain_name(self): |
| 313 | + """Test that the API Gateway domain name is valid.""" |
| 314 | + mock_settings = Settings(init_info="test_settings_aws_apigateway_domain_name()") |
| 315 | + hostname = mock_settings.aws_apigateway_domain_name |
| 316 | + self.assertIsNotNone(mock_settings.aws_apigateway_domain_name) |
| 317 | + # pylint: disable=anomalous-backslash-in-string |
| 318 | + assert re.match( |
| 319 | + "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$", |
| 320 | + hostname, |
| 321 | + ), "Invalid hostname" |
0 commit comments