Coverage for mindsdb / integrations / handlers / minds_endpoint_handler / tests / test_minds_endpoint_handler.py: 0%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1import os 

2import pytest 

3import pandas as pd 

4from unittest.mock import patch 

5 

6from tests.unit.ml_handlers.base_ml_test import BaseMLAPITest 

7 

8 

9@pytest.mark.skipif(os.environ.get('MDB_TEST_MINDS_ENDPOINT_API_KEY') is None, reason='Missing API key!') 

10class TestMindsEndpoint(BaseMLAPITest): 

11 """ 

12 Integration tests for Minds Endpoint engine. 

13 """ 

14 

15 # TODO: Should random names be generated for the project, model etc.? 

16 # TODO: Are the resources created being cleaned up after the test? 

17 

18 def setup_method(self): 

19 """ 

20 Setup test environment by creating a project and a Minds Endpoint engine. 

21 """ 

22 

23 super().setup_method() 

24 self.run_sql("CREATE DATABASE proj") 

25 self.run_sql( 

26 f""" 

27 CREATE ML_ENGINE minds_endpoint_engine 

28 FROM minds_endpoint 

29 USING 

30 minds_endpoint_api_key = '{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}'; 

31 """ 

32 ) 

33 

34 def test_create_model_raises_exception_with_invalid_model_parameter(self): 

35 """ 

36 Test for invalid parameter during model creation. 

37 """ 

38 

39 self.run_sql( 

40 f""" 

41 CREATE MODEL proj.test_minds_endpoint_invalid_parameter_model 

42 PREDICT answer 

43 USING 

44 engine='minds_endpoint_engine', 

45 model_name='this-model-does-not-exist', 

46 prompt_template='dummy_prompt_template', 

47 minds_endpoint_api_key='{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}'; 

48 """ 

49 ) 

50 with pytest.raises(Exception): 

51 self.wait_predictor("proj", "test_minds_endpoint_invalid_model") 

52 

53 def test_create_model_raises_exception_with_unknown_model_argument(self): 

54 """ 

55 Test for unknown argument during model creation. 

56 """ 

57 

58 self.run_sql( 

59 f""" 

60 CREATE MODEL proj.test_minds_endpoint_unknown_argument_model 

61 PREDICT answer 

62 USING 

63 engine='minds_endpoint_engine', 

64 prompt_template='dummy_prompt_template', 

65 minds_endpoint_api_key='{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}', 

66 evidently_wrong_argument='wrong value'; 

67 """ 

68 ) 

69 with pytest.raises(Exception): 

70 self.wait_predictor("proj", "test_minds_endpoint_unknown_argument_model") 

71 

72 def test_select_runs_no_errors_on_chat_completion_question_answering_single(self): 

73 """ 

74 Test for a valid answer to a question answering task (chat completion). 

75 """ 

76 

77 self.run_sql( 

78 f""" 

79 CREATE MODEL proj.test_minds_endpoint_single_qa 

80 PREDICT answer 

81 USING 

82 engine='minds_endpoint_engine', 

83 question_column='question', 

84 minds_endpoint_api_key='{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}'; 

85 """ 

86 ) 

87 self.wait_predictor("proj", "test_minds_endpoint_single_qa") 

88 

89 result_df = self.run_sql( 

90 """ 

91 SELECT answer 

92 FROM proj.test_minds_endpoint_single_qa 

93 WHERE question = 'What is the capital of Sweden?'; 

94 """ 

95 ) 

96 

97 assert "stockholm" in result_df["answer"].iloc[0].lower() 

98 

99 @patch("mindsdb.integrations.handlers.postgres_handler.Handler") 

100 def test_select_runs_no_errors_on_chat_completion_question_answering_bulk(self, mock_postgres_handler): 

101 """ 

102 Test for valid answers to bulk questions in a question answering task (chat completion). 

103 """ 

104 

105 df = pd.DataFrame.from_dict({"question": [ 

106 "What is the capital of Sweden?", 

107 "What is the second planet in the solar system?" 

108 ]}) 

109 self.set_handler(mock_postgres_handler, name="pg", tables={"df": df}) 

110 

111 self.run_sql( 

112 f""" 

113 CREATE MODEL proj.test_minds_endpoint_bulk_qa 

114 PREDICT answer 

115 USING 

116 engine='minds_endpoint_engine', 

117 question_column='question', 

118 minds_endpoint_api_key='{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}'; 

119 """ 

120 ) 

121 self.wait_predictor("proj", "test_minds_endpoint_bulk_qa") 

122 

123 result_df = self.run_sql( 

124 """ 

125 SELECT p.answer 

126 FROM pg.df as t 

127 JOIN proj.test_minds_endpoint_bulk_qa as p; 

128 """ 

129 ) 

130 

131 assert "stockholm" in result_df["answer"].iloc[0].lower() 

132 assert "venus" in result_df["answer"].iloc[1].lower() 

133 

134 def test_select_runs_no_errors_on_embeddings_completion_single(self): 

135 """ 

136 Test for a valid answer to a question answering task (chat completion). 

137 """ 

138 

139 self.run_sql( 

140 f""" 

141 CREATE MODEL proj.test_minds_endpoint_single_embeddings 

142 PREDICT embeddings 

143 USING 

144 engine='minds_endpoint_engine', 

145 question_column='text', 

146 mode='embedding', 

147 minds_endpoint_api_key='{self.get_api_key('MDB_TEST_MINDS_ENDPOINT_API_KEY')}'; 

148 """ 

149 ) 

150 self.wait_predictor("proj", "test_minds_endpoint_single_embeddings") 

151 

152 result_df = self.run_sql( 

153 """ 

154 SELECT embeddings 

155 FROM proj.test_minds_endpoint_single_embeddings 

156 WHERE text = 'MindsDB'; 

157 """ 

158 ) 

159 

160 assert isinstance(result_df["embeddings"].iloc[0], list)