Skip to content

Commit e070918

Browse files
committed
test: add unit tests
1 parent 5dd901f commit e070918

File tree

6 files changed

+99
-10
lines changed

6 files changed

+99
-10
lines changed

.coverage

-16 KB
Binary file not shown.

.coveragerc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[run]
2+
omit =
3+
api/terraform/python/openai_api/common/tests/*
4+
api/terraform/python/openai_api/lambda_info/tests/*
5+
api/terraform/python/openai_api/lambda_langchain/tests/*
6+
api/terraform/python/openai_api/lambda_openai_function/tests/*
7+
api/terraform/python/openai_api/lambda_openai_v2/tests/*

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.coverage
2+
.coverage.*
3+
14
# Jupyter Notebook
25
.ipynb_checkpoints
36
build

CHANGELOG.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
## [0.10.2](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.10.1...v0.10.2) (2024-01-23)
22

3-
43
### Bug Fixes
54

6-
* add more granular info section selections ([ce870f0](https://github.com/FullStackWithLawrence/aws-openai/commit/ce870f0eca1f2519d1de8ee3cecdf26ce1acae1c))
5+
- add more granular info section selections ([ce870f0](https://github.com/FullStackWithLawrence/aws-openai/commit/ce870f0eca1f2519d1de8ee3cecdf26ce1acae1c))
76

87
## [0.10.1](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.10.0...v0.10.1) (2024-01-23)
98

10-
119
### Bug Fixes
1210

13-
* configure google maps api for use in aws lambda. ([37068ee](https://github.com/FullStackWithLawrence/aws-openai/commit/37068ee3d84293b8f4c7e2095d625b1f35937cd1))
14-
* graceful failure if google geolocation api key is missing. ([f601c64](https://github.com/FullStackWithLawrence/aws-openai/commit/f601c6401d61d8c81e841bc9f3ff05a398de9d96))
15-
* implement get_current_weather() using google maps api + open-meteo ([453a432](https://github.com/FullStackWithLawrence/aws-openai/commit/453a432bd87be03aff3ec6628ca00555e91eaa34))
11+
- configure google maps api for use in aws lambda. ([37068ee](https://github.com/FullStackWithLawrence/aws-openai/commit/37068ee3d84293b8f4c7e2095d625b1f35937cd1))
12+
- graceful failure if google geolocation api key is missing. ([f601c64](https://github.com/FullStackWithLawrence/aws-openai/commit/f601c6401d61d8c81e841bc9f3ff05a398de9d96))
13+
- implement get_current_weather() using google maps api + open-meteo ([453a432](https://github.com/FullStackWithLawrence/aws-openai/commit/453a432bd87be03aff3ec6628ca00555e91eaa34))
1614

1715
# [0.10.0](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.9.1...v0.10.0) (2024-01-23)
1816

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ analyze:
3838
cloc . --exclude-ext=svg,json,zip --vcs=git
3939

4040
coverage:
41-
coverage run --source=api/terraform/python/openai_api -m unittest discover -s api/terraform/python/openai_api/
41+
coverage run --source=api/terraform/python/openai_api \
42+
-m unittest discover -s api/terraform/python/openai_api/
4243
coverage report -m
4344
coverage html
4445

api/terraform/python/openai_api/common/tests/test_configuration.py

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# -*- coding: utf-8 -*-
22
# 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+
"""
47

5-
# python stuff
68
import os
9+
10+
# python stuff
11+
import re
712
import sys
813
import unittest
914
from unittest.mock import patch
@@ -16,10 +21,20 @@
1621
PYTHON_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(os.path.dirname(__file__))))
1722
sys.path.append(PYTHON_ROOT) # noqa: E402
1823

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+
1933
# our stuff
20-
from openai_api.common.conf import Settings, SettingsDefaults # noqa: E402
34+
from openai_api.common.exceptions import OpenAIAPIConfigurationError
2135

2236

37+
# pylint: disable=too-many-public-methods
2338
class TestConfiguration(unittest.TestCase):
2439
"""Test configuration."""
2540

@@ -239,3 +254,68 @@ def test_initialize_with_values(self):
239254
# pylint: disable=no-member
240255
self.assertEqual(mock_settings.pinecone_api_key.get_secret_value(), "TEST_pinecone_api_key")
241256
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

Comments
 (0)