-
Notifications
You must be signed in to change notification settings - Fork 7
CV2-5077-Add-test-coverage-for-OpenAI-module-in-Alegre #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
skyemeedan
merged 15 commits into
develop
from
CV2-5077-Add-test-coverage-for-OpenAI-module-in-Alegre
Feb 28, 2025
Merged
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c79e5fb
create test_openai.py and add `test_openai_get_document_body`
ahmednasserswe 4d287a2
Update test_openai.py with `test_retrieve_openai_embeddings_handles_a…
ahmednasserswe d864f29
Update test_openai.py to mock redis cache
ahmednasserswe bce50e6
Update test_openai.py to include tests that call the api
ahmednasserswe 22423dc
commented `test_openai_get_document_body` and `test_retrieve_openai_e…
ahmednasserswe 653e9aa
uncommenting `test_openai_get_document_body` and `test_retrieve_opena…
ahmednasserswe b677ccc
mocking cache in `test_openai_get_document_body` and `test_retrieve_o…
ahmednasserswe bf1de40
asserting openai key is set during CI
ahmednasserswe 5862523
adding a unit test to find if openai_api_key is in env
ahmednasserswe 0bb458a
making TestRetrieveOpenAIEmbeddings implement BaseTestCase
ahmednasserswe 5f081fc
[CV2-6182] add bash script to copy ssm params for CI to env and modif…
1e0480f
add execute permissions for script
7391af1
better comments and correct .env_file name
5d66ed8
added test OPENAI key for CI to encoded .env file https://meedan.atla…
4e24bbb
removing ssm extraction script and build step because credential now …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
from app.main.lib.text_similarity import get_document_body | ||
from app.main.lib.openai import retrieve_openai_embeddings | ||
from app.main.lib.openai import PREFIX_OPENAI | ||
|
||
class TestRetrieveOpenAIEmbeddings(unittest.TestCase): | ||
def test_retrieve_openai_embeddings_calls_openai_api(self): | ||
with patch('openai.embeddings_utils.get_embedding') as mock_get_embedding, \ | ||
patch('app.main.lib.openai.redis_client.get_client') as mock_redis_client: | ||
mock_redis = mock_redis_client.return_value | ||
mock_redis.get.return_value = None # Ensure cache is empty | ||
|
||
test_content = { | ||
'text': 'this is a test', | ||
'models': ["openai-text-embedding-ada-002"], | ||
'content': 'let there be content', | ||
} | ||
|
||
mock_get_embedding.return_value = [0.1, 0.2, 0.3] | ||
ahmednasserswe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
result = retrieve_openai_embeddings(test_content['content'], test_content['models'][0]) | ||
mock_get_embedding.assert_called_once_with(test_content['content'], | ||
engine=test_content['models'][0][len(PREFIX_OPENAI):]) | ||
self.assertEqual(result, [0.1, 0.2, 0.3]) | ||
|
||
def test_retrieve_openai_embeddings_handles_api_error(self): | ||
with patch('openai.embeddings_utils.get_embedding') as mock_get_embedding, \ | ||
patch('app.main.lib.openai.redis_client.get_client') as mock_redis_client: | ||
mock_redis = mock_redis_client.return_value | ||
mock_redis.get.return_value = None # Ensure cache is empty | ||
|
||
test_content = { | ||
'text': 'this is a test', | ||
'models': ["openai-text-embedding-ada-002"], | ||
'content': 'let there be content', | ||
} | ||
|
||
mock_get_embedding.side_effect = Exception("API Error") | ||
result = retrieve_openai_embeddings(test_content['content'], test_content['models'][0]) | ||
mock_get_embedding.assert_called_once() | ||
self.assertIsNone(result) | ||
|
||
def test_openai_get_document_body(self): | ||
with patch('app.main.lib.text_similarity.retrieve_openai_embeddings') as mock_retrieve_openai_embeddings: | ||
test_content = { | ||
'text': 'this is a test', | ||
'models': ["openai-text-embedding-ada-002"], | ||
'min_es_score': 0.1, | ||
'content': 'let there be content', | ||
} | ||
mock_retrieve_openai_embeddings.return_value = [0.1, 0.2, 0.3] | ||
|
||
result = get_document_body(test_content) | ||
|
||
mock_retrieve_openai_embeddings.assert_called_once_with(test_content['content'], "openai-text-embedding-ada-002") | ||
self.assertEqual({'text': 'this is a test', 'models': ['openai-text-embedding-ada-002'], 'min_es_score': 0.1, 'content': 'let there be content', 'model': 'openai-text-embedding-ada-002', 'vector_openai-text-embedding-ada-002': [0.1, 0.2, 0.3], 'model_openai-text-embedding-ada-002': 1}, result) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.