python - Django 客户端不会在测试中删除

标签 python django testing

您好,我现在正在使用 Django REST3.5.3。我可以使用可浏览的 API 完美地完成 CRUD。

我的问题是我的测试没有通过。

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()

这是我的 pdb 控制台:

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">

我是不是错过了什么?

最佳答案

我使用了错误的客户端。我必须使用 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()

关于python - Django 客户端不会在测试中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42489343/

相关文章:

python - 如何标记化,拆分相邻的数字字母?

django 在生产服务器上打印信息

django - django 中管理模型类的详细名称

python - 在 python 中将列标题添加到 csv

python utf-8编码抛出UnicodeDecodeError尽管 "errors = '替换'“

javascript - Django + Javascript - 我们如何验证执行 Javascript 的 Django 值不为空?

python Selenium : Safari returns different text for span than Chrome and Firefox

javascript - Angular Jasmine : 'undefined' is not an object - broadcast within timeout - error

c++ - 如何使用 CMake 对库的私有(private)特性 (TDD) 进行单元测试

python - 如何在 python tkinter 中以编程方式打开菜单?