assertlibrary.sas

Go to the documentation of this file.
00001  
00031 /* 
00032    28.05.2010 KL  Pfade mit Sonderzeichen machen unter SAS9.2 Probleme, deshalb Strings in Hochkommata
00033    06.02.2008 AM  Dokumentation verbessert
00034    05.11.2008 KL  Unterdrücken von Warnings (Keine ODS Destination offen).
00035    21.10.2008 KL  Logik bei MoreColumns etc. war leider falschrum.
00036    17.12.2007 KL  Parameter ExcludeList hinzugefügt
00037 */
00038 
00039 %MACRO assertLibrary (
00040     i_actual       = _NONE_
00041    ,i_expected     = _NONE_
00042    ,i_desc         = Bibliotheken prüfen
00043    ,i_libraryCheck = STRICT
00044    ,i_CompareCheck = STRICT
00045    ,i_fuzz         = _NONE_
00046    ,i_id           = _NONE_
00047    ,i_ExcludeList  = _NONE_
00048 );
00049 
00050    %GLOBAL g_inTestcase;
00051    %IF &g_inTestcase EQ 1 %THEN %DO;
00052       %endTestcall;
00053    %END;
00054    %ELSE %IF &g_inTestcase NE 2 %THEN %DO;
00055       %PUT &g_error: assert muss nach initTestcase aufgerufen werden;
00056       %RETURN;
00057    %END;
00058 
00059    %LOCAL l_casid l_tstid;
00060    %_sasunit_asserts(
00061        i_type     = assertLibrary
00062       ,i_expected = %eval (%sysfunc (libref (&i_expected.))=0)
00063       ,i_actual   = %eval (%sysfunc (libref (&i_actual.))=0)
00064       ,i_desc     = &i_desc
00065       ,i_result   = .
00066       ,r_casid    = l_casid
00067       ,r_tstid    = l_tstid
00068    )
00069 
00070    %local l_actual_ok l_expected_ok l_result l_path_actual l_path_expected _sysinfo l_col_names l_id_col l_counter l_id;
00071 
00072    %*************************************************************;
00073    %*** Check preconditions                                   ***;
00074    %*************************************************************;
00075 
00076    %*** check for valid actual libref ***;
00077    %let l_actual_ok=%sysfunc (libref (&i_actual.));
00078    %if (&l_actual_ok. > 0) %then %do;
00079        %put &g_error: Libref i_actual (&i_actual.) is invalid!;
00080        %let l_rc=1;
00081        %goto Update;
00082    %end;
00083 
00084    %*** check for valid expected libref ***;
00085    %let l_expected_ok=%sysfunc (libref (&i_expected.));
00086    %if (&l_expected_ok. > 0) %then %do;
00087        %put &g_error: Libref i_expected (&i_expected.) is invalid!;
00088        %let l_rc=1;
00089        %goto Update;
00090    %end;
00091 
00092    %*** check for equal librefs ***;
00093    %if (&i_actual. = &i_expected.) %then  %do;
00094        %put &g_error: Librefs are identical!;
00095        %let l_rc=1;
00096        %goto Update;
00097    %end;
00098 
00099    %*** check for identical paths ***;
00100    %let l_path_actual = %sysfunc (pathname (&i_actual.));
00101    %let l_path_expected = %sysfunc (pathname (&i_expected.));
00102    %if ("&l_path_actual."  = "&l_path_expected.") %then %do;
00103        %put &g_error: paths are identical!;
00104        %let l_rc=1;
00105        %goto Update;
00106    %end;
00107 
00108    %let i_LibraryCheck = %upcase (%trim(&i_LibraryCheck.));
00109    %if (&i_LibraryCheck. ne STRICT AND &i_LibraryCheck. ne MORETABLES) %then %do;
00110        %put &g_error: Value of i_LibraryCheck (%trim(&i_LibraryCheck.)) is invalid!;
00111        %let l_rc=1;
00112        %goto Update;
00113    %end;
00114 
00115    %let i_CompareCheck = %upcase (%trim(&i_CompareCheck.));
00116    %if (&i_CompareCheck. ne STRICT AND &i_CompareCheck. ne MORECOLUMNS 
00117         AND &i_CompareCheck. ne MOREOBS AND &i_CompareCheck. ne MORECOLSNOBS) %then %do;
00118        %put &g_error: Value of i_CompareCheck (%trim(&i_CompareCheck.)) is invalid!;
00119        %let l_rc=1;
00120        %goto Update;
00121    %end;
00122 
00123    %let i_ExcludeList = %upcase (%trim(&i_ExcludeList.));
00124 
00125    %local AnzCompares;
00126 
00127    %*************************************************************;
00128    %*** start tests                                           ***;
00129    %*************************************************************;
00130 
00131    %*** get table names from the two libraries ***;
00132    proc sql noprint;
00133       create table WORK._assertLibraryActual as
00134          select libname as BaseLibname,
00135                 memname as BaseMemName,
00136                 nlobs as BaseObs format=commax18.,
00137                 nvar as BaseNVar format=commax18.
00138          from dictionary.tables
00139          where libname = "%upcase(&i_actual)" 
00140             %if (&i_ExcludeList. ne _NONE_) %then %do;
00141                AND memname not in (
00142                %let counter=1;
00143                %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
00144                %do %while (&l_id_col. ne );
00145                   "&l_id_col."
00146                   %let counter=%eval (&counter.+1);
00147                   %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
00148                %end;
00149                )
00150             %end;
00151          order by memname;
00152       create table WORK._assertLibraryExpected as
00153          select libname as CmpLibname,
00154                 memname as CmpMemName,
00155                 nlobs as CmpObs format=commax18.,
00156                 nvar as CmpNVar format=commax18.
00157          from dictionary.tables
00158          where libname = "%upcase(&i_expected)"
00159             %if (&i_ExcludeList. ne _NONE_) %then %do;
00160                AND memname not in (
00161                %let counter=1;
00162                %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
00163                %do %while (&l_id_col. ne );
00164                   "&l_id_col."
00165                   %let counter=%eval (&counter.+1);
00166                   %let l_id_col = %scan (&i_ExcludeList.,&counter., %str ( ));
00167                %end;
00168                )
00169             %end;
00170          order by memname;
00171    quit;
00172 
00173    %*** flag tables according to check results ***;
00174    data work._ergebnis;
00175       length DoCompare CompareFailed l_rc 8;
00176       merge WORK._assertLibraryActual (in=InAct rename=(BaseMemname=memname))
00177             WORK._assertLibraryExpected (in=InExp rename=(CmpMemname=memname));
00178       by memname;
00179       l_rc = .;
00180       InActual=InAct;
00181       InExpected=InExp;
00182       if (InAct AND inExp) then do;
00183          DoCompare=1;
00184          CompareFailed=1;
00185          if (BaseObs ne CmpObs) then do;
00186             if ("&i_CompareCheck." = "STRICT" OR "&i_CompareCheck." = "MORECOLUMNS") then do;
00187                DoCompare=0;
00188             end;
00189          end;
00190          if (BaseNVar ne CmpNVar) then do;
00191             if ("&i_CompareCheck." = "STRICT" OR "&i_CompareCheck." = "MOREOBS") then do;
00192                DoCompare=0;
00193             end;
00194          end;
00195       end;
00196       else if (InAct AND not inExp AND "&i_LibraryCheck." ne "STRICT") then do;
00197          DoCompare=0;
00198          CompareFailed=0;
00199       end;
00200       else do;
00201          DoCompare=0;
00202          CompareFailed=1;
00203       end;
00204    run;
00205 
00206    %*** determine number of compared tables ***;
00207    proc sql noprint;
00208       select sum (DoCompare) into :AnzCompares
00209       from work._ergebnis;
00210    quit;
00211 
00212    %if (&AnzCompares. > 0) %then %do;
00213       %do i=1 %to &AnzCompares.;
00214          %local Memname&i.;
00215       %end;
00216 
00217       proc sql noprint;
00218          select memname into :Memname1-:Memname%trim(&AnzCompares.)
00219          from work._ergebnis 
00220          where DoCompare=1;
00221       quit;
00222 
00223       %*** upcase for id columsn ***;
00224       %if (&i_id. ne _NONE_) %then %do;
00225          %let i_id=%upcase (&i_id.);
00226       %end;
00227 
00228       %*** Compare each pair of tables ***;
00229       %do i=1 %to &AnzCompares.;
00230          %if (&i_id. ne _NONE_) %then %do;
00231             %let l_col_names=;
00232             %let l_id=;
00233             proc sql noprint;
00234                select distinct upcase (name) into :l_col_names separated by ' '
00235                from dictionary.columns
00236                where libname = "%upcase (&i_actual.)" AND upcase (memname) = "%upcase(&&memname&i.)";
00237             run;
00238 
00239             %let counter=1;
00240             %let l_id_col = %scan (&i_id.,&counter., %str ( ));
00241             %do %while (&l_id_col. ne );
00242                 %let l_found=%sysfunc (indexw (&l_col_names, &l_id_col.));
00243                 %if (&l_found. > 0) %then %do;
00244                    %let l_id = &l_id. &l_id_col;
00245                 %end;
00246                %let counter=%eval (&counter.+1);
00247                %let l_id_col = %scan (&i_id.,&counter., %str ( ));
00248             %end;
00249          %end;
00250 
00251          proc compare 
00252             base=&i_expected..&&memname&i. 
00253             compare=&i_actual..&&memname&i.
00254             %if (&i_fuzz. ne _NONE_) %then %do;
00255                CRITERION=&i_fuzz.
00256             %end;
00257             noprint;
00258             %if (&i_id. ne _NONE_) %then %do;
00259                id &l_id.;
00260             %end;
00261          run;
00262          %let _sysinfo=&sysinfo.;
00263          proc sql noprint;
00264             update work._ergebnis
00265             set l_rc=&_sysinfo.
00266             where upcase (memname)="%upcase (&&memname&i.)";
00267          quit;
00268       %end;
00269    %end;
00270    
00271    %*** set test result ***;
00272    data work._ergebnis;
00273       set work._ergebnis;
00274       select (l_rc);
00275          when (0)
00276             Comparefailed=0;
00277          when (128) do; /* COMPOBS - Comparison data set has observation not in base */
00278             if ("&i_CompareCheck." = "MOREOBS" OR "&i_CompareCheck." = "MORECOLSNOBS") then do;
00279                Comparefailed=0;
00280             end;
00281          end;
00282          when (2048) do; /* COMPVAR - Comparison data set has variable not in base */
00283             if ("&i_CompareCheck." = "MORECOLUMNS" OR "&i_CompareCheck." = "MORECOLSNOBS") then do;
00284                Comparefailed=0;
00285             end;
00286          end;
00287          when (2176) do; /* COMPOBS & COMPVAR */
00288             if ("&i_CompareCheck." = "MORECOLSNOBS") then do;
00289                Comparefailed=0;
00290             end;
00291          end;
00292          otherwise;
00293       end;
00294    run;
00295 
00296    %*** format resuts for report ***;
00297    data work._ergebnis;
00298       length icon_column $45 i_LibraryCheck i_CompareCheck $15 i_id i_ExcludeList $80;
00299       set work._ergebnis;
00300       SELECT (CompareFailed);
00301          WHEN (0) icon_column='<img src="ok.png" alt="OK"></img>';
00302          WHEN (1) icon_column='<img src="error.png" alt="Fehler"></img>';
00303          OTHERWISE icon_column='&nbsp;';
00304       END;
00305       i_LibraryCheck="&i_LibraryCheck.";
00306       i_CompareCheck="&i_CompareCheck.";
00307       i_id="&i_id.";
00308       i_ExcludeList="&i_ExcludeList.";
00309    run;
00310 
00311    %*** determine return value for test ***;
00312    proc sql noprint;
00313       select max (CompareFailed) into :l_rc
00314       from work._ergebnis;
00315    quit;
00316 
00317    %*** create library listing ***;
00318    ods document name=testout._%substr(00&g_scnid,%length(&g_scnid))_&l_casid._&l_tstid._Library_act(WRITE);
00319       TITLE "Contents of the actual library";
00320       proc print data=WORK._assertLibraryActual label noobs;
00321       run;
00322    ods document close;
00323    ods document name=testout._%substr(00&g_scnid,%length(&g_scnid))_&l_casid._&l_tstid._Library_exp(WRITE);
00324       TITLE "Contents of the expected library";
00325       proc print data=WORK._assertLibraryExpected label noobs;
00326       run;
00327    ods document close;
00328    data testout._%substr(00&g_scnid,%length(&g_scnid))_&l_casid._&l_tstid._Library_rep;
00329       set work._ergebnis;
00330    run;
00331 
00332    proc datasets lib=work nolist memtype=(data view);
00333       delete _ergebnis _assertLibraryActual _assertLibraryExpected;
00334    quit;
00335 
00336 %Update:;
00337    /* update result in test database */
00338    PROC SQL NOPRINT;
00339       UPDATE target.tst 
00340          SET tst_res = &l_rc 
00341          WHERE 
00342             tst_scnid = &g_scnid AND
00343             tst_casid = &l_casid AND
00344             tst_id    = &l_tstid
00345          ;
00346    QUIT;
00347 %MEND;

Generated on Sun Oct 10 14:10:15 2010 for SASUnit Examples by  doxygen 1.5.3