zoukankan      html  css  js  c++  java
  • 接口自动化框架之DB构造数据

    整体流程:

    下面主要是阐述这一块

    1.接口自动化框架关键代码-数据构造:

    可desc 获取table最新的结构,转换成xml格式。做数据构造时遇到数据库变化,例如有必填的字段添加,框架都会读xml里面的默认值去构造。减少数据库

    表变化对接口自动化影响

    (1)更新数据库表结构,保存为List<Map<String, String>>后转成xml

    public List<Map<String, String>> getTableDesc(String tableName) {
            String sql = "desc " + tableName;  //desc table 获取最新的表结构
            List<Map<String, String>> res = new ArrayList<Map<String, String>>();
            try {
                PreparedStatement stmt = null;
                stmt = cnn.prepareStatement(sql);
                ResultSet result = stmt.executeQuery(sql);
                while (result.next()) {
                    Map<String, String> map = new HashMap<String, String>();
                    res.add(map);
                    String filedValue = result.getString("Field");
                    map.put("Field", filedValue);
                    String typeValue = result.getString("Type");
                    map.put("Type", typeValue);
                    String nullValue = result.getString("Null");
                    map.put("Null", nullValue);
                    String defaultValue = result.getString("Default");
                    map.put("Default", defaultValue);
                    String keyValue = result.getString("Key");
                    map.put("Key", keyValue);
                    String extraValue = result.getString("Extra");
                    map.put("Extra", extraValue);
                }
     <table tablename="T_APP_ACTIVITY" shardColumn="" modebase="0"/>
        <column defaultValue="" autoIncrement="true" columName="FID" type="bigint(20)"/>
        <column defaultValue="apps" autoIncrement="false" columName="FPRODUCT" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FTYPE" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FSUBJECT" type="varchar(128)"/>
        <column defaultValue="" autoIncrement="false" columName="FDESCRIPTION" type="varchar(1024)"/>
        <column defaultValue="http:www.meizu.com" autoIncrement="false" columName="FHTML_URL" type="varchar(255)"/>
        <column defaultValue="" autoIncrement="false" columName="FPUBLICITY_IMG" type="varchar(255)"/>
        <column defaultValue="" autoIncrement="false" columName="FSTART_TIME" type="datetime"/>
        <column defaultValue="" autoIncrement="false" columName="FEND_TIME" type="datetime"/>
        <column defaultValue="" autoIncrement="false" columName="FPUSH_TIME" type="datetime"/>
        <column defaultValue="" autoIncrement="false" columName="FBACK_COLOR" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FTITLE_COLOR" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FBTN_COLOR" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FBTN_SELECTED_COLOR" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FSB_COLOR" type="varchar(16)"/>
        <column defaultValue="" autoIncrement="false" columName="FSTATUSICON_COLOR" type="varchar(16)"/>

    2.插入数据的关键代码

            //测试API构造测试数据到DB库 插入100条数据
            TableInsertor.begin().table(tableName).value("FPRODUCT", "测试产品")
            .value("FSUBJECT", "应用商店人工测试数据").value("FBTN_COLOR", "BlueX").value("FDESCRIPTION", "${1~22}").insert(100);

    逻辑为:如果人为插入的表字段(上面的)包含xml里面的表字段,则用人为的key-value去插入。。不包含表示 数据表字段名字改了或者人为设置的表字段写错了

    如果不包含,将用xml面的defaultvalue做插入。 我们只需要关注我们插入的key,而不需要关注数据表的结构更新,降低整体维护成本

    @Override
        public List<String> transfer(HashMap<String, Object> values,
                TreeMap<String, TreeMap<String, Object>> configs ,int N) {
            List<String> sqls = new ArrayList<String>();
            //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
            String sql_column = "(";
            TreeMap<String,ArrayList<Object>> tmpValues = new TreeMap<String,ArrayList<Object>>();    
            
            int size = configs.size();
            int i = 0;
            
            if(!ObjectUtils.isEmpty(values) 
                    && !ObjectUtils.isEmpty(configs)){
                //注意:没有设置VALUE的列部分用配置文件中的默认值来设置            
                for(Entry<String,TreeMap<String, Object>> configItem : configs.entrySet()){
                    i++;
                    String configColumnName = configItem.getKey().substring(configItem.getKey().indexOf(Constants.TILDE_STR)+1);
                    //若是人为设置的值
                    if(values.containsKey(configColumnName)){ 
                        //拼接COLUMNS部分
                        if(i < size){
                            sql_column += configColumnName + Constants.COMMA;
                        }else if(i == size){
                            sql_column += configColumnName + ")";
                        }
                        //拼接VALUES部分
                        String actV = values.get(configColumnName).toString();
                        /**
                         * 增加特殊规则,为构造数据的多样性,特规定支持数据有如下构造方式:
                         * 1: ${1,2,3,4} 如有四个int型数据集
                         * 2: ${1~25} 如从1到25的,自增1的int型数据集
                         */
                        List<Object> datas = vMaker.getValue(actV, N);
                        tmpValues.put(configColumnName, (ArrayList<Object>) datas);
                    }
                    //若不是人为设置,则使用配置模板中的默认值
                    else{ 
                        //拼接COLUMNS部分
                        if(i < size){
                            sql_column += configColumnName + Constants.COMMA;
                        }else{
                            sql_column += configColumnName + ")";
                        }
                        //拼接VALUES部分
                        String actDefaultValue = configItem.getValue().get(Constants.ATTR_NAME_DEFAULT_VALUE).toString();
                        //新增部分:目的是处理特殊类型的数据 added by houjunguang@20160516
                        String actType = configItem.getValue().get(Constants.ATTR_NAME_TYPE).toString();

    后面的:http请求和解析验证就不说了,就是封装get post请求还有不同content type请求,带seesion等请求

    解析验证就是json解析等,这些都是大家经常用的东西

  • 相关阅读:
    [java,2019-01-28] 枪手博弈,谁才是最后赢家
    [java,2019-01-25] 图片和二进制互转
    [java,2019-01-15] word转pdf
    [python,2018-06-29] 37%法则及其拓展解决恋爱问题
    [java,2018-06-26] 扑克牌抽牌求和问题
    [python,2018-06-25] 高德纳箭号表示法
    [java,2017-06-12] myEclipse双击无法打开文件
    OpenGL核心技术之法线贴图
    游戏中水的渲染技术系列一
    Unity 3D实现帧同步技术
  • 原文地址:https://www.cnblogs.com/season-xie/p/6337891.html
Copyright © 2011-2022 走看看