def assertQueryResultMultiTool(self, query, mock_fetch_results, query_results_expected, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query returns a list with multiple tools and that resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. query_results_expected: The expected winning tools that the resolver returns. tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = tool_fetcher.ToolFetcher().fetch mock_fetch.return_value = mock_fetch_results query_results_actual = self.resolver.answer_query(query) self.assertSetEqual(set(query_results_expected), set(query_results_actual)) mock_fetch.assert_called_with(tool_properties_expected)
def assertQueryResultWithRandomShuffle(self, query, mock_fetch_results, query_results_expected, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query finds a list of tool candidates to return and then randomly selects a subset of those tools as the winners. Also asserts that the resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. query_results_expected: Expected results from calling resolver.answer_query(). tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = tool_fetcher.ToolFetcher().fetch mock_fetch.return_value = mock_fetch_results # Mock out random behavior to allow deterministic test results with mock.patch('random.shuffle') as mock_shuffle: # Change the random shuffle to a deterministic list reverse mock_shuffle.side_effect = lambda x: x.reverse() query_results_actual = self.resolver.answer_query(query) self.assertSetEqual(set(query_results_expected), set(query_results_actual)) mock_fetch.assert_called_with(tool_properties_expected)
def testAnswerQueryWhenNoToolsMatchToolId(self): tool_id = 'non_existent_tool' query = lookup_query.LookupQuery() query.tool_id = tool_id # Simulate no matching tools tool_fetcher.ToolFetcher().fetch.return_value = [] # Result should be None when there are no matches. self.assertIsNone(self.resolver.answer_query(query))
def setUp(self): tool_fetcher_datastore_patch = mock.patch.object( tool_fetcher, 'ToolFetcherDatastore', autospec=True) self.addCleanup(tool_fetcher_datastore_patch.stop) tool_fetcher_datastore_patch.start() tool_fetcher_memcache_patch = mock.patch.object( tool_fetcher, 'ToolFetcherMemcache', autospec=True) self.addCleanup(tool_fetcher_memcache_patch.stop) tool_fetcher_memcache_patch.start() self.fetcher = tool_fetcher.ToolFetcher()
def testAnswerReturnsNoneWhenCountryIsNotSpecified(self): query = lookup_query.LookupQuery() query.tool_id = _TOOL_ID # query omits country attribute candidate_tools = (_createSliverTool(_TOOL_ID), _createSliverTool(_TOOL_ID)) mock_fetch = tool_fetcher.ToolFetcher().fetch mock_fetch.return_value = candidate_tools query_results = self.resolver.answer_query(query) # Result should be None when there are no matches. self.assertIsNone(query_results)
def assertQueryResultMultiToolWithRandomSample(self, query, mock_fetch_results, filtered_tool_candidates, sample_size, tool_properties_expected): """Assert that the resolver result matches expected values. Assert that calling resolver.answer_query finds a list of tool candidates to return and then randomly selects a single tool as the winner. Also asserts that the resolver fetched tools from the db using the correct criteria. Args: query: LookupQuery instance based on the client's query. mock_fetch_results: Mock results from querying the db. filtered_tool_candidates: The expected candidate tools from which the resolver will randomly pick a winner. sample_size: The number of randomly selected elements expected in the final result. tool_properties_expected: Expected tool properties that resolver used to retrieve tools from the db. """ mock_fetch = tool_fetcher.ToolFetcher().fetch mock_fetch.return_value = mock_fetch_results # Mock out random behavior to allow deterministic test results with mock.patch('random.sample') as mock_random: # Make random.sample yield the k last elements of the set mock_random.side_effect = lambda x, k: x[-k:] query_results_expected = filtered_tool_candidates[-sample_size:] query_results_actual = self.resolver.answer_query(query) self.assertSequenceEqual(query_results_expected, query_results_actual) # Make sure that the random selection was between the expected # candidate tools, after any filtering self.assertSequenceEqual(filtered_tool_candidates, mock_random.call_args[0][0]) mock_fetch.assert_called_with(tool_properties_expected)
def __init__(self): self.tool_fetcher = tool_fetcher.ToolFetcher()