|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest.mock import DEFAULT |
| 17 | +from unittest.mock import patch |
| 18 | + |
| 19 | +from server.lib.nl.common.bad_words import EMPTY_BANNED_WORDS |
| 20 | +from server.lib.nl.explore.related import generate_follow_up_questions |
| 21 | +from web_app import app |
| 22 | + |
| 23 | + |
| 24 | +class TestFollowUpQuestions(unittest.TestCase): |
| 25 | + |
| 26 | + @patch('server.routes.explore.api.related.generate_follow_up_questions', |
| 27 | + autospec=True) |
| 28 | + def test_follow_up_questions_typical(self, mock): |
| 29 | + query = "What is the rate of education in El Paso?" |
| 30 | + related_topics = [ |
| 31 | + "Educational Attachment", "School Type", "Housing", "Commute" |
| 32 | + ] |
| 33 | + |
| 34 | + expected_questions = [ |
| 35 | + 'What is the school dropout rate in El Paso?', |
| 36 | + 'What is the distribution of students by school type in El Paso?', |
| 37 | + 'What is the rate of homeownership in El Paso?', |
| 38 | + 'What is the average commute time in El Paso?' |
| 39 | + ] |
| 40 | + mock.return_value = expected_questions |
| 41 | + |
| 42 | + resp = app.test_client().post('api/explore/follow-up-questions', |
| 43 | + json={ |
| 44 | + 'q': query, |
| 45 | + 'relatedTopics': related_topics |
| 46 | + }) |
| 47 | + |
| 48 | + assert resp.status_code == 200 |
| 49 | + assert resp.json['follow_up_questions'] == expected_questions |
| 50 | + |
| 51 | + @patch('server.routes.explore.api.bad_words.is_safe', autospec=True) |
| 52 | + @patch('server.routes.explore.api.related.generate_follow_up_questions', |
| 53 | + autospec=True) |
| 54 | + def test_follow_up_questions_excludes_adversarial(self, mock_gemini, |
| 55 | + mock_safe): |
| 56 | + query = "What is the rate of education in El Paso?" |
| 57 | + related_topics = [ |
| 58 | + "Educational Attachment", "School Type", "Housing", "Commute" |
| 59 | + ] |
| 60 | + |
| 61 | + expected_questions = [ |
| 62 | + 'What is the school dropout rate in El Paso?', |
| 63 | + 'What is the distribution of students by school type in El Paso?', |
| 64 | + 'What is the rate of homeownership in El Paso?', |
| 65 | + 'What is the average commute time in El Paso?' |
| 66 | + ] |
| 67 | + mock_gemini.return_value = expected_questions |
| 68 | + #Labels do not match the actual adversarial label of each query, it is simply for testing purposes. |
| 69 | + mock_safe.side_effect = [False, True, False, True] |
| 70 | + |
| 71 | + resp = app.test_client().post('api/explore/follow-up-questions', |
| 72 | + json={ |
| 73 | + 'q': query, |
| 74 | + 'relatedTopics': related_topics |
| 75 | + }) |
| 76 | + |
| 77 | + assert resp.status_code == 200 |
| 78 | + assert resp.json['follow_up_questions'] == expected_questions[1:4:2] |
| 79 | + |
| 80 | + def test_follow_up_questions_empty_related_topics(self): |
| 81 | + query = "What is the rate of education in El Paso?" |
| 82 | + related_topics = [] |
| 83 | + resp = app.test_client().post('api/explore/follow-up-questions', |
| 84 | + json={ |
| 85 | + 'q': query, |
| 86 | + 'relatedTopics': related_topics |
| 87 | + }) |
| 88 | + |
| 89 | + assert resp.status_code == 400 |
| 90 | + assert resp.json['error'] == 'Missing related topics in request.' |
| 91 | + |
| 92 | + def test_follow_up_questions_empty_query(self): |
| 93 | + query = "" |
| 94 | + related_topics = ["Housing", "Commute"] |
| 95 | + resp = app.test_client().post('api/explore/follow-up-questions', |
| 96 | + json={ |
| 97 | + 'q': query, |
| 98 | + 'relatedTopics': related_topics |
| 99 | + }) |
| 100 | + |
| 101 | + assert resp.status_code == 400 |
| 102 | + assert resp.json['error'] == 'Missing query in request.' |
| 103 | + |
| 104 | + @patch('server.routes.explore.api.related.generate_follow_up_questions', |
| 105 | + autospec=True) |
| 106 | + def test_follow_up_questions_error_gemini_call(self, mock): |
| 107 | + query = "What is the rate of education in El Paso?" |
| 108 | + related_topics = [ |
| 109 | + "Educational Attachment", "School Type", "Housing", "Commute" |
| 110 | + ] |
| 111 | + expected_questions = [] |
| 112 | + |
| 113 | + mock.return_value = expected_questions |
| 114 | + app.config['NL_BAD_WORDS'] = EMPTY_BANNED_WORDS |
| 115 | + resp = app.test_client().post('api/explore/follow-up-questions', |
| 116 | + json={ |
| 117 | + 'q': query, |
| 118 | + 'relatedTopics': related_topics |
| 119 | + }) |
| 120 | + |
| 121 | + assert resp.status_code == 200 |
| 122 | + assert resp.json['follow_up_questions'] == expected_questions |
| 123 | + |
| 124 | + @patch('google.genai.Client', autospec=True) |
| 125 | + def test_generate_follow_up_questions_typical(self, mock_gemini): |
| 126 | + query = "What is the rate of education in El Paso?" |
| 127 | + related_topics = [ |
| 128 | + "Educational Attachment", "School Type", "Housing", "Commute" |
| 129 | + ] |
| 130 | + expected_questions = [ |
| 131 | + 'What is the school dropout rate in El Paso?', |
| 132 | + 'What is the distribution of students by school type in El Paso?', |
| 133 | + 'What is the rate of homeownership in El Paso?', |
| 134 | + 'What is the average commute time in El Paso?' |
| 135 | + ] |
| 136 | + mock_gemini.return_value.models.generate_content.return_value.parsed.questions = expected_questions |
| 137 | + app.config['LLM_API_KEY'] = "" |
| 138 | + with app.app_context(): |
| 139 | + assert expected_questions == generate_follow_up_questions( |
| 140 | + query=query, related_topics=related_topics) |
| 141 | + |
| 142 | + @patch('google.genai.Client', autospec=True) |
| 143 | + def test_generate_follow_up_questions_retry_once(self, mock_gemini): |
| 144 | + query = "What is the rate of education in El Paso?" |
| 145 | + related_topics = [ |
| 146 | + "Educational Attachment", "School Type", "Housing", "Commute" |
| 147 | + ] |
| 148 | + expected_questions = [ |
| 149 | + 'What is the school dropout rate in El Paso?', |
| 150 | + 'What is the distribution of students by school type in El Paso?', |
| 151 | + 'What is the rate of homeownership in El Paso?', |
| 152 | + 'What is the average commute time in El Paso?' |
| 153 | + ] |
| 154 | + mock_gemini.return_value.models.generate_content.return_value.parsed.questions = expected_questions |
| 155 | + mock_gemini.return_value.models.generate_content.side_effect = [ |
| 156 | + None, DEFAULT |
| 157 | + ] |
| 158 | + app.config['LLM_API_KEY'] = "" |
| 159 | + with app.app_context(): |
| 160 | + assert expected_questions == generate_follow_up_questions( |
| 161 | + query=query, related_topics=related_topics) |
| 162 | + |
| 163 | + @patch('google.genai.Client', autospec=True) |
| 164 | + def test_generate_follow_up_questions_error_request(self, mock_gemini): |
| 165 | + query = "What is the rate of education in Mountain View?" |
| 166 | + related_topics = ["Education"] |
| 167 | + mock_gemini.return_value.models.generate_content.side_effect = [ |
| 168 | + None, None, None |
| 169 | + ] |
| 170 | + app.config['LLM_API_KEY'] = "" |
| 171 | + with app.app_context(): |
| 172 | + assert [] == generate_follow_up_questions(query=query, |
| 173 | + related_topics=related_topics) |
| 174 | + |
| 175 | + def test_generate_follow_up_questions_empty_related_topics(self): |
| 176 | + query = "What is the rate of education in Mountain View?" |
| 177 | + related_topics = [] |
| 178 | + assert [] == generate_follow_up_questions(query=query, |
| 179 | + related_topics=related_topics) |
| 180 | + |
| 181 | + def test_generate_follow_up_questions_empty_query(self): |
| 182 | + query = "" |
| 183 | + related_topics = ["Housing", "Commute"] |
| 184 | + assert [] == generate_follow_up_questions(query=query, |
| 185 | + related_topics=related_topics) |
0 commit comments