test_mysqlutils.py 4.32 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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@Version: 1.0
@Python Version:3.6.6
@Author: ludq1
@File: test_mysqlutils
@Time: 2019/1/7 007 22:01
@Description: 测试代码
"""
from unittest import TestCase, main

from cucc_common_pkg.mysql_utils import MysqlUtils


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

    def test_gen_insertsql(self):
        r'''

        Returns:

        '''
        a_dict = {'c1': 'c1', 'c2': '2'}
        table_name = 'table_name'
        expect_result = 'insert `' + table_name + "`(`c1`,`c2`) select 'c1','2'"
        a_result = MysqlUtils.gen_insertsql(table_name, a_dict)
        self.assertEqual(expect_result, a_result)

        a_list = [a_dict]
        table_name = 'table_name'
        expect_result = ['insert `' + table_name + "`(`c1`,`c2`) select 'c1','2'"]
        a_result = MysqlUtils.gen_insertsql_multi(table_name, a_list)
        self.assertEqual(expect_result, a_result)

    def test_gen_part_sql(self):
        r'''

        Returns:

        '''
        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        expect_result = "`c1`='c1',`c2`='2'"
        a_result = MysqlUtils.gen_part_sql_for_update(a_dict)
        self.assertEqual(expect_result, a_result)

        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        expect_result = "`c1`='c1',`c2`='2',`c3`=null"
        a_result = MysqlUtils.gen_part_sql_for_update(a_dict, ignore_none=False)
        self.assertEqual(expect_result, a_result)

        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        expect_result = "`c1`='c1' and `c2`='2' and `c3` is null"
        a_result = MysqlUtils.gen_part_sql_for_where(a_dict, where_dict_can_be_none_or_empty=False)
        self.assertEqual(expect_result, a_result)

        a_dict = None
        expect_result = "1=1"
        a_result = MysqlUtils.gen_part_sql_for_where(a_dict, where_dict_can_be_none_or_empty=True)
        self.assertEqual(expect_result, a_result)

        a_dict = {}
        expect_result = "1=1"
        a_result = MysqlUtils.gen_part_sql_for_where(a_dict, where_dict_can_be_none_or_empty=True)
        self.assertEqual(expect_result, a_result)

        a_dict = {}
        with self.assertRaises(Exception):
            MysqlUtils.gen_part_sql_for_where(a_dict)

        a_dict = {}
        with self.assertRaises(Exception):
            MysqlUtils.gen_part_sql_for_where(a_dict)

    def test_gen_updatesql(self):
        r'''

        Returns:

        '''
        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        where_dict = {'c1': '1'}
        table_name = 'table_name'
        expect_result = "update `" + table_name + "` set `c1`='c1',`c2`='2' where `c1`='1'"
        a_result = MysqlUtils.gen_updatesql(table_name, a_dict, where_dict)
        self.assertEqual(expect_result, a_result)

        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        where_dict = {'c1': '1'}
        table_name = 'table_name'
        expect_result = "update `" + table_name + "` set `c1`='c1',`c2`='2',`c3`=null where `c1`='1'"
        a_result = MysqlUtils.gen_updatesql(table_name, a_dict, where_dict, ignore_none=False)
        self.assertEqual(expect_result, a_result)

        a_dict = {'c1': 'c1', 'c2': '2', 'c3': None}
        where_dict = {}
        table_name = 'table_name'
        expect_result = "update `" + table_name + "` set `c1`='c1',`c2`='2',`c3`=null where 1=1"
        a_result = MysqlUtils.gen_updatesql(table_name, a_dict, where_dict, ignore_none=False,
                                            where_dict_can_be_none_or_empty=True)
        self.assertEqual(expect_result, a_result)

    def test_gen_delsql(self):
        r'''

        Returns:

        '''
        where_dict = {'c1': '1'}
        table_name = 'table_name'
        expect_result = "delete from `" + table_name + "` where `c1`='1'"
        a_result = MysqlUtils.gen_delsql(table_name, where_dict)
        self.assertEqual(expect_result, a_result)

        where_dict = {}
        table_name = 'table_name'
        expect_result = "delete from `" + table_name + "` where 1=1"
        a_result = MysqlUtils.gen_delsql(table_name, where_dict,
                                         where_dict_can_be_none_or_empty=True)
        self.assertEqual(expect_result, a_result)


if __name__ == '__main__':
    main()