Example #1
0
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)
Example #2
0
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)