SASUnit Examples  Version 1.5.0
assertrecordcount.sas
Go to the documentation of this file.
1 
32 %MACRO assertRecordCount(i_libref =
33  ,i_memname =
34  ,i_operator = EQ
35  ,i_recordsExp =
36  ,i_where = 1
37  ,i_desc = Check for a specific number of records
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_result l_actual;
51  %LET l_dsname = %sysfunc(catx(., &i_libref., &i_memname.));
52  %LET l_result = 2;
53  %LET l_actual = -999;
54  %LET i_operator = %sysfunc(upcase(&i_operator.));
55  %LET l_errmsg =;
56 
57  %IF &i_where = %THEN %LET i_where=1;
58 
59  %*************************************************************;
60  %*** Check preconditions ***;
61  %*************************************************************;
62 
63  %*** check for valid libref und existence of data set***;
64  %IF ((%sysfunc (libref (&i_libref.)) NE 0) or (%sysfunc(exist(&l_dsname)) EQ 0)) %THEN %DO;
65  %LET l_actual =-1;
66  %LET l_errmsg =Libref is invalid or table does not exist;
67  %goto Update;
68  %END;
69 
70  %*** check for valid parameter i_recordsExp ***;
71  DATA _NULL_;
72  IF (INPUT(&i_recordsExp., 32.) =.) then call symput('l_actual',"-3");
73  ELSE IF (&i_recordsExp. < 0) then call symput('l_actual',"-4");;
74  RUN;
75 
76  %IF (&l_actual. EQ -3 OR &l_actual. EQ -4) %THEN %DO;
77  %LET l_errmsg =Parameter i_recordsExp does not contain a number;
78  %goto Update;
79  %END;
80 
81  %*** check for valid parameter i_operator***;
82  DATA _NULL_;
83  IF NOT("&i_operator." IN ("EQ", "NE", "GT", "LT", "GE", "LE", "=", "<", ">", ">=", "<=", "~=")) THEN call symput('l_actual',"-5");
84  RUN;
85  %IF (&l_actual. EQ -5) %THEN %DO;
86  %LET l_errmsg =Parameter i_opertaor contains an invalid operator;
87  %goto Update;
88  %END;
89 
90  %*************************************************************;
91  %*** start tests ***;
92  %*************************************************************;
93 
94  %*** Determine results***;
95  PROC SQL noprint;
96  select count(*) into :l_actual
97  from &l_dsname.
98  where &i_where.
99  ;
100  QUIT;
101  %IF (&SQLRC. NE 0) %THEN %DO;
102  %LET l_actual = -2;
103  %LET l_result = 2;
104  %goto Update;
105  %END;
106 
107  %*** Determine results***;
108  %IF (&l_actual. &i_operator. &i_recordsExp. AND &l_actual. NE -999) %THEN %DO;
109  %LET l_result = 0;
110  %END;
111 
112 %Update:
113  *** update result in test database ***;
114  %_asserts(i_type = assertRecordCount
115  ,i_expected = %str(&i_operator. &i_recordsExp.)
116  ,i_actual = %str(&l_actual.)
117  ,i_desc = &i_desc.
118  ,i_result = &l_result.
119  ,i_errmsg = &l_errmsg.;
120  )
121 %MEND;