Skip to content

Commit b0ce55f

Browse files
author
RobuRishabh
committed
Github_actions_updated
1 parent 4493188 commit b0ce55f

File tree

6 files changed

+203
-52
lines changed

6 files changed

+203
-52
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- name: Set up Python
2323
uses: actions/setup-python@v5
2424
with:
25-
python-version: '3.11' # Specify your Python version
25+
python-version: '3.10' # Specify your Python version
2626

2727
# Step 3: Install dependencies
2828
- name: Install dependencies

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ tenacity
4242
tqdm
4343
typing_extensions
4444
urllib3
45+
unittest-mock
4546
watchdog

tests/test_app.py

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/test_mock_databse.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,48 @@
11
import unittest
2-
from mock_database import flights
2+
from mock_database import flights, search_flights, check_ollama_availability
33

44
class TestMockDatabase(unittest.TestCase):
55
def test_flight_data_exists(self):
66
self.assertGreater(len(flights), 0, "Flight database should not be empty")
77

88
def test_flight_structure(self):
99
flight = flights[0]
10-
expected_keys = {"flight_number", "origin", "destination", "time"}
10+
expected_keys = {"flight_number", "origin", "destination", "time", "airline"}
1111
self.assertEqual(set(flight.keys()), expected_keys, "Flight data has incorrect structure")
1212

13+
def test_search_flights_by_flight_number(self):
14+
results = search_flights(flight_number="NY100") # Use keyword arg for flight_number
15+
self.assertGreater(len(results), 0, "Should find at least one matching flight")
16+
self.assertIn("NY100", [f["flight_number"] for f in results], "Flight NY100 should be found")
17+
self.assertEqual(len(results), 1, "Should find exactly one flight for NY100")
18+
19+
def test_search_flights_by_origin(self):
20+
results = search_flights(origin="New York")
21+
self.assertGreater(len(results), 0, "Should find flights from New York")
22+
self.assertTrue(all(f["origin"] == "New York" for f in results), "All results should have origin New York")
23+
24+
def test_search_flights_by_destination(self):
25+
results = search_flights(destination="London")
26+
self.assertGreater(len(results), 0, "Should find flights to London")
27+
self.assertTrue(all(f["destination"] == "London" for f in results), "All results should have destination London")
28+
29+
def test_search_flights_by_airline(self):
30+
results = search_flights(airline="Global Airways")
31+
self.assertGreater(len(results), 0, "Should find flights by Global Airways")
32+
self.assertTrue(all(f["airline"] == "Global Airways" for f in results), "All results should have airline Global Airways")
33+
34+
def test_search_flights_no_results(self):
35+
results = search_flights(flight_number="XYZ999")
36+
self.assertEqual(len(results), 0, "Should return no results for invalid flight number")
37+
38+
def test_search_flights_no_parameters(self):
39+
results = search_flights()
40+
self.assertEqual(len(results), 0, "Should return no results when no parameters provided")
41+
42+
def test_search_flights_case_insensitivity(self):
43+
results = search_flights(flight_number="ny100")
44+
self.assertGreater(len(results), 0, "Should find NY100 regardless of case")
45+
self.assertIn("NY100", [f["flight_number"] for f in results], "Flight NY100 should be found")
46+
1347
if __name__ == "__main__":
1448
unittest.main()

tests/test_ollama_api.py

Lines changed: 87 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,93 @@
11
import unittest
2-
from unittest.mock import patch
3-
from ollama_api import call_ollama_api
2+
from unittest.mock import patch, Mock
3+
from ollama_api import initialize_ollama, check_ollama_availability, generate_response, generate_fallback_response
44

