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