diff --git a/tests/test_history.txt b/tests/test_history.txt index fffe1396..6904acf5 100644 --- a/tests/test_history.txt +++ b/tests/test_history.txt @@ -1,30 +1,6 @@ -23/11 14:23:17 User: How are you? -23/11 14:23:17 Assistant: Mocked response +25/11 12:13:25 User: How are you? +25/11 12:13:25 Assistant: Mocked response -23/11 14:24:53 User: How are you? -23/11 14:24:53 Assistant: Mocked response - -23/11 14:25:10 User: How are you? -23/11 14:25:10 Assistant: Mocked response - -23/11 14:27:11 User: How are you? -23/11 14:27:11 Assistant: Mocked response - -23/11 14:31:04 User: How are you? -23/11 14:31:04 Assistant: Mocked response - -23/11 14:31:50 User: How are you? -23/11 14:31:50 Assistant: Mocked response - -24/11 19:38:05 User: How are you? -24/11 19:38:05 Assistant: Mocked response - -24/11 19:38:43 User: How are you? -24/11 19:38:43 Assistant: Mocked response - -24/11 19:39:11 User: How are you? -24/11 19:39:11 Assistant: Mocked response - -24/11 19:39:51 User: How are you? -24/11 19:39:51 Assistant: Mocked response +25/11 12:13:25 User: How are you? +25/11 12:13:25 Assistant: Mocked response diff --git a/tests/test_utilities.py b/tests/test_utilities.py index f03331b6..95985b28 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,6 +1,6 @@ import unittest from unittest.mock import patch, MagicMock -from datetime import datetime +from datetime import datetime, timedelta import sys import os import openai @@ -15,10 +15,10 @@ sys.path.append(parent_dir) from utilities import * -class TestChatFunctions(unittest.TestCase): +class TestUtilities(unittest.TestCase): @patch('openai.ChatCompletion.create') - def test_chatcompletion(self, mock_openai_create): + def test_chatcompletion_pass(self, mock_openai_create): # Mock the OpenAI API call mock_openai_create.return_value = { 'choices': [{'message': {'content': 'Mocked response'}}] @@ -28,13 +28,27 @@ def test_chatcompletion(self, mock_openai_create): impersonated_role = "Assistant" explicit_input = "Provide assistance" chat_history = "User: Hi\nAssistant: Hello" + result = chatcompletion(user_input, impersonated_role, explicit_input, chat_history) + self.assertEqual(result, 'Mocked response') + + @patch('openai.ChatCompletion.create') + def test_chatcompletion_fail(self, mock_openai_create): + # Mock the OpenAI API call + mock_openai_create.return_value = { + 'choices': [{'message': {'content': 'Mocked response'}}] + } + + user_input = "Hello" + impersonated_role = "Fitness coach" + explicit_input = "Provide fitness advice" + chat_history = "User: Hi\nAssistant: Hello" result = chatcompletion(user_input, impersonated_role, explicit_input, chat_history) - self.assertEqual(result, 'Mocked response') + self.assertNotEqual(result, '') @patch('openai.ChatCompletion.create') - def test_chat(self, mock_openai_create): + def test_chat_pass(self, mock_openai_create): # Mock the OpenAI API call mock_openai_create.return_value = { 'choices': [{'message': {'content': 'Mocked response'}}] @@ -52,8 +66,27 @@ def test_chat(self, mock_openai_create): self.assertEqual(result, 'Mocked response') - def test_get_response(self): - # Mocking the chat function since it is already tested separately + @patch('openai.ChatCompletion.create') + def test_chat_fail(self, mock_openai_create): + # Mock the OpenAI API call + mock_openai_create.return_value = { + 'choices': [{'message': {'content': 'Mocked response'}}] + } + + chat_history = "User: Hi\nAssistant: Hello" + name = "Assistant" + chatgpt_output = "" + user_input = "How are you?" + history_file = "test_history.txt" + impersonated_role = "Assistant" + explicit_input = "Provide assistance" + + result = chat(chat_history, name, chatgpt_output, user_input, history_file, impersonated_role, explicit_input) + + self.assertNotEqual(result, 'Incorrect response') + + def test_get_response_pass(self): + # Mocking the chat function with patch('utilities.chat', return_value='Mocked response'): chat_history = "User: Hi\nAssistant: Hello" name = "Assistant" @@ -67,8 +100,22 @@ def test_get_response(self): self.assertEqual(result, 'Mocked response') - def test_get_entries_for_email(self): - # Assuming you have a MongoDB mock (you can use mongomock for testing) + @patch('utilities.chat', return_value='Mocked response') + def test_get_response_fail(self, mock_chat): + chat_history = "User: Hi\nAssistant: Hello" + name = "Assistant" + chatgpt_output = "" + user_text = "How are you?" + history_file = "test_history.txt" + impersonated_role = "Assistant" + explicit_input = "Provide assistance" + + result = get_response(chat_history, name, chatgpt_output, user_text, history_file, impersonated_role, explicit_input) + + self.assertNotEqual(result, 'Incorrect response') + + def test_get_entries_for_email_pass(self): + mock_db = MagicMock() entries_data = [{'email': 'test@example.com', 'date': '2023-11-23'}] mock_db.calories.find.return_value = entries_data @@ -81,11 +128,31 @@ def test_get_entries_for_email(self): self.assertEqual(result, entries_data) - def test_calc_bmi(self): + def test_get_entries_for_email_fail(self): + + mock_db = MagicMock() + entries_data = [{'email': 'test@example.com', 'date': '2023-11-23'}] + mock_db.calories.find.return_value = entries_data + + email = 'test@example.com' + start_date = datetime(2023, 1, 1) + end_date = datetime(2023, 12, 31) + + result, _ = get_entries_for_email(mock_db, email, start_date, end_date) + + self.assertNotEqual(result, [{'email': 'wrong@example.com', 'date': '2023-11-23'}]) + + def test_calc_bmi_pass(self): result = calc_bmi(70, 175) + # Match correct value self.assertEqual(result, 22.86) - def test_get_bmi_category(self): + def test_calc_bmi_fail(self): + result = calc_bmi(70, 175) + # Match correct value + self.assertNotEqual(result, 12) + + def test_get_bmi_category_pass(self): result_underweight = get_bmi_category(18.0) result_normal = get_bmi_category(22.0) result_overweight = get_bmi_category(27.0) @@ -96,5 +163,39 @@ def test_get_bmi_category(self): self.assertEqual(result_overweight, 'Overweight') self.assertEqual(result_obese, 'Obese') + def test_get_bmi_category_fail(self): + result_underweight = get_bmi_category(18.0) + result_normal = get_bmi_category(22.0) + result_overweight = get_bmi_category(27.0) + result_obese = get_bmi_category(30.0) + + self.assertNotEqual(result_underweight, 'Normal Weight') + self.assertNotEqual(result_normal, 'Obese') + self.assertNotEqual(result_overweight, 'Underweight') + self.assertNotEqual(result_obese, 'Overweight') + + def test_total_calories_to_burn_pass(self): + result = total_calories_to_burn(70, 60) + # Expected result: (70 - 60) * 7700 = 10000 + self.assertEqual(result, 77000) + + def test_total_calories_to_burn_fail(self): + result = total_calories_to_burn(70, 60) + # This should fail, as the expected result is 10000 + self.assertNotEqual(result, 5000) + + def test_calories_to_burn_pass(self): + target_date = datetime.today() + timedelta(days=10) + start_date = datetime.today() - timedelta(days=5) + result = calories_to_burn(2000, 1500, target_date, start_date) + self.assertEqual(result, 1166) + + def test_calories_to_burn_fail(self): + target_date = datetime.today() + timedelta(days=10) + start_date = datetime.today() - timedelta(days=5) + result = calories_to_burn(2000, 1500, target_date, start_date) + # This should fail, as the expected result is 100 + self.assertNotEqual(result, 50) + if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file