0

I am just trying to test the CRUD operations for Flask REST service.

  1. In CREATEoperation, i want to create a new object.
  2. In UPDATE operation, i need to get the id of the object created in Create operation and update it.
  3. In DELETE operation, i need to delete the object created in Create operation.

How can i approach this?

The current code looks something like this

class BaseTestCase(TestCase):
    def create_app(self):
        return create_app('testing')

    def setUp(self):
        self.response = self.client.get('/api/v1/list_portfolios').json
        self.portfolio_id = self.response['respbody']['Portfolios'][0]['_id']
        self.view_response = self.client.get('/api/v1/view_portfolio/' + self.portfolio_id).json

class ModelTestAPI(BaseTestCase):
    def test_model_create(self):
        model_create_data = {
            "PortfolioId": "558d0e575dddb726b8cd06bc",
            "ModelName": "New Model",
            "ModelLifetimePeriod": "Month",
            "ModelLifetimeDuration": 12
        }
        response = self.client.post('/api/v1/model/create', data=dumps(model_create_data),
                                    content_type='application/json').json
        portfolio_model_id = response['respbody']['_id']
        print(portfolio_model_id)

        new_model_dict = model_create_data.copy()
        new_model_dict['_id'] = portfolio_model_id

        new_json = response['respbody'].copy()
        new_json.pop('CreateDate', None)
        new_json.pop('LastUpdateDate', None)

        self.assertDictEqual(new_model_dict, new_json)

    def test_model_update(self):
        data = {
            "ModelName": "UPDATE New Model",
            "ModelLifetimePeriod": "Month",
            "ModelLifetimeDuration": 6
        }
        portfolio_model_id = self.view_response['respbody']['PortfolioModels'][-1]['_id']
        json = self.client.put('/api/v1/model/' + portfolio_model_id, data=dumps(data),
                               content_type='application/json').json

        data['_id'] = portfolio_model_id
        new_json = json['respbody'].copy()
        new_json.pop('CreateDate', None)
        new_json.pop('LastUpdateDate', None)
        new_json.pop('PortfolioId', None)

        self.assertDictEqual(data, new_json)

    def test_model_delete(self):
        portfolio_model_id = self.view_response['respbody']['PortfolioModels'][-1]['_id']
        json = self.client.delete('http://localhost:5000/api/v1/model/' + portfolio_model_id).json
        expected_dict = {'success': True}
        self.assertDictEqual(expected_dict, json['respbody'])
1
  • What framework are you using?, and what is the current code not doing that it should do? Commented Jul 1, 2015 at 8:21

1 Answer 1

1

According to my experience, first you build a web service, second starting to write unittest

Web service for examples:

from rest_framework.views import APIView
class view_example(APIView):
    def get(self, request, format=None):
        ...
    def post(self, request, format=None):
        ...
    def put(self, request, format=None):
        ...
    def delete(self, request, format=None):
        ...

To write unittest when you are sure that you register the view in urls.py.

from rest_framework.test import APITestCase

class ViewExampleTests(APITestCase):
    def test_create(self):
        ...
        response = self.client.post(url, data, format='json')
        ...
    def test_update(self):
        ...
        response = self.client.put(url, data, format='json')
        ...
    def test_delete(self):
        ...
        response = self.client.delete(url, data, format='json')
        ...
    def test_read(self):
        ...
        response = self.client.get(url, data, format='json')
        ...

However, It's substantial completion of the works.

Sign up to request clarification or add additional context in comments.

1 Comment

Namely, the one added feature of APITestCase over vanilla Django testcast is it takes a dictionary and encodes it into whatever you set as settings.TEST_REQUEST_DEFAULT_FORMAT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.