Skip to content

Commit

Permalink
Complete refactor - must fix broken tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dreaminglucid committed Aug 19, 2023
1 parent e1548bc commit 407d4c3
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 67 deletions.
Empty file added lucidserver/tests/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_summary):
with patch('lucidserver.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('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_with_error):
with patch('lucidserver.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('lucidserver.actions.actions_main.text_completion', side_effect=Exception("An unexpected exception")):
with patch('lucidserver.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}"
Expand All @@ -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('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_analysis):
with patch('lucidserver.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('lucidserver.actions.actions_main.text_completion', side_effect=mock_text_completion_with_error):
with patch('lucidserver.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('lucidserver.actions.actions_main.text_completion', side_effect=Exception("An unexpected exception")):
with patch('lucidserver.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)
Expand All @@ -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('lucidserver.actions.actions_main.get_image_summary', return_value="Generated Summary"):
with patch('lucidserver.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"
Expand All @@ -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('lucidserver.actions.actions_main.get_image_summary', return_value="Generated Summary"):
with patch('lucidserver.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"
Expand All @@ -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('lucidserver.actions.actions_main.get_image_summary', side_effect=Exception("An unexpected exception")):
with patch('lucidserver.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)
Expand All @@ -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('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion):
with patch('lucidserver.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('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion):
with patch('lucidserver.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('lucidserver.actions.actions_main.chat_completion', side_effect=mock_chat_completion_with_error):
with patch('lucidserver.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('lucidserver.actions.actions_main.chat_completion', side_effect=Exception("An unexpected exception")):
with patch('lucidserver.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)
Expand All @@ -173,15 +173,15 @@ def mock_count_tokens(*args, **kwargs):

# Test case for search_dreams function
def test_search_dreams():
with patch('lucidserver.memories.memories_main.search_memory', side_effect=mock_search_memory):
with patch('lucidserver.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('lucidserver.actions.actions_main.function_completion', side_effect=mock_function_completion):
with patch('lucidserver.actions.main.function_completion', side_effect=mock_function_completion):
function_name = "discuss_emotions"
prompt = "Discuss emotions in dreams"
messages = []
Expand All @@ -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('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):
with patch('lucidserver.memories.main.search_dreams', side_effect=mock_search_memory), \
patch('lucidserver.actions.main.count_tokens', side_effect=mock_count_tokens), \
patch('lucidserver.actions.main.call_function_by_name', side_effect=mock_function_completion):
function_name = "discuss_emotions"
prompt = "Discuss emotions in dreams"
user_email = "[email protected]"
Expand All @@ -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('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):
with patch('lucidserver.memories.main.search_memory', side_effect=lambda *args, **kwargs: []), \
patch('lucidserver.actions.main.count_tokens', side_effect=mock_count_tokens), \
patch('lucidserver.actions.main.call_function_by_name', side_effect=mock_function_completion):
function_name = "discuss_emotions"
prompt = "Discuss emotions in dreams"
user_email = "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def 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)
@patch("lucidserver.endpoints.main.create_dream", return_value={"id": 1})
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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)
Expand All @@ -54,9 +54,9 @@ def test_create_dream_endpoint(mock_get_dream, mock_extract_user_email_from_toke


# 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)
@patch("lucidserver.endpoints.main.get_dreams", return_value=[{"id": 1}])
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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)
Expand All @@ -65,9 +65,9 @@ def test_get_dreams_endpoint(mock_get_dream, mock_extract_user_email_from_token,


# 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)
@patch("lucidserver.endpoints.main.update_dream_analysis_and_image", return_value={"id": 1, "analysis": "updated_analysis"})
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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(
Expand All @@ -77,8 +77,8 @@ def test_update_dream_endpoint(mock_get_dream, mock_extract_user_email_from_toke


# 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)
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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)
Expand All @@ -87,9 +87,9 @@ def test_get_dream_endpoint(mock_get_dream, mock_extract_user_email_from_token,


# 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)
@patch("lucidserver.endpoints.main.get_dream_analysis", return_value={"analysis": "test_analysis"})
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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)
Expand All @@ -98,9 +98,9 @@ def test_get_dream_analysis_endpoint(mock_get_dream, mock_extract_user_email_fro


# 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)
@patch("lucidserver.endpoints.main.get_dream_image", return_value="test_image")
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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)
Expand All @@ -109,9 +109,9 @@ def test_get_dream_image_endpoint(mock_get_dream, mock_extract_user_email_from_t


# 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)
@patch("lucidserver.endpoints.main.search_dreams", return_value=[{"id": 1}])
@patch("lucidserver.endpoints.main.extract_user_email_from_token", return_value=test_user_email)
@patch("lucidserver.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",
Expand Down
Loading

0 comments on commit 407d4c3

Please sign in to comment.