SASUnit Examples  Version 1.5.0
asserttableexists.sas
Go to the documentation of this file.
1 
33 %MACRO assertTableExists (i_libref =
34  ,i_memname =
35  ,i_target = DATA
36  ,i_desc = Check for existence of a specific dataset
37  ,i_not = 0
38  );
39 
40  /*-- verify correct sequence of calls-----------------------------------------*/
41  %GLOBAL g_inTestcase;
42  %IF &g_inTestcase EQ 1 %THEN %DO;
43  %endTestcall;
44  %END;
45  %ELSE %IF &g_inTestcase NE 2 %THEN %DO;
46  %PUT &g_error.(SASUNIT): assert must be called after initTestcase;
47  %RETURN;
48  %END;
49 
50  %LOCAL l_dsname l_libref_ok l_table_exist l_result l_date l_suffix l_errMsg;
51  %LET l_dsname =%sysfunc(catx(., &i_libref, &i_memname));
52  %LET l_table_exist = -1;
53  %LET l_result=2;
54  %LET l_date =;
55 
56  %*************************************************************;
57  %*** Check preconditions ***;
58  %*************************************************************;
59 
60  %*** check for valid libref ***;
61  %LET l_libref_ok=%sysfunc (libref (&i_libref.));
62  %IF &l_libref_ok. NE 0 %THEN %DO;
63  %LET l_errMsg=Libref &i_libref. is invalid!;
64  %goto Update;
65  %END;
66 
67  %*** check if i_target is valid ***;
68  %LET i_target=%sysfunc(upcase(&i_target));
69  %IF not(&i_target=DATA or &i_target=VIEW or &i_target=CATALOG) %THEN %DO;
70  %LET l_table_exist = -2;
71  %LET l_errMsg=%bquote(Invalid value for parameter i_target (&i_target.)!);
72  %goto Update;
73  %END;
74 
75  %*************************************************************;
76  %*** start tests ***;
77  %*************************************************************;
78 
79  %IF %sysfunc(exist(&l_dsname, &i_target)) %THEN %DO;
80  %LET l_table_exist=1;
81  %PUT &g_note.(SASUNIT): &i_target. &l_dsname. exists.;
82  %LET l_errMsg=&i_target &l_dsname exists;
83 
84  %*** get creation und modification date of tested member ***;
85  data _null_ ;
86  length _crdate _modate $20;
87  dsid=open("&l_dsname") ;
88  _crdate = attrn(dsid,'CRDTE');
89  _modate = attrn(dsid,'MODTE');
90  dsid=close(dsid) ;
91  call symput('l_date',catt("#",_crdate,"#",_modate));
92  run ;
93  %END;
94  %ELSE %DO;
95  %PUT &g_note.(SASUNIT): &i_target. &l_dsname. does not exist.;
96  %LET l_errMsg=&i_target &l_dsname does not exist;
97  %LET l_table_exist=0;
98  %END;
99 
100  %LET l_result = %eval(1 - &l_table_exist.);
101  %LET l_suffix=%str(, but it should exist!);
102  %IF (&i_not) %THEN %DO;
103  %LET l_result = %eval(1 - &l_result.);
104  %LET l_suffix=%str(, but it should not exist!);
105  %END;
106  %LET l_errMsg =&l_errMsg.&l_suffix.;
107  %LET l_result = %eval(&l_result.*2);
108 
109  %Update:;
110  %_asserts(i_type = assertTableExists
111  ,i_expected = %str(&i_target.:&l_dsname.:&i_not.)
112  ,i_actual = %str(&l_table_exist.&l_date.)
113  ,i_desc = &i_desc.
114  ,i_result = &l_result.
115  ,i_errMsg = &l_errMsg.
116  )
117 
118 %MEND assertTableExists;