Python Django REST API test -
as first attempt in python create api test, test file looks this:
from django.test import testcase, requestfactory customer.models import customer, customerstatus customer.views import customerpartialview rest_framework.test import apirequestfactory import pprint 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 can customer db through api """ factory = apirequestfactory() request = factory.get('/customer/b27a3251-56e0-4870-8a03-27b0e92af9e5') reponse = customerpartialview(request) #in here need check: #response.data.first_name = "hamster"
what achieve insert dummy data database , using apirequestfactory retrieve individual customer record , make sure first name matches expect.
i have managed far not sure next.
my questions are:
- am on right track?
- how test result i.e.
response.data.first_name = hamster ?
- is there better way of doing trying achieve
i newbie python apologise in advance if there major fails in code.
thanks
if understand right think problem you're using response.data.first_name
instead of response.data['first_name']
, response.data dict.
so should like:
self.assertequal(response.data['first_name'], 'hamster')
or, i'd recommened:
self.assertequal(response.data['first_name'], self.customer.first_name)
i'd think it's bit neater change request url '/customer/%s' % self.customer.guid
saves re-writing string.
Comments
Post a Comment