-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Complete refactor - must fix broken tests.
- Loading branch information
1 parent
78d5ddb
commit d51bc0e
Showing
12 changed files
with
204 additions
and
179 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -2,8 +2,8 @@ | |
sys.path.append('.') | ||
|
||
import pytest | ||
from ...memories import search_dreams | ||
from .. import get_image_summary, generate_dream_analysis, generate_dream_image, regular_chat, call_function_by_name, search_chat_with_dreams | ||
from lucidserver.memories.memories_main import search_dreams | ||
from lucidserver.actions.actions_main import get_image_summary, generate_dream_analysis, generate_dream_image, regular_chat, call_function_by_name, search_chat_with_dreams | ||
from unittest.mock import patch, Mock | ||
|
||
|
||
|
@@ -21,22 +21,22 @@ def mock_text_completion_with_error(*args, **kwargs): | |
|
||
# Test case for successful summary generation | ||
def test_get_image_summary_success(): | ||
with patch('..openai.text_completion', side_effect=mock_text_completion_summary): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_summary): | ||
dream_entry = "A mysterious dream about a forest" | ||
result = get_image_summary(dream_entry) | ||
assert result == "Generated Summary", f"Expected 'Generated Summary', but got {result}" | ||
|
||
|
||
# Test case for summary generation with GPT-4 error | ||
def test_get_image_summary_gpt4_error(): | ||
with patch('..openai.text_completion', side_effect=mock_text_completion_with_error): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_with_error): | ||
dream_entry = "A mysterious dream about a forest" | ||
result = get_image_summary(dream_entry) | ||
assert result == "Error: Unable to generate a summary.", f"Expected error message, but got {result}" | ||
|
||
# Test case for exception handling | ||
def test_get_image_summary_exception(): | ||
with patch('..openai.text_completion', side_effect=Exception("An unexpected exception")): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=Exception("An unexpected exception")): | ||
dream_entry = "A mysterious dream about a forest" | ||
result = get_image_summary(dream_entry) | ||
assert result == "Error: Unable to generate a summary.", f"Expected error message, but got {result}" | ||
|
@@ -52,23 +52,23 @@ def mock_text_completion_with_error(*args, **kwargs): | |
|
||
# Test case for successful dream analysis generation | ||
def test_generate_dream_analysis_success(): | ||
with patch('..openai.text_completion', side_effect=mock_text_completion_analysis): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_analysis): | ||
prompt = "A profound dream about a journey" | ||
system_content = "System Content" | ||
result = generate_dream_analysis(prompt, system_content) | ||
assert result == "Generated Analysis", f"Expected 'Generated Analysis', but got {result}" | ||
|
||
# Test case for dream analysis generation with GPT-4 error | ||
def test_generate_dream_analysis_gpt4_error(): | ||
with patch('..openai.text_completion', side_effect=mock_text_completion_with_error): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_with_error): | ||
prompt = "A profound dream about a journey" | ||
system_content = "System Content" | ||
result = generate_dream_analysis(prompt, system_content) | ||
assert result == "Error: Unable to generate a response.", f"Expected error message, but got {result}" | ||
|
||
# Test case for exception handling in dream analysis generation | ||
def test_generate_dream_analysis_exception(): | ||
with patch('..openai.text_completion', side_effect=Exception("An unexpected exception")): | ||
with patch('lucidserver.actions.actions_main.text_completion', side_effect=Exception("An unexpected exception")): | ||
prompt = "A profound dream about a journey" | ||
system_content = "System Content" | ||
result = generate_dream_analysis(prompt, system_content) | ||
|
@@ -83,7 +83,7 @@ def mock_requests_post(*args, **kwargs): | |
|
||
# Test case for successful dream image generation | ||
def test_generate_dream_image_success(): | ||
with patch('..openai.get_image_summary', return_value="Generated Summary"): | ||
with patch('lucidserver.actions.actions_main.get_image_summary', return_value="Generated Summary"): | ||
with patch('requests.post', side_effect=mock_requests_post): | ||
dreams = [{"id": "1", "metadata": {"entry": "A mysterious dream about a forest"}}] | ||
dream_id = "1" | ||
|
@@ -99,7 +99,7 @@ def test_generate_dream_image_dream_not_found(): | |
|
||
# Test case for dream image generation with error in OpenAI API response | ||
def test_generate_dream_image_api_error(): | ||
with patch('..openai.get_image_summary', return_value="Generated Summary"): | ||
with patch('lucidserver.actions.actions_main.get_image_summary', return_value="Generated Summary"): | ||
with patch('requests.post', return_value=Mock(json=lambda: {})): # No 'data' in response | ||
dreams = [{"id": "1", "metadata": {"entry": "A mysterious dream about a forest"}}] | ||
dream_id = "1" | ||
|
@@ -108,7 +108,7 @@ def test_generate_dream_image_api_error(): | |
|
||
# Test case for exception handling in dream image generation | ||
def test_generate_dream_image_exception(): | ||
with patch('..openai.get_image_summary', side_effect=Exception("An unexpected exception")): | ||
with patch('lucidserver.actions.actions_main.get_image_summary', side_effect=Exception("An unexpected exception")): | ||
dreams = [{"id": "1", "metadata": {"entry": "A mysterious dream about a forest"}}] | ||
dream_id = "1" | ||
result = generate_dream_image(dreams, dream_id) | ||
|
@@ -124,31 +124,31 @@ def mock_chat_completion_with_error(*args, **kwargs): | |
|
||
# Test case for successful regular chat | ||
def test_regular_chat_success(): | ||
with patch('..openai.chat_completion', side_effect=mock_chat_completion): | ||
with patch('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion): | ||
message = "Tell me more about lucid dreaming techniques." | ||
user_email = "[email protected]" | ||
result = regular_chat(message, user_email) | ||
assert result == "Generated Chat Response", f"Expected 'Generated Chat Response', but got {result}" | ||
|
||
# Test case for regular chat without user message | ||
def test_regular_chat_default_message(): | ||
with patch('..openai.chat_completion', side_effect=mock_chat_completion): | ||
with patch('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion): | ||
message = "" | ||
user_email = "[email protected]" | ||
result = regular_chat(message, user_email) | ||
assert result == "Generated Chat Response", f"Expected 'Generated Chat Response', but got {result}" | ||
|
||
# Test case for regular chat with GPT-4 error | ||
def test_regular_chat_gpt4_error(): | ||
with patch('..openai.chat_completion', side_effect=mock_chat_completion_with_error): | ||
with patch('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion_with_error): | ||
message = "Tell me more about lucid dreaming techniques." | ||
user_email = "[email protected]" | ||
result = regular_chat(message, user_email) | ||
assert result == "Error: Unable to generate a response.", f"Expected error message, but got {result}" | ||
|
||
# Test case for exception handling in regular chat | ||
def test_regular_chat_exception(): | ||
with patch('..openai.chat_completion', side_effect=Exception("An unexpected exception")): | ||
with patch('lucidserver.actions.actions_main.chat_completion', side_effect=Exception("An unexpected exception")): | ||
message = "Tell me more about lucid dreaming techniques." | ||
user_email = "[email protected]" | ||
result = regular_chat(message, user_email) | ||
|
@@ -173,15 +173,15 @@ def mock_count_tokens(*args, **kwargs): | |
|
||
# Test case for search_dreams function | ||
def test_search_dreams(): | ||
with patch('..database.database.search_memory', side_effect=mock_search_memory): | ||
with patch('lucidserver.memories.memories_main.search_memory', side_effect=mock_search_memory): | ||
keyword = "Dream" | ||
user_email = "[email protected]" | ||
result = search_dreams(keyword, user_email) | ||
assert len(result) == 1, f"Expected 1 result, but got {len(result)}" | ||
|
||
# Test case for call_function_by_name function | ||
def test_call_function_by_name(): | ||
with patch('..openai.function_completion', side_effect=mock_function_completion): | ||
with patch('lucidserver.actions.actions_main.function_completion', side_effect=mock_function_completion): | ||
function_name = "discuss_emotions" | ||
prompt = "Discuss emotions in dreams" | ||
messages = [] | ||
|
@@ -190,9 +190,9 @@ def test_call_function_by_name(): | |
|
||
# Test case for search_chat_with_dreams function with search results | ||
def test_search_chat_with_dreams_with_results(): | ||
with patch('..database.database.search_dreams', side_effect=mock_search_memory), \ | ||
patch('..openai.count_tokens', side_effect=mock_count_tokens), \ | ||
patch('..openai.call_function_by_name', side_effect=mock_function_completion): | ||
with patch('lucidserver.memories.memories_main.search_dreams', side_effect=mock_search_memory), \ | ||
patch('lucidserver.actions.actions_main.count_tokens', side_effect=mock_count_tokens), \ | ||
patch('lucidserver.actions.actions_main.call_function_by_name', side_effect=mock_function_completion): | ||
function_name = "discuss_emotions" | ||
prompt = "Discuss emotions in dreams" | ||
user_email = "[email protected]" | ||
|
@@ -202,9 +202,9 @@ def test_search_chat_with_dreams_with_results(): | |
|
||
# Test case for search_chat_with_dreams function without search results | ||
def test_search_chat_with_dreams_without_results(): | ||
with patch('..database.database.search_memory', side_effect=lambda *args, **kwargs: []), \ | ||
patch('..openai.count_tokens', side_effect=mock_count_tokens), \ | ||
patch('..openai.call_function_by_name', side_effect=mock_function_completion): | ||
with patch('lucidserver.memories.memories_main.search_memory', side_effect=lambda *args, **kwargs: []), \ | ||
patch('lucidserver.actions.actions_main.count_tokens', side_effect=mock_count_tokens), \ | ||
patch('lucidserver.actions.actions_main.call_function_by_name', side_effect=mock_function_completion): | ||
function_name = "discuss_emotions" | ||
prompt = "Discuss emotions in dreams" | ||
user_email = "[email protected]" | ||
|
This file contains 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
This file contains 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,27 @@ | ||
from .endpoints_main import ( | ||
register_endpoints, | ||
create_dream, | ||
get_dreams, | ||
get_dream, | ||
update_dream_analysis_and_image, | ||
get_dream_analysis, | ||
get_dream_image, | ||
search_dreams, | ||
delete_dream, | ||
search_chat_with_dreams, | ||
regular_chat | ||
) | ||
|
||
__all__ = [ | ||
"register_endpoints", | ||
"create_dream", | ||
"get_dreams", | ||
"get_dream", | ||
"update_dream_analysis_and_image", | ||
"get_dream_analysis", | ||
"get_dream_image", | ||
"search_dreams", | ||
"delete_dream", | ||
"search_chat_with_dreams", | ||
"regular_chat" | ||
] |
This file contains 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
This file contains 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,120 @@ | ||
import sys | ||
sys.path.append('.') | ||
|
||
from unittest.mock import patch | ||
from lucidserver.app import app | ||
from lucidserver.endpoints.endpoints_main import * | ||
import pytest | ||
import json | ||
|
||
# Placeholder for test user email | ||
test_user_email = "[email protected]" | ||
|
||
# Sample JWT token for testing | ||
test_token = f"Bearer test_token_for_{test_user_email}" | ||
|
||
# Sample dream args for testing | ||
dream_args = { | ||
"title": "Test Dream", | ||
"date": "2021-10-10", | ||
"entry": "A dream about testing", | ||
"id_token": "sample_token" | ||
} | ||
|
||
# Ensure that the dream with id 1 exists in the mocked data | ||
mocked_dream = { | ||
"id": 1, | ||
"metadata": { | ||
"entry": "test_entry", | ||
"title": "Test Dream", | ||
"date": "2021-10-10", | ||
"useremail": "[email protected]", | ||
"analysis": "test_analysis", | ||
"image": "test_image", | ||
} | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
app.config['TESTING'] = True | ||
with app.test_client() as client: | ||
yield client | ||
|
||
|
||
# Test create dream endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.create_dream", return_value={"id": 1}) | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_create_dream_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_create_dream, client): | ||
headers = {"Authorization": test_token} | ||
response = client.post("/api/dreams", json=dream_args, headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json["id"] == 1 | ||
|
||
|
||
# Test get dreams endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.get_dreams", return_value=[{"id": 1}]) | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_get_dreams_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_get_dreams, client): | ||
headers = {"Authorization": test_token} | ||
response = client.get("/api/dreams", headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json[0]["id"] == 1 | ||
|
||
|
||
# Test update dream endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.update_dream_analysis_and_image", return_value={"id": 1, "analysis": "updated_analysis"}) | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_update_dream_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_update_dream, client): | ||
headers = {"Authorization": test_token} | ||
response = client.put( | ||
"/api/dreams/1", json={"analysis": "updated_analysis"}, headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json["analysis"] == "updated_analysis" | ||
|
||
|
||
# Test get dream endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_get_dream_endpoint(mock_get_dream, mock_extract_user_email_from_token, client): | ||
headers = {"Authorization": test_token} | ||
response = client.get("/api/dreams/1", headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json["metadata"]["title"] == "Test Dream" | ||
|
||
|
||
# Test get dream analysis endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream_analysis", return_value={"analysis": "test_analysis"}) | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_get_dream_analysis_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_get_dream_analysis, client): | ||
headers = {"Authorization": test_token} | ||
response = client.get("/api/dreams/1/analysis", headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json["analysis"] == "test_analysis" | ||
|
||
|
||
# Test get dream image endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream_image", return_value="test_image") | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_get_dream_image_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_get_dream_image, client): | ||
headers = {"Authorization": test_token} | ||
response = client.get("/api/dreams/1/image", headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json["image"] == "test_image" | ||
|
||
|
||
# Test search dreams endpoint | ||
@patch("lucidserver.endpoints.endpoints_main.search_dreams", return_value=[{"id": 1}]) | ||
@patch("lucidserver.endpoints.endpoints_main.extract_user_email_from_token", return_value=test_user_email) | ||
@patch("lucidserver.endpoints.endpoints_main.get_dream", return_value=mocked_dream) | ||
def test_search_dreams_endpoint(mock_get_dream, mock_extract_user_email_from_token, mock_search_dreams, client): | ||
headers = {"Authorization": test_token} | ||
response = client.post("/api/dreams/search", | ||
json={"query": "test_query"}, headers=headers) | ||
assert response.status_code == 200 | ||
assert response.json[0]["id"] == 1 |
Oops, something went wrong.