55
class TestOllamaAPI(unittest.TestCase):
6-
@patch("ollama_api.requests.post")
7-
def test_call_ollama_api_success(self, mock_post):
8-
mock_post.return_value.status_code = 200
9-
mock_post.return_value.json.return_value = {"response": "Flight NY100 departs at 08:00."}
10-
query = "What are flights from New York to London?"
11-
flight_data = [{"flight_number": "NY100", "origin": "New York", "destination": "London", "time": "2025-05-01 08:00"}]
12-
response = call_ollama_api(query, flight_data)
13-
self.assertEqual(response, "Flight NY100 departs at 08:00.")
14-
15-
@patch("ollama_api.requests.post")
16-
def test_call_ollama_api_failure(self, mock_post):
17-
mock_post.return_value.status_code = 500
18-
query = "What are flights from New York to London?"
19-
flight_data = [{"flight_number": "NY100", "origin": "New York", "destination": "London", "time": "2025-05-01 08:00"}]
20-
with self.assertRaises(Exception):
21-
call_ollama_api(query, flight_data)
6+
def setUp(self):
7+
# Reset global ollama_llm for each test
8+
global ollama_llm
9+
ollama_llm = None
10+
11+
@patch("ollama_api.OllamaLLM")
12+
def test_initialize_ollama_success(self, mock_ollama_llm):
13+
mock_llm_instance = Mock()
14+
mock_ollama_llm.return_value = mock_llm_instance
15+
result = initialize_ollama()
16+
self.assertIsNotNone(result, "Ollama LLM should initialize successfully")
17+
self.assertEqual(result, mock_llm_instance)
18+
19+
@patch("ollama_api.OllamaLLM", side_effect=Exception("Initialization failed"))
20+
def test_initialize_ollama_failure(self, mock_ollama_llm):
21+
result = initialize_ollama()
22+
self.assertIsNone(result, "Ollama LLM should return None on failure")
23+
24+
@patch("ollama_api.requests.get")
25+
def test_check_ollama_availability_success(self, mock_get):
26+
mock_response = Mock()
27+
mock_response.status_code = 200
28+
mock_get.return_value = mock_response
29+
is_available, message = check_ollama_availability()
30+
self.assertTrue(is_available, "Ollama should be available")
31+
self.assertIn("available", message)
32+
33+
@patch("ollama_api.requests.get", side_effect=requests.RequestException("Connection error"))
34+
def test_check_ollama_availability_failure(self, mock_get):
35+
is_available, message = check_ollama_availability()
36+
self.assertFalse(is_available, "Ollama should not be available")
37+
self.assertIn("not available", message)
38+
39+
def test_generate_fallback_response_with_flights(self):
40+
flights = [
41+
{"flight_number": "NY100", "origin": "New York", "destination": "London", "time": "2025-05-01 08:00", "airline": "Global Airways"}
42+
]
43+
response = generate_fallback_response("test query", flights)
44+
self.assertIn("NY100", response)
45+
self.assertIn("New York", response)
46+
self.assertIn("London", response)
47+
self.assertIn("2025-05-01 08:00", response)
48+
self.assertIn("Global Airways", response)
49+
50+
def test_generate_fallback_response_no_flights(self):
51+
response = generate_fallback_response("test query", [])
52+
self.assertEqual(response, "I couldn't find any flights matching your criteria. Please try again.")
53+
54+
@patch("ollama_api.check_ollama_availability")
55+
@patch("ollama_api.ollama_llm", new=Mock(invoke=lambda x: "Flight NY100 departs at 08:00 from New York to London with Global Airways"))
56+
def test_generate_response_success(self, mock_check_availability):
57+
mock_check_availability.return_value = (True, "Ollama available")
58+
query = "Show me flight NY100"
59+
flight_data = [
60+
{"flight_number": "NY100", "origin": "New York", "destination": "London", "time": "2025-05-01 08:00", "airline": "Global Airways"}
61+
]
62+
response = generate_response(query, flight_data)
63+
self.assertIn("NY100", response)
64+
self.assertIn("08:00", response)
65+
self.assertIn("New York", response)
66+
self.assertIn("London", response)
67+
self.assertIn("Global Airways", response)
68+
69+
@patch("ollama_api.check_ollama_availability")
70+
def test_generate_response_no_flights(self, mock_check_availability):
71+
mock_check_availability.return_value = (True, "Ollama available")
72+
# Ensure ollama_llm is initialized (mock it globally)
73+
global ollama_llm
74+
ollama_llm = Mock(invoke=lambda x: "No flights found.")
75+
query = "Show me flight XYZ999"
76+
flight_data = []
77+
response = generate_response(query, flight_data)
78+
self.assertEqual(response, "I couldn't find any flights matching your criteria. Please try again.")
79+
80+
@patch("ollama_api.check_ollama_availability")
81+
def test_generate_response_ollama_unavailable(self, mock_check_availability):
82+
mock_check_availability.return_value = (False, "Ollama not available")
83+
query = "Show me flight NY100"
84+
flight_data = [
85+
{"flight_number": "NY100", "origin": "New York", "destination": "London", "time": "2025-05-01 08:00", "airline": "Global Airways"}
86+
]
87+
response = generate_response(query, flight_data)
88+
self.assertIn("NY100", response)
89+
self.assertIn("New York", response)
90+
self.assertIn("London", response)
2291

2392
if __name__ == "__main__":
2493
unittest.main()

tests/test_query_handler.py

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,84 @@
11
import unittest
2-
from query_handler import parse_query, search_flights
2+
from unittest.mock import patch, Mock
3+
from query_handler import process_query, extract_entities_ollama, extract_flight_number, extract_entities_from_keywords, OLLAMA_AVAILABLE
34

