應用場景:
本周在進行SIT,我幫助僅有的一個測試妹妹對部分表進行數據質量驗證,第一步需要做的就是比對source與stage表的table definition 與 數據內容的一致性。
本項目使用的是oracle作為DW,source是oracle,sqlserver和xls.
沒有權限建立database link, 測試們常用的方法是比對總行數,然后如果數據集太大的話,則抽樣比對,導出數據到xls,然后使用beyondcompare進行比對。
如果值出現不同,則需要查找出哪些行的值不同,最少要找出一條具體的值。
我測試的時候發現,手動去做這一些事也是挺費力的。就寫了以下的小工具進行輔助測試。
功能:
1.支持從oracle,sqlserver,xlsx中取數據進行比對。
2.對oracle與sqlserver,可采用傳統的比對,各自獲取一個readonly ,forward 胡datareader,逐行比對。點擊CompareData按鈕。因為數據庫不同找不到統一的方法計算值,oracle中的ora_hash和sqlserver中的hashbyte不一致。
3.對于oracle與oracle的或者是sqlserver對sqlserver的query比對,可以使用GetDiffRange按鈕,即二分查找法進行比對。因為相同的數據庫可以找到一個方法進行數據比對。
測試數據如下:
在oracle的數據庫中執行以下代碼,生成測試數據。
--create table mytest(id int, rid int,name varchar2(20));--create table mytest2(id int, rid int,name varchar2(20));declare i integer :=1;beginwhile i<110000 loopinsert into mytest(ID,RID,NAME) VALUES(mysequence.nextval,i,'dataitem'||i);i:=i+1;end loop;end ; insert into mytest2 SELECT * FROM mytest order by id; update mytest2 set rid=100 where id=100000; commit;select * from mytest minus select * from mytest2; select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest t)select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest2 t) update mytest2 set name='EvanTEst' where id=1000;commit;
Range:可以自己設定,如果是1的話,則直接定位到第一條不同的記錄,如果是大于1 則是一個區間值。
表示不同的數據行就是在他們之間。
為了同時支持oracle和sqlserver,我使用庫中自帶的接口IDbConnection和IDataReader進行開發,可以同時支持oracle和sqlserver and Xlsx.
sql要求:
二分查找需要樣本有序,所以需要寫成
select count(*) as totalcount, to_char(avg(ora_hash(id||rid ||name))) as avghash from (select id as rn, t.* from mytest t)
的樣式,第一個是總行數,第二個要求是里面需要有一個固定列名叫rn。
我使用ora_hash函數對每行的值進行hash,然后求平均值的方式來計算表區間內行的內容是否相同,這是oracle提供的函數。
類似的有函數checksum,但是計算速度要比這個慢許多,尤其是表數據量大的時候。
sqlserver中有hashbyte函數類似。 注意如果不使用to_char函數,則算出的值會溢出,如果放到.net中來承接,會報oci數據溢出的錯誤,所以to_char也是必須的。
歡迎大家分享BI測試的一些經驗心得。
軟件下載地址: files.cnblogs.com/huaxiaoyao/datacompare.rar
主要源碼:
PRivate void compareData2(int start, int end,int total) { rtb_log.AppendText(string.Format("comparedata2({0},{1},{2})", start, end, total) + Environment.NewLine); string tmpsql = " where rn between {0} and {1} "; if (total == 0) { dr1 = getDataReader(conn, rtx_sql.Text); dr2 = getDataReader(conn2, rtx_sql2.Text); } else { dr1 = getDataReader(conn, rtx_sql.Text + string.Format(tmpsql, start, end)); dr2 = getDataReader(conn2, rtx_sql2.Text + string.Format(tmpsql, start, end)); } //assume the dr schema totalcount,avghash bool isdiff = false; while (dr1.Read() && dr2.Read()) { if (total == 0) {end= total = dr1.GetInt32(0); } for (int col = 1; col < dr1.FieldCount; col++) { if (!dr1.GetValue(col).ToString().Equals(dr2.GetValue(col).ToString()) || !(dr1.IsDBNull(col) == dr2.IsDBNull(col)) ) { isdiff = true; } } } dr1.Close(); dr2.Close(); //if isdiff=true the split the totalcount two part if (isdiff) { if ((end - start < range)) return; compareData2(start, (start + end) / 2, end); } else //if same { if (start == end) end = total; compareData2(end, total, total); } }
新聞熱點
疑難解答