jeudi 4 juin 2015

Python Django REST API test

As my first attempt in python to create a API test, my test file currently looks like this:

from django.test import TestCase, RequestFactory
from customer.models import Customer, CustomerStatus
from customer.views import CustomerPartialView
from rest_framework.test import APIRequestFactory


import pprint
from django.utils import timezone

class CustomerTest(TestCase) :

    def setUp(self) :
        self.factory = RequestFactory()

        self.cust_status = CustomerStatus.objects.create(
            status_id=1,name="banned",description="test desc" )

        self.customer = Customer.objects.create(
            guid='b27a3251-56e0-4870-8a03-27b0e92af9e5',
            created_date=timezone.now(),
            status_id=1,first_name='Hamster')



    def test_get_customer(self) :
        """                                                                                                         
        Ensure we can get a customer from db through the API                                                        
        """
        factory = APIRequestFactory()
        request = factory.get('/customer/b27a3251-56e0-4870-8a03-27b0e92af9e5')
        reponse = CustomerPartialView(request)

#in here I need something to check:
#response.data.first_name = "Hamster"

What I like to achieve is to insert dummy data into the database and then using APIRequestFactory to retrieve an individual customer record and make sure their first name matches what I expect.

I have managed to get this far but not sure what to do next.

My questions are:

  1. Am I on the right track?
  2. How do I test my result i.e. response.data.first_name = hamster ?
  3. Is there a better way of doing what I am trying to achieve

I am newbie to python so I apologise in advance if there are any major fails in my code.

Thanks

Aucun commentaire:

Enregistrer un commentaire