def get(request): """ Action when calling the endpoint with GET Return list of patients :param request: request for patients list :return: json response with patients list """ pagination_args = PaginationViewUtils.get_pagination_args(request) current_path = request.build_absolute_uri() query = "" if 'query' in request.GET: query = request.GET['query'] if request.auth_user['is_admin']: all_patient_info = PatientService.list_patients( pagination_args['page_num'], pagination_args['page_size'], current_path, query) else: all_patient_info = PhysiotherapistService.list_physios_patients( request.auth_user['id'], pagination_args['page_num'], pagination_args['page_size'], current_path, query) return JsonResponse(all_patient_info, safe=False)
def test_list_patients(self, mock_pag_service_valid_page, mock_pag_service_get_results, mock_patient_list_serializer, mock_query_patients): """ Tests if method returns all patients successfully :param mock_pag_service_valid_page: Mock of page number validation method :param mock_pag_service_get_results: Mock of paginated results method :param mock_patient_list_serializer: Mock of patient list serializer :param mock_query_patients: Mock of list of searched patients """ with patch.object(Paginator, '__init__', return_value=None) as mock_paginator: with patch.object(Paginator, 'page', return_value=Mock()) as mock_paginator_page: pagination_args = PaginationDataRepository.get_valid_pagination( ) mock_results = mock_pag_service_get_results.return_value mock_patients_list = mock_query_patients.return_value return_value = PatientService.list_patients( pagination_args.GET['page_num'], pagination_args.GET['page_size'], pagination_args.build_absolute_uri(), self.query) mock_query_patients.assert_called_once_with( Patient.objects, self.query, mock.ANY) mock_paginator.assert_called_once_with( mock_patients_list, pagination_args.GET['page_size']) mock_pag_service_valid_page.assert_called_once_with( mock.ANY, int(pagination_args.GET['page_num'])) mock_patient_list_serializer.assert_called_once_with( mock_paginator_page(pagination_args.GET['page_num'])) self.assertEqual(return_value, mock_results)