测试数据心得——holo

        目前是做报表项目,经常会有新的报表通过新的视图呈现在前端,所以经常需要在新表里面造数据,有一个区划维度大概有100+,另外还有一些其他维度,此为背景,使用数据库为holo

1、整理区划维度 

区划维度是以拉链形式存于表中,这一块就需要根据根节点将整个数据平铺起来

select   
t1.code 大区code,t1.name 大区名称,
t2.code 城市code,t2.name 城市名称,
t3.code 团队code,t3.name 团队名称,
t4.code 门店code,t4.name 门店名称

from (select  id,name,parent_id,code  from  data_permission where  parent_id in 
        -- 从这个子查询中获取根节点
       (select id from data_permission where data_type = 'BI.Data.Delivery.Area')     
     ) t1 
left join data_permission t2 on  t1.id   =  t2.parent_id
left join data_permission t3 on  t2.id   =  t3.parent_id  
left join data_permission t4 on  t3.id   =  t4.parent_id  
order by t1.code,t2.code,t3.code,t4.code;

然后通过正则将数据处理规整

2、将测试数据存入维度表中,此表专为测试数据服务 

-- 交付区划辅助表
drop table if exists ads.交付区划辅助表;
create table ads.交付区划辅助表
(
    大区编码 text,大区名称 text,城市编码 text,城市名称 text,门店编码 text,门店名称 text
);


-- 将查到到平铺数据插入到辅助表里面
INSERT INTO  ads.交付区划辅助表 (大区编码,大区名称,城市编码,城市名称,门店编码,门店名称) 
VALUES
 ('#999','其他','#999','其他','#999','其他'),
。。。
。。。

3、从维度表里面造多个时间节点的数据

-- 插入日期和区划维度
insert into 业务表(biz_datetime,big_area_code,city_code,store_code)
select generate_series(current_date - 100,current_date + 130, '4 hours'),大区编码,城市编码,门店编码
from ads.交付区划辅助表 ;

以上的数据是基于当前日期 ,历史时间造100天以前以及130天以后

1天一份的话,4 hours可以改成1 days

如果是数据少的话,可以 使用迪卡尔积形式,但是我这边数据维度如果使用迪卡尔积的话,一天的数据都可以达到十数万,所以取某一个维度较多的数据作为基础维度,然后其他维度使用随机生成,这种数据基本满足所有维度都可以命中

4、不规则维度测试数据处理

造数据时,有些是是规则维度 ,有些是不规则维度,可以 使用case when 进行修复,

例: 枚举: aa,bb,cc

update 业务表
set 
enum_code = ceil(random()*4) where true;

update 业务表
set 
enum_code = 
    case enum_code 
        when '1' then 'aa' 
        when '2' then 'bb' 
        when '3' then 'cc' 
        else enum_code end 
where true;

你可能感兴趣的:(postgresql)