Django之TestCase单元测试

单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。

Posted by dzt on February 19, 2019

前言

Django的单元测试非常简单,因为Django有现成的框架TestCase可以使用 from django.test import TestCase

setUp为测试前执行

tearDown为测试最后执行

其他函数必须为test_开头,如 test_fun, test_login…

到处models, 可以测试数据库的增删改查

我测试的内容为APIVIEW

所以引用了Client

以此来使用get, post, put, delete请求访问

c = Client()
response = c.post('/usermanager/login/', {"username": "dzt", "password": "123"})

表示 使用post请求 /usermanager/login/目录,带上json格式的数据{“username”: “dzt”, “password”: “123”})

self.assertEqual(response.json()['error_no'], 0)

表示判断response的error_no是否等于0

源码

# -*- coding: utf-8 -*-
from django.test import TestCase
from django.test import Client
from usermanager.models import User
from common.models import EquipmentOneNetInfo, AgentCompany
from equipmentsetting.models import EquipmentUserInfo, EquipmentUse, Alarm, Contact, ElecFence, ClassMute, SosNumbers
from message.models import MessageInfo, AppMessageInfo
import json


class UsermanagerTest(TestCase):
    def setUp(self):
        User.objects.create_superuser(username='dzt', password='123', email='dzt@qq.com')
        User.objects.create_user(username='23370763530', password='123')
        User.objects.create_user(username='33370763530', password='123')
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)

    def tearDown(self):
        print('usermanagerTest over')

    # # 注册
    # def test_a_register(self):
    #     c = Client()
    #     response1 = c.post('/usermanager/register/', {
    #         "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
    #     })
    #     self.assertEqual(response.json()['error_no'], 0)

    # 登录
    def test_login(self):
        c = Client()
        response = c.post('/usermanager/login/', {"username": "dzt", "password": "123"})
        response1 = c.post('/usermanager/login/', {"username": "13370763530", "password": "123"})
        self.assertEqual(response.json()['error_no'], 0)
        self.assertEqual(response1.json()['error_no'], 0)

    # 检查用户是否存在
    def test_checkUser(self):
        c = Client()
        response = c.post('/usermanager/checkUser/', {"username": "13370763530"})
        self.assertEqual(response.json()['error_no'], 0)

    # 重设密码
    def test_resetPassword(self):
        c = Client()
        response = c.post('/usermanager/resetPassword/', {"username": "13370763530", "newpassword": "123"})
        self.assertEqual(response.json()['error_no'], 0)

    # 检查是否能登录
    def test_checkLogin(self):
        c = Client()
        response = c.get('/usermanager/checkLogin/?username=13370763530')
        self.assertEqual(response.json()['error_no'], 0)

    # 查询用户信息
    def test_userInfo_GET(self):
        c = Client()
        response = c.get('/usermanager/userInfo/?username=13370763530')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改用户信息
    def test_userInfo_PUT(self):
        c = Client()
        response = c.put('/usermanager/userInfo/', json.dumps({
            "username": "13370763530", "nickname": "111", "shortphone": "123"
        }), content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 微信登录
    def test_loginWeixin(self):
        c = Client()
        response = c.post('/usermanager/loginWeixin/', {
            "username": "13370763530", "password": "123", "openid": "5"
        })
        self.assertEqual(response.json()['error_no'], 0)

    # 退出登录
    def test_logout(self):
        c = Client()
        response = c.post('/usermanager/logout/', {
            "username": "13370763530"
        })
        self.assertEqual(response.json()['error_no'], 0)

    # openid查询username
    def test_ByOpenId(self):
        c = Client()
        response = c.get('/usermanager/usernameByOpenId/?openid=5')
        self.assertEqual(response.json()['error_no'], 0)

    # 微信解绑
    def test_unbindWeixin(self):
        c = Client()
        response = c.post('/usermanager/unbindWeixin/', {"openid": "5"})
        self.assertEqual(response.json()['error_no'], 0)

    # 查询或创建token
    def test_token(self):
        c = Client()
        response = c.post('/usermanager/api-token-auth/', {"username": "13370763530", "password": "123"})
        self.assertEqual(response.status_code, 200)


class aftersaleTest(TestCase):
    def setUp(self):
        print("aftersaleTest start")
        c = Client()
        response3 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        response = c.post('/aftersale/saledWatchInfo/', {"imei": "111"})
        self.assertEqual(response.json()['error_no'], 0)
        self.assertEqual(response3.json()['error_no'], 0)

    def tearDown(self):
        print('aftersaleTest over')

    # # 登录或注册获取token
    # def test_login(self):
    #     c = Client()
    #     response = c.post('/aftersale/login/', {"username": "93370763530", "password": "123"})
    #     self.assertEqual(response.json()['error_no'], 0)

    # # 售后服务添加imei
    # def test_saledWatchInfo_POST(self):
    #     c = Client()
    #     response = c.post('/aftersale/saledWatchInfo/', {"imei": "111"})
    #     self.assertEqual(response.json()['error_no'], 0)

    # 售后服务查询
    def test_saledWatchInfo_GET(self):
        c = Client()
        response = c.get('/aftersale/saledWatchInfo/?imei=111')
        self.assertEqual(response.json()['error_no'], 0)

    # # imei模糊查询
    # def test_saledWatchInfoList_GET(self):
    #     c = Client()
    #     response2 = c.post('/aftersale/login/', {"username": "13370763530", "password": "123"})
    #     print(response2.json()["token"])
    #     response = c.get('/aftersale/saledWatchInfoList/?imei=111', Authorization="token {}".format(response2.json()["token"]))
    #     self.assertEqual(response.json()['error_no'], 0)


class chatTest(TestCase):
    def setUp(self):
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)
        AgentCompany.objects.create(companyname="cmx", password="123456", )
        EquipmentOneNetInfo.objects.create(imei="362523432425471", sn="20180423192658",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")
        EquipmentUserInfo.objects.create(imei_id="362523432425471", nickname="dzt")
        EquipmentUse.objects.create(imei_id="362523432425471", admin_user_id="13370763530", isactive=True,
                                    watchinfo_id="362523432425471", relation="xx")

    # getUserToken
    def test_getUserToken(self):
        c = Client()
        response = c.post('/chat/getUserToken/', {"username": "13370763530"})
        self.assertEqual(response.json()['error_no'], 0)

    # getWatchToken
    def test_getWatchToken(self):
        c = Client()
        response = c.post('/chat/getWatchToken/', {"imei": "362523432425471"})
        self.assertEqual(response.json()['error_no'], 0)

    # 获取家庭群组
    def test_familyGroup_POST(self):
        c = Client()
        response = c.post('/chat/familyGroup/', {"imei": "362523432425471"})
        self.assertEqual(response.json()['error_no'], 0)

    # 查询家庭群组
    def test_familyGroup_GET(self):
        c = Client()
        response = c.get('/chat/familyGroup/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 查询视频剩余分钟数
    def test_videochatminutes_GET(self):
        c = Client()
        response = c.get('/chat/videochatminutes/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 使用视频后修改分钟数
    def test_videochatminutes_PUT(self):
        c = Client()
        response = c.put('/chat/videochatminutes/', json.dumps({"imei": "362523432425471", "useminutes": "0"}),
                         content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 获取视频充值价格表
    def test_videochatprice(self):
        c = Client()
        response = c.get('/chat/videochatprice/')
        self.assertEqual(response.json()['error_no'], 0)


class commonTest(TestCase):
    def setUp(self):
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)
        AgentCompany.objects.create(companyname="cmx", password="123456")
        EquipmentOneNetInfo.objects.create(imei="362523432425471", sn="20180423192658",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")
        EquipmentUserInfo.objects.create(imei_id="362523432425471", nickname="dzt", gender="man")
        EquipmentUse.objects.create(imei_id="362523432425471", admin_user_id="13370763530", isactive=True,
                                    watchinfo_id="362523432425471", relation="")

        print('commonTest start')

    # 查询app_url
    def test_trueUrl_GET(self):
        c = Client()
        response = c.get('/common/trueUrl/?app_name=app_name&app_version=app_version&app_channel=app_channel')
        self.assertEqual(response.json()['error_no'], 0)

    # 新增app_url
    def test_trueUrl_POST(self):
        c = Client()
        response = c.post('/common/trueUrl/', {"app_name": "app_name", "app_version": "app_version",
                                               "server_url": "server_url"})
        self.assertEqual(response.json()['error_no'], 0)

    # OneNet设备查询
    def test_equipmentOnenetInfo_GET(self):
        c = Client()
        response = c.get('/common/equipmentOnenetInfo/?imei=362523432425471&sn=20180423192658')
        self.assertEqual(response.json()['error_no'], 0)

    # 向OneNet注册新设备(pass)
    def test_equipmentOnenetInfo_POST(self):
        print("向OneNet注册新设备(pass)")

    # 用户绑定信息查询
    def test_equipmentOnenetInfoMobile(self):
        c = Client()
        response = c.get('/common/equipmentOnenetInfoMobile/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备绑定 修改 删除 涉及到融云创建删除群组 pass
    def test_equipmentUse(self):
        print("设备绑定 修改 删除 涉及到融云创建删除群组 pass")

    # 设备绑定信息查询ByUser
    def test_equipmentUseByUser(self):
        c = Client()
        response = c.get('/common/equipmentUseByUser/?admin_user=13370763530')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备绑定ByAdmin
    def test_equipmentUseByAdmin(self):
        print("设备绑定涉及到融云创建删除群组 pass")

    # OneNet发送命令
    def test_sendCommand(self):
        print("sendCommand 参数构造不正确")

    # sn查询imei
    def test_checkImei(self):
        c = Client()
        response = c.get('/common/checkImei/?imei=20180423192658')
        self.assertEqual(response.json()['error_no'], 26)

    # 发送验证码
    def test_sendVeriCode(self):
        c = Client()
        response = c.post('/common/sendVeriCode/', {"phone": "13370763530"})
        self.assertEqual(response.json()['error_no'], 0)

    # 查询公司下所有设备
    def test_equipmentListByCompany(self):
        c = Client()
        response = c.get('/common/equipmentListByCompany/?companyname=cmx')
        self.assertEqual(response.json()['error_no'], 0)

    # 根据用户名查询绑定设备
    def test_equipmentActiveUseByUser(self):
        c = Client()
        response = c.get('/common/equipmentActiveUseByUser/?admin_user=13370763530')
        print(response)
        self.assertEqual(response.json()['error_no'], 0)

    # 检查是否需要更新
    def test_checkupdate(self):
        c = Client()
        response = c.get('/common/checkupdate/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 获取广告
    def test_getBanners(self):
        c = Client()
        response = c.get('/common/getBanners/')
        self.assertEqual(response.json()['error_no'], 0)

    # 添加集成商信息
    def test_agentInfo(self):
        c = Client()
        response = c.post('/common/agentInfo/', {"companyname": "xxx", "password": "1231", "showname": "",
                                                 "contact": "", "phone": "", "address": ""})
        self.assertEqual(response.json()['error_no'], 0)

    # 获取版本
    def test_getServerVersion(self):
        c = Client()
        response = c.get('/common/getServerVersion/')
        self.assertEqual(response.json()['error_no'], 0)

    # 获取设备设置信息
    def test_getAppFuncs_GET(self):
        c = Client()
        response = c.get('/common/getAppFuncs/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改设备设置信息
    def test_getAppFuncs_PUT(self):
        c = Client()
        response = c.put('/common/getAppFuncs/?imei=362523432425471', json.dumps({"imei": "362523432425471",
                                                                                  "is_support_addressbook": True,
                                                                                  "is_support_dial": True,
                                                                                  "is_support_sport": True,
                                                                                  "is_support_findequipment": True,
                                                                                  "is_support_alarm": True,
                                                                                  "is_support_classmute": True,
                                                                                  "is_support_remotepower": True,
                                                                                  "is_support_sos_settings": True,
                                                                                  "is_support_funcswitch": True,
                                                                                  "is_support_quickdial": False,
                                                                                  "is_support_historytrack": True,
                                                                                  "is_support_wechat": False,
                                                                                  "is_support_videochat": False,
                                                                                  "is_support_audiochat": True,
                                                                                  "is_support_album": False,
                                                                                  "is_support_remote_camera": False,
                                                                                  "is_support_remote_record": False,
                                                                                  "is_support_remote_listen": True,
                                                                                  "is_support_english_learning": False,
                                                                                  "is_support_reserve_power": False,
                                                                                  "is_support_data_limit": False,
                                                                                  "is_support_timing_switch": False,
                                                                                  "is_support_child_guard": False,
                                                                                  "is_support_magic_answer": True,
                                                                                  "is_support_recharge": False,
                                                                                  "is_support_wifi_setting": False,
                                                                                  "is_support_mifi": False}),
                         content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 查询自动下载信息
    def test_autodownload_GET(self):
        c = Client()
        response = c.get('/common/autodownload/')
        self.assertEqual(response.json()['error_no'], 0)

    # 记录跑流量信息
    def test_autodownload_POST(self):
        c = Client()
        response = c.post('/common/autodownload/', {"imei": "362523432425471"})
        self.assertEqual(response.json()['error_no'], 0)

    # SHOW_RECHARGE
    def test_showrecharge(self):
        c = Client()
        response = c.get('/common/showrecharge/')
        self.assertEqual(response.json()['error_no'], 0)

    # 和固话推送
    def test_hecallnotice(self):
        c = Client()
        response = c.post('/common/hecallnotice/', json.dumps({"type": "001", "msisdn": "188xxxxxxxx", "devices": [
            {"deviceId": "CMCC-31504-069bd4eeaac"}
        ], "appkey": "appkey", "secret": "secret"}), content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)


class commanderTest(TestCase):
    def setUp(self):
        print('所有都涉及到onenet发送命令 pass')


class customerTest(TestCase):
    def setUp(self):
        AgentCompany.objects.create(companyname="cmx", password="123456", )
        c = Client()
        response = c.get('/customer/login/')
        self.assertEqual(response.status_code, 200)
        print('customerTest')

    def test_appdownload(self):
        c = Client()
        response = c.get('/customer/appdownload/')
        self.assertEqual(response.status_code, 200)

    def test_current_datetime(self):
        c = Client()
        response = c.get('/customer/current_datetime/')
        self.assertEqual(response.status_code, 200)

    def test_login(self):
        c = Client()
        response = c.get('/customer/login/')
        self.assertEqual(response.status_code, 200)

    def test_login_action_POST(self):
        c = Client()
        response = c.post('/customer/login_action/', {"agentname": "cmx", "password": "123456"})
        self.assertEqual(response.status_code, 302)

    def test_logout(self):
        c = Client()
        response = c.get('/customer/logout/')
        self.assertEqual(response.status_code, 302)

    def test_bind_list(self):
        c = Client()
        response = c.get('/customer/bind_list/')
        self.assertEqual(response.status_code, 302)

    def test_remove_bind_action(self):
        print('删除绑定访问的是/common/equipmentUse/')


class equipmentsettingTest(TestCase):
    def setUp(self):
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)
        AgentCompany.objects.create(companyname="cmx", password="123456")
        EquipmentOneNetInfo.objects.create(imei="362523432425471", sn="20180423192658",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")
        EquipmentOneNetInfo.objects.create(imei="111222333444555", sn="20180423192658",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")
        EquipmentUserInfo.objects.create(imei_id="362523432425471", nickname="dzt", gender="man")
        EquipmentUserInfo.objects.create(imei_id="111222333444555", nickname="dzt", gender="man")
        EquipmentUse.objects.create(imei_id="362523432425471", admin_user_id="13370763530", isactive=True,
                                    watchinfo_id="362523432425471", relation="")
        Alarm.objects.create(imei_id="362523432425471")
        Alarm.objects.create(imei_id="362523432425471")
        Alarm.objects.create(imei_id="362523432425471")
        Contact.objects.create(imei_id="362523432425471", name="test", phone="13370763530")
        ElecFence.objects.create(imei_id="362523432425471", name="test", radius=100, enable=1)
        ClassMute.objects.create(imei_id="362523432425471", title="test")
        ClassMute.objects.create(imei_id="362523432425471", title="test")
        ClassMute.objects.create(imei_id="362523432425471", title="test")
        SosNumbers.objects.create(imei_id="362523432425471")

    # 闹钟查询
    def test_alarm_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/alarm/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 闹钟修改
    def test_alarm_PUT(self):
        print('需要onenet发送命令修改 pass')
        # c = Client()
        # response = c.put('/equipmentsetting/alarm/', json.dumps({"imei": "362523432425471", "data": [
        #         {
        #             "imei": "362523432425471",
        #             "time": "00:00",
        #             "al_repeat": "1111100",
        #             "ring": "1",
        #             "label": "alarm1",
        #             "enable": False
        #         },
        #         {
        #             "imei": "362523432425471",
        #             "time": "00:00",
        #             "al_repeat": "1111100",
        #             "ring": "1",
        #             "label": "alarm2",
        #             "enable": False
        #         },
        #         {
        #             "imei": "362523432425471",
        #             "time": "00:00",
        #             "al_repeat": "1111100",
        #             "ring": "1",
        #             "label": "alarm2",
        #             "enable": False
        #         }
        #     ]
        # }), content_type="application/json")
        # self.assertEqual(response.json()['error_no'], 0)

    # 查询设备联系人
    def test_contracts_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/contracts/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改设备联系人
    def test_contracts_PUT(self):
        print('需要onenet发送数据 pass')

    # 电子围栏
    def test_elecfence_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/elecfence/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备详细信息查询
    def test_equipmentInfo_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/equipmentInfo/?imei=362523432425471&admin_user=13370763530')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备详细信息修改
    def test_equipmentInfo_PUT(self):
        print('修改涉及到融云修改群组名字 pass')

    # 查询用户的设备列表
    def test_equipmentInfoList(self):
        c = Client()
        response = c.get('/equipmentsetting/equipmentInfoList/?admin_user=13370763530')
        self.assertEqual(response.json()['error_no'], 0)

    # 上课禁用查询
    def test_classmute(self):
        c = Client()
        response = c.get('/equipmentsetting/classmute/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改上课禁
    def test_classmute_PUT(self):
        print('需要onenet发送数据 pass')

    # 贴心设置查询
    def test_goodsetting(self):
        c = Client()
        response = c.get('/equipmentsetting/goodsetting/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 贴心设置修改
    def test_goodsetting_PUT(self):
        print('需要onenet发送数据 pass')

    # imei查询设备用户列表
    def test_equipment_use(self):
        c = Client()
        response = c.get('/equipmentsetting/equipment_use/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备管理员绑定
    def test_equipment_use_POST(self):
        c = Client()
        response = c.post('/equipmentsetting/equipment_use/', {"imei": "111222333444555", "admin_user": "13370763530",
                                                               "relation": "test", "watchinfo": "111222333444555"})
        self.assertEqual(response.json()['error_no'], 0)

    # 查询所有设置信息
    def test_initInfos(self):
        c = Client()
        response = c.get('/equipmentsetting/initInfos/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 运动信息
    def test_sportInfo(self):
        print("onenet")

    # 查询工作模式是否开启
    def test_workmode_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/workmode/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改工作模式
    def test_workmode_PUT(self):
        # c = Client()
        # response = c.put("/equipmentsetting/workmode/", json.dumps({"imei": "362523432425471", "workmode": 0}),
        #                  content_type="application/json")
        # self.assertEqual(response.json()['error_no'], 0)
        print("onenet 发送命令")

    # WIFI状态报告
    def test_wifiStatusReport(self):
        c = Client()
        response = c.post('/equipmentsetting/wifiStatusReport/', {"imei": "362523432425471"})
        self.assertEqual(response.json()['error_no'], 4)

    # WIFI设置查询
    def test_wifiSetting_GET(self):
        c = Client()
        response = c.get('/equipmentsetting/wifiSetting/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # WIFI设置
    def test_wifiSetting_PUT(self):
        c = Client()
        response = c.put("/equipmentsetting/wifiSetting/", json.dumps({"imei": "362523432425471"}),
                         content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # WIFI设置同步
    def test_wifiSetting_POST(self):
        # c = Client()
        # response = c.post("/equipmentsetting/wifiSetting/", json.dumps({"imei": "362523432425471", "": 0}),
        #                  content_type="application/json")
        # self.assertEqual(response.json()['error_no'], 0)
        print("WIFI onenet")

    # 控制wifi
    def test_wifiSwitch(self):
        print("onenet发送命令")

    # WIFI列表状态
    def test_wifiListReport(self):
        c = Client()
        response = c.post("/equipmentsetting/wifiListReport/", json.dumps({"imei": "362523432425471"}),
                          content_type="application/json")
        self.assertEqual(response.json()['error_no'], 4)

    # SOS查询
    def test_sosnumbers(self):
        c = Client()
        response = c.get('/equipmentsetting/sosnumbers/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # SOS修改
    def test_sosnumbers_PUT(self):
        # c = Client()
        # response = c.put("/equipmentsetting/sosnumbers/", json.dumps({"imei": "362523432425471"}),
        #                  content_type="application/json")
        # self.assertEqual(response.json()['error_no'], 0)
        print('SOS 修改需要onenet')

    # 设备功能控制查询
    def test_funcControl(self):
        c = Client()
        response = c.get('/equipmentsetting/funcControl/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 设备功能控制修改
    def test_funcControl_PUT(self):
        print("需onenet命令")

    # 定时开关机查询
    def test_timingSwitch(self):
        c = Client()
        response = c.get('/equipmentsetting/timingSwitch/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 定时开关机修改
    def test_timingSwitch_PUT(self):
        print("需onenet命令")

    # 快速拨号
    def test_quickdialnumbers(self):
        c = Client()
        response = c.get('/equipmentsetting/quickdialnumbers/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 修改快速拨号
    def test_quickdialnumbers_PUT(self):
        print("需onenet命令")

    # 回传IMEI
    def test_allownetworkupload(self):
        c = Client()
        response = c.get('/equipmentsetting/allownetworkupload/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 音箱频道查询或创建
    def test_loudspeakerchannels(self):
        c = Client()
        response = c.get('/equipmentsetting/loudspeakerchannels/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 音箱频道修改或创建
    def test_loudspeakerchannels_POST(self):
        print("需onenet命令")


class locationTest(TestCase):
    def setUp(self):
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)
        AgentCompany.objects.create(companyname="cmx", password="123456")
        EquipmentOneNetInfo.objects.create(imei="362523432425471", sn="20180423192658",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")

    # 历史轨迹
    def test_history(self):
        c = Client()
        response = c.get('/location/history/?starttime=2018-07-30T00:25&endtime=2019-01-30T20:25&imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)

    # 最后一次
    def test_last(self):
        c = Client()
        response = c.get('/location/last/?imei=362523432425471')
        self.assertEqual(response.json()['error_no'], 0)


class messageTest(TestCase):
    def setUp(self):
        c = Client()
        response1 = c.post('/usermanager/register/', {
            "username": "13370763530", "password": "123", "phone": "13370763530", "nickname": "ddd", "openid": "5"
        })
        self.assertEqual(response1.json()['error_no'], 0)
        AgentCompany.objects.create(companyname="cmx", password="123456")
        EquipmentOneNetInfo.objects.create(imei="869273030005675", sn="869273030005675",
                                           api_key="G8kfqOmAFEFZ0vh=lDREidL4f8Y=", device_id="503793636",
                                           kind="loudspeaker", company_id="cmx")
        EquipmentUserInfo.objects.create(imei_id="869273030005675", nickname="dzt", gender="man")
        EquipmentUse.objects.create(imei_id="869273030005675", admin_user_id="13370763530", isactive=True,
                                    watchinfo_id="869273030005675", relation="")
        MessageInfo.objects.create(imei_id="869273030005675", kind=3, time="2019-02-27T15:25", id=2)
        AppMessageInfo.objects.create(username_id="13370763530", kind=3, time="2019-02-27T15:25")

    # 查询推送信息
    def test_pushinfo_GET(self):
        c = Client()
        response = c.get('/message/pushinfo/?imei=869273030005675')
        self.assertEqual(response.json()['error_no'], 0)

    # 推送消息
    def test_pushinfo_POST(self):
        c = Client()
        response = c.post("/message/pushinfo/", json.dumps({"imei": "869273030005675", "kind": 5,
                                                            "time": "2019-02-27T15:25", "content": "测试",
                                                            "summary": "11"}),
                          content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 删除消息
    def test_pushinfo_DELETE(self):
        c = Client()
        response = c.delete("/message/pushinfo/", json.dumps({"id": 2}), content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 推送APP系统消息
    def test_pushAppMessage_POST(self):
        c = Client()
        response = c.post("/message/pushAppMessage/", json.dumps({
            "username": "13370763530", "summary": "测试推送", "kind": 5, "content": ""}),
                          content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)

    # 查询APP系统消息
    def test_pushAppMessage_GET(self):
        c = Client()
        response = c.get("/message/pushAppMessage/?username=13370763530")
        self.assertEqual(response.json()['error_no'], 0)

    # 删除APP系统消息
    def test_pushAppMessage_DELETE(self):
        c = Client()
        response = c.delete("/message/pushAppMessage/", json.dumps({"id": 1}), content_type="application/json")
        self.assertEqual(response.json()['error_no'], 0)