test_my_servicehandle.py 9.3 KB
Newer Older
qunfeng qiu's avatar
qunfeng qiu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@Version: 1.0
@Python Version:3.6.6
@Author: ludq1
@File: test_my_servicehandle
@Time: 2019/2/1 10:44
@Description:
"""
import os
from unittest import TestCase, main

from flask import Response

from cucc_common_pkg.globalutility import Utility
from cucc_common_pkg.my_servicehandle import ServiceHandle
from cucc_common_pkg.globalconst import GlobalConst, UserInfoKeyConst, EnvNameConst
from cucc_common_pkg.my_baseexception import create_base_exception


class TestUtility(TestCase):
    r'''
    TestUtility
    '''

    def test_get_mapwebarg(self):
        r'''

        Returns:

        '''

        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            return Utility.dict2jsonstr(ServiceHandle.get_mapwebarg())

        with app.test_client() as client:
            expect_result = dict(user='admin', pwd='123')
            rv = client.post('/', data=expect_result)
            self.assertEqual(expect_result, Utility.jsonstr2dict(rv.data))

    def test_check_is_success(self):
        r'''

        Returns:

        '''

        a_dict = None
        self.assertFalse(ServiceHandle.check_is_success(a_dict))

        a_dict = {}
        self.assertFalse(ServiceHandle.check_is_success(a_dict))

        a_dict = {
            GlobalConst.RETKEY_RET_CODE: "0"
        }
        self.assertFalse(ServiceHandle.check_is_success(a_dict))

        a_dict = {
            GlobalConst.RETKEY_RET_CODE: "1"
        }
        self.assertTrue(ServiceHandle.check_is_success(a_dict))

    def test_querydb(self):
        r'''

        Returns:

        '''
        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            sql_str = 'select infra_id from infra_manager_config where id=1'
            service_url = 'http://10.0.209.148:58006/mysqlpool/standardsql/assetmanage/softinst/r'
            result = ServiceHandle.querydb(service_url, sql_str)
            return ServiceHandle.do_make_response(result)

        with app.test_client() as client:
            expect_result = {GlobalConst.RETKEY_RET_CODE: "1", GlobalConst.RETKEY_RET_VAL: "1",
                             GlobalConst.RETKEY_ROWCOUNT: "1",
                             GlobalConst.RETKEY_DATAROWS: [{"infra_id": "__all__"}]}
            rv: Response = client.post('/')
            self.assertEqual(expect_result, Utility.jsonstr2dict(rv.data))

    def test_querycount_db(self):
        r'''

        Returns:

        '''
        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            sql_str = 'select count(*) from infra_manager_config where id=1'
            service_url = 'http://10.0.209.148:58006/mysqlpool/standardsql/assetmanage/softinst/r'
            result = ServiceHandle.querycount_db(service_url, sql_str)
            return ServiceHandle.do_make_response(result)

        with app.test_client() as client:
            expect_result = '1'
            rv: Response = client.post('/')
            self.assertEqual(expect_result, rv.data.decode())

    def test_operate_db(self):
        r'''

        Returns:

        '''
        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            sql_str = 'update infra_manager_config set id=1 where id=1'
            service_url = 'http://10.0.209.148:58006/mysqlpool/trans/assetmanage/softinst/w'
            result = ServiceHandle.operate_db(service_url, operate_sql_list=[sql_str])
            return ServiceHandle.do_make_response(result)

        with app.test_client() as client:
            expect_result = {"RetVal": "1", "RetCode": "1", "DataRows": [
                {"transrowsnum": "1", "transsql": "update infra_manager_config set id=1 where id=1"}]}
            rv: Response = client.post('/')
            self.assertEqual(expect_result, Utility.jsonstr2dict(rv.data.decode()))

    def test_call_proc(self):
        r'''

        Returns:

        '''
        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            arg_dict = {}
            proc_name = 'assigndepttask_query_proc'
            service_url = 'http://10.0.209.148:58006/mysqlpool/standardproc/assetmanage/softinst/r'
            result = ServiceHandle.call_proc(service_url, proc_name, arg_dict)
            return ServiceHandle.do_make_response(result)

        with app.test_client() as client:
            expect_result = "1"
            rv: Response = client.post('/')
            self.assertEqual(expect_result, Utility.jsonstr2dict(rv.data.decode()).get(GlobalConst.RETKEY_RET_CODE))

    def test_safe_get_first_dict_from_list(self):
        r'''

        Returns:

        '''
        expect_result = {"a": 11}
        a_param = [expect_result]
        result = ServiceHandle.safe_get_first_dict_from_list(a_param)
        self.assertEqual(result, expect_result)

    def test_safe_get_list_from_dict(self):
        r'''

        Returns:

        '''
        expect_result = [{"a": 11}]
        a_param = {GlobalConst.RETKEY_DATAROWS: expect_result}
        result = ServiceHandle.safe_get_list_from_dict(a_param, key_name=GlobalConst.RETKEY_DATAROWS)
        self.assertEqual(result, expect_result)

        expect_result = [{"a": 11}]
        a_param = {GlobalConst.RETKEY_DATAROWS: expect_result}
        result = ServiceHandle.safe_get_datarows(a_param)
        self.assertEqual(result, expect_result)

    def test_do_make_response(self):
        r'''

        Returns:

        '''
        from flask import Flask
        app = Flask(__name__)

        @app.route('/', methods=['GET', 'POST'])
        def hello_world():
            ret_dict = {GlobalConst.RETKEY_RET_CODE: "1", GlobalConst.RETKEY_RET_VAL: "success"}
            return ServiceHandle.do_make_response(ret_dict)

        with app.test_client() as client:
            expect_result = {GlobalConst.RETKEY_RET_CODE: "1", GlobalConst.RETKEY_RET_VAL: "success"}
            rv: Response = client.post('/')
            self.assertEqual(rv.status_code, 200)
            self.assertEqual(expect_result, Utility.jsonstr2dict(rv.data.decode()))

    def test_get_othermsg_from_exception(self):
        r'''

        Returns:

        '''
        expect_result = 'othermsg'
        a_param = create_base_exception('test', submitted_webarg=expect_result)
        result = ServiceHandle.get_othermsg_from_exception(a_param)
        self.assertEqual(expect_result, result)

    def test_get_userinfo(self):
        r'''

        Returns:

        '''

        usersystem = 'mysys'
        userid = 'uuu'
        username = 'username'
        g_userinfo = {UserInfoKeyConst.USER_ID: userid, UserInfoKeyConst.USER_SYSTEM: usersystem,
                      UserInfoKeyConst.USER_NAME: username}

        class TmpClass: pass

        global_obj = TmpClass()
        setattr(global_obj, UserInfoKeyConst.G_USERINFO, g_userinfo)
        result = ServiceHandle.get_userinfo(global_obj=global_obj)
        self.assertEqual(g_userinfo, result)

        result = ServiceHandle.get_usersystem(global_obj=global_obj)
        self.assertEqual(usersystem, result)

        result = ServiceHandle.get_userid(global_obj=global_obj)
        self.assertEqual(userid, result)

        result = ServiceHandle.is_login(global_obj=global_obj, expect_usersystem=usersystem)
        self.assertTrue(result)

        result = ServiceHandle.get_username(global_obj=global_obj)
        self.assertEqual(username, result)

        a_dict = {'arg_myuserid': '123'}
        ServiceHandle.put_userinfo_to_dict(arg_dict=a_dict, global_obj=global_obj)
        self.assertEqual(a_dict.get('arg_myuserid'), userid)
        self.assertEqual(a_dict.get('arg_myusername'), username)

        a_dict = {'arg_myuserid': '123'}
        os.environ[EnvNameConst.ENV_DONT_OVERRITE_USERINFO_FROM_FRANT] = '1'
        ServiceHandle.put_userinfo_to_dict(arg_dict=a_dict, global_obj=global_obj)
        self.assertEqual(a_dict.get('arg_myuserid'), '123')
        self.assertEqual(a_dict.get('arg_myusername'), username)

    def test_get_info_with_fromenv_first(self):
        r'''

        Returns:

        '''
        info_obj = 'http://www.baidu.com'
        info_in_env = 'http://a.com'
        info_key = 'MYSQLPOOL_SERVICE'
        os.environ[EnvNameConst.ENV_LOCAL_RUN] = '1'
        os.environ[info_key] = ''
        result = ServiceHandle.get_info_with_fromenv_first(info_obj=info_obj, info_key=info_key)
        self.assertEqual(info_obj, result)

        os.environ[info_key] = info_in_env
        result = ServiceHandle.get_info_with_fromenv_first(info_obj=info_obj, info_key=info_key)
        self.assertEqual(info_in_env, result)

        os.environ[EnvNameConst.ENV_LOCAL_RUN] = '0'
        os.environ[info_key] = info_in_env
        result = ServiceHandle.get_info_with_fromenv_first(info_obj=info_obj, info_key=info_key)
        self.assertEqual(info_obj, result)


if __name__ == '__main__':
    main()