def update_service(self): get_service_obj = GetService() get_service_obj.set_pk(self.pk) airplane = get_service_obj.get_instance_service() serializer = AirplaneSerializer(airplane, data=self.request.data, partial=True) if serializer.is_valid(): serializer.save() return serializer.data, status.HTTP_200_OK return serializer.errors, status.HTTP_400_BAD_REQUEST
class AirplaneDetail(APIView): def __init__(self, **kwargs): super().__init__(**kwargs) self.get_service_obj = GetService() self.update_service_obj = UpdateService() self.delete_service_obj = DeleteService() def get(self, request, pk, format=None): self.get_service_obj.set_pk(pk) data = self.get_service_obj.get_detail_service() return Response(data) def put(self, request, pk, format=None): self.update_service_obj.set_request_and_pk(request, pk) data, status = self.update_service_obj.update_service() return Response(data, status=status) def delete(self, request, pk, format=None): self.delete_service_obj.set_pk(pk) status = self.delete_service_obj.delete_service() return Response(status=status)
def setUp(self): # Getting started by adding an airplane instance. self.factory = RequestFactory() self.view = views.AirplaneList.as_view() self.uri = 'airplanes/' self.data = {"airplane_id": 1, "passengers": 100} self.request = self.factory.post(self.uri, data=self.data, format='json') self.response = self.view(self.request) # For testing get service. self.get_obj = GetService() self.pk = 1 self.get_obj.set_pk(self.pk) self.result = { "airplane_id": 1, "passengers": 100, "fuel_consumption": 0.20, "flight_time": 1000.00 }
class TestGetService(TestCase): def setUp(self): # Getting started by adding an airplane instance. self.factory = RequestFactory() self.view = views.AirplaneList.as_view() self.uri = 'airplanes/' self.data = {"airplane_id": 1, "passengers": 100} self.request = self.factory.post(self.uri, data=self.data, format='json') self.response = self.view(self.request) # For testing get service. self.get_obj = GetService() self.pk = 1 self.get_obj.set_pk(self.pk) self.result = { "airplane_id": 1, "passengers": 100, "fuel_consumption": 0.20, "flight_time": 1000.00 } def test_get_and_set_pk(self): self.assertEqual(self.get_obj.get_pk(), self.pk) def test_get_instance_service(self): self.assertIsInstance(self.get_obj.get_instance_service(), Airplane) def test_get_service(self): self.assertEqual(len(self.get_obj.get_service()), 1) def test_get_detail_service(self): self.assertEqual(self.get_obj.get_detail_service()['airplane_id'], 1)
class AirplaneList(APIView): def __init__(self, **kwargs): super().__init__(**kwargs) self.get_service_obj = GetService() self.post_service_obj = PostService() def get(self, request, format=None): data = self.get_service_obj.get_service() return Response(data) def post(self, request, format=None): self.post_service_obj.set_request(request) data, status = self.post_service_obj.post_service() return Response(data, status=status)
def __init__(self, **kwargs): super().__init__(**kwargs) self.get_service_obj = GetService() self.post_service_obj = PostService()
def __init__(self, **kwargs): super().__init__(**kwargs) self.get_service_obj = GetService() self.update_service_obj = UpdateService() self.delete_service_obj = DeleteService()
def delete_service(self): get_service_obj = GetService() get_service_obj.set_pk(self.pk) airplane = get_service_obj.get_instance_service() airplane.delete() return status.HTTP_204_NO_CONTENT