def suggest(request): data = _parse_query_params(request.query_params, ['text', 'size']) serializer = SuggestInputSerializer(data=data) if serializer.is_valid(): results = es_interface.suggest(**serializer.validated_data) return Response(results, headers=_buildHeaders()) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_suggest_with_size__valid(self, mock_suggest): mock_suggest.return_value = self.fixture_response self.fixture_request["suggest"]["sgg"]["completion"]["size"] = 10 res = suggest(text="Loan", size=10) self.assertEqual(len(mock_suggest.call_args), 2) self.assertEqual(0, len(mock_suggest.call_args[0])) self.assertEqual(2, len(mock_suggest.call_args[1])) self.assertDictEqual( mock_suggest.call_args[1]["body"], self.fixture_request ) self.assertEqual(mock_suggest.call_args[1]["index"], "INDEX") self.assertEqual(["test 1", "test 2"], res)
def test_suggest_with_text__valid(self, mock_suggest): mock_suggest.return_value = self.fixture_response self.fixture_request["suggest"]["sgg"]["text"] = "Mortgage" res = suggest(text="Mortgage") self.assertEqual(len(mock_suggest.call_args), 2) self.assertEqual(0, len(mock_suggest.call_args[0])) self.assertEqual(2, len(mock_suggest.call_args[1])) self.assertDictEqual( mock_suggest.call_args[1]["body"], self.fixture_request ) self.assertEqual(mock_suggest.call_args[1]["index"], "INDEX") self.assertEqual(["test 1", "test 2"], res)
def test_suggest_with_no_param__valid(self, mock_suggest): mock_suggest.return_value = self.fixture_response res = suggest() mock_suggest.assert_not_called() self.assertEqual([], res)