11import 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
55class 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
2392if __name__ == "__main__" :
2493 unittest .main ()
0 commit comments