45
class TestQueryHandler(unittest.TestCase):
5-
def test_parse_query(self):
6-
query = "What flights are from New York to London?"
7-
result = parse_query(query)
8-
self.assertEqual(result["origin"], "New York")
9-
self.assertEqual(result["destination"], "London")
10-
11-
def test_search_flights(self):
12-
query = {"origin": "New York", "destination": "London"}
13-
results = search_flights(query)
14-
self.assertGreater(len(results), 0, "Should find at least one matching flight")
15-
self.assertIn("NY100", [f["flight_number"] for f in results])
16-
17-
def test_no_results(self):
18-
query = {"origin": "Mars", "destination": "Jupiter"}
19-
results = search_flights(query)
20-
self.assertEqual(len(results), 0, "Should return no results for invalid query")
6+
def setUp(self):
7+
# Reset global ollama_llm for each test
8+
global ollama_llm
9+
ollama_llm = None
10+
11+
@patch("query_handler.OllamaLLM")
12+
def test_extract_entities_ollama_success(self, mock_ollama_llm):
13+
mock_llm_instance = Mock()
14+
mock_llm_instance.invoke.return_value = '''
15+
{"origin": "New York", "destination": "London", "flight_number": "NY100", "date": "2025-05-01", "airline": "Global Airways"}
16+
'''
17+
mock_ollama_llm.return_value = mock_llm_instance
18+
global ollama_llm
19+
ollama_llm = mock_llm_instance # Set global for OLLAMA_AVAILABLE check
20+
21+
if OLLAMA_AVAILABLE:
22+
result = extract_entities_ollama("Show me flight NY100")
23+
self.assertEqual(result["origin"], "New York")
24+
self.assertEqual(result["flight_number"], "NY100")
25+
self.assertEqual(result["destination"], "London")
26+
self.assertEqual(result["date"], "2025-05-01")
27+
self.assertEqual(result["airline"], "Global Airways")
28+
29+
@patch("query_handler.OllamaLLM")
30+
def test_extract_entities_ollama_fallback(self, mock_ollama_llm):
31+
mock_llm_instance = Mock()
32+
mock_llm_instance.invoke.side_effect = Exception("Ollama error")
33+
mock_ollama_llm.return_value = mock_llm_instance
34+
global ollama_llm
35+
ollama_llm = mock_llm_instance
36+
37+
result = extract_entities_ollama("Show me flights from New York")
38+
self.assertEqual(result["origin"], "new york") # From keyword fallback
39+
self.assertNotIn("destination", result) # No destination in simple keyword match
40+
41+
def test_extract_flight_number(self):
42+
self.assertEqual(extract_flight_number("Show me flight NY100"), "NY100")
43+
self.assertIsNone(extract_flight_number("Show me flights from New York"))
44+
45+
def test_extract_entities_from_keywords(self):
46+
result = extract_entities_from_keywords("Flights from New York with Global Airways")
47+
self.assertEqual(result["origin"], "new york")
48+
self.assertEqual(result["airline"], "global airways")
49+
self.assertNotIn("destination", result)
50+
51+
result = extract_entities_from_keywords("Flight NY100")
52+
self.assertEqual(result["flight_number"], "NY100")
53+
54+
result = extract_entities_from_keywords("Random text")
55+
self.assertEqual(result, {})
56+
57+
def test_process_query_success(self):
58+
success, message, flights = process_query("Show me flight NY100")
59+
self.assertTrue(success, "Query should succeed")
60+
self.assertEqual(message, "Here are the flights that match your criteria:")
61+
self.assertGreater(len(flights), 0, "Should return at least one flight")
62+
self.assertIn("NY100", [f["flight_number"] for f in flights])
63+
64+
def test_process_query_no_results(self):
65+
success, message, flights = process_query("Show me flight XYZ999")
66+
self.assertFalse(success, "Query should fail for invalid flight")
67+
self.assertEqual(message, "⚠️ No flights found matching your criteria. Please try again with different details.")
68+
self.assertEqual(len(flights), 0, "Should return no flights")
69+
70+
@patch("query_handler.OllamaLLM")
71+
def test_process_query_ollama_unavailable(self, mock_ollama_llm):
72+
mock_llm_instance = Mock()
73+
mock_llm_instance.invoke.side_effect = Exception("Ollama unavailable")
74+
mock_ollama_llm.return_value = mock_llm_instance
75+
global ollama_llm
76+
ollama_llm = mock_llm_instance
77+
78+
success, message, flights = process_query("Flights from New York")
79+
self.assertTrue(success, "Query should succeed with keyword fallback")
80+
self.assertEqual(message, "Here are the flights that match your criteria:")
81+
self.assertGreater(len(flights), 0, "Should return flights from New York")
2182

2283
if __name__ == "__main__":
2384
unittest.main()

0 commit comments

Comments
 (0)