2017-02-27 1 views
0

안녕하세요. 이제 Django REST3.5.3을 사용하고 있습니다. 나는 브라우저 API로 CRUD를 완벽하게 할 수있다.
Django 클라이언트가 테스트에서 삭제하지 않습니다.

내 문제는 통과하지 못합니다.

apps/price_list_excel_files/tests.py . 
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB set_trace (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 
> /Users/el/Code/siam-sbrand/portal/apps/price_list_excel_files/tests.py(37)test_mgmt_user_remove_excel() 
-> assert 0 == PriceListExcelFile.objects.count() 
(Pdb) list 
32   url = '/api/price-list-excel-files/' + str(excel_id) + '/' 
33   res2 = client.delete(url, data={'format': 'json'}) 
34   import pdb; 
35   pdb.set_trace() 
36 
37 ->  assert 0 == PriceListExcelFile.objects.count() 
[EOF] 
(Pdb) res2 
*** KeyError: 'content-type' 
(Pdb) url 
'/api/price-list-excel-files/2/' 
(Pdb) res3 = client.delete(url) 
(Pdb) res3 
<Response status_code=404, "application/json"> 

내가 뭔가를 놓치고 있습니까 : 내 pdb 콘솔은 여기에

tests.py

import os 

from django.test import Client 

from apps.price_list_excel_files.models import PriceListExcelFile 


def upload_excel(user: str, passwd: str) -> tuple: 
    client = Client() 
    client.login(username=user, password=passwd) 

    dir_path = os.path.dirname(os.path.realpath(__file__)) 
    with open(dir_path + '/mixed_case.xlsx', 'rb') as fp: 
     response = client.post(
      '/api/price-list-excel-files/', 
      {'file': fp}, 
      format='multipart' 
     ) 
    return client, response 


def test_mgmt_user_upload_excel(prepare_mgmt_users): 
    client, response = upload_excel("John", "johnpassword") 
    assert 201 == response.status_code 
    assert 1 == PriceListExcelFile.objects.count() 


# TODO: Fix this testcase 
def test_mgmt_user_remove_excel(prepare_mgmt_users): 
    client, response = upload_excel("John", "johnpassword") 
    excel_id = response.data.get('id') 
    url = '/api/price-list-excel-files/' + str(excel_id) + '/' 
    res2 = client.delete(url, data={'format': 'json'}) 
    import pdb; 
    pdb.set_trace() 

    assert 0 == PriceListExcelFile.objects.count() 

입니까?

답변

0

잘못된 클라이언트를 사용합니다. Djano REST 클라이언트를 사용해야합니다.

from rest_framework.test import APIClient 
def test_mgmt_user_remove_excel(prepare_mgmt_users): 
    client, response = upload_excel("John", "johnpassword") 
    excel_id = response.data.get('id') 
    url = '/api/price-list-excel-files/' + str(excel_id) + '/' 

    client2 = APIClient() 
    client2.login(username="John", password="johnpassword") 

    res2 = client2.delete(url) 
    assert 0 == PriceListExcelFile.objects.count() 
관련 문제