#!/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()