Loading...
Searching...
No Matches
mpe_derived.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief testing the mpe_derived macro
4
5 @details Covers the generated statements, the view, the metadata the callers
6 consume, and the dependency rules.
7
8 The configuration checks abort the run when they fail, so they are not
9 exercised here - this suite covers the behaviour that callers depend on.
10
11 <h4> SAS Macros </h4>
12 @li mf_existds.sas
13 @li mp_assert.sas
14 @li mp_assertcols.sas
15 @li mp_assertdsobs.sas
16 @li mp_assertscope.sas
17 @li mpe_derived.sas
18
19 @author 4GL Apps Ltd
20 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
21 and may not be re-distributed or re-sold without the express permission of
22 4GL Apps Ltd.
23
24 @sasjslint {"maxLineLength": 119}
25
26**/
27
28
29/* a source table, and a table with no derived variables */
30data work.class;
31 set sashelp.class;
32run;
33data work.noconfig;
34 set sashelp.class;
35run;
36
37/* clean up any config left behind by an earlier run */
38proc sql;
39delete from &dclib..mpe_derived where upcase(libref)='WORK';
40quit;
41
42/**
43 * Test 1 - statements, view, chaining
44 */
45
46proc sql;
47insert into &dclib..mpe_derived set
48 tx_from=0
49 ,tx_to='31DEC5999:23:59:59'dt
50 ,libref='WORK'
51 ,dsn='CLASS'
52 ,var_nm='BAND'
53 ,var_deps='AGE'
54 ,var_type='C'
55 ,var_len=8
56 ,var_fmt=''
57 ,var_label='Age band'
58 ,var_logic="ifc(AGE>13,'TEEN','JUNIOR')"
59 ,active=1;
60insert into &dclib..mpe_derived set
61 tx_from=0
62 ,tx_to='31DEC5999:23:59:59'dt
63 ,libref='WORK'
64 ,dsn='CLASS'
65 ,var_nm='BAND_AGE'
66 ,var_deps='BAND AGE'
67 ,var_type='C'
68 ,var_len=16
69 ,var_fmt=''
70 ,var_logic="cats(BAND,'-',AGE)"
71 ,active=1;
72insert into &dclib..mpe_derived set
73 tx_from=0
74 ,tx_to='31DEC5999:23:59:59'dt
75 ,libref='WORK'
76 ,dsn='CLASS'
77 ,var_nm='DOUBLE_AGE'
78 ,var_deps='AGE'
79 ,var_type='N'
80 ,var_len=8
81 ,var_fmt=''
82 ,var_logic='AGE * 2'
83 ,active=1;
84quit;
85
86/* lower case libds on purpose - the config match must be case insensitive */
87/* the setup SQL above creates the SQL automatic macro variables, so the
88 snapshot is taken here - the check is for the macro's own leakage */
89%mp_assertscope(SNAPSHOT)
90
91%mpe_derived(libds=work.class,
92 dclib=&dclib,
93 outref=derref1,
94 outview=work.vw_derived,
95 outvars=work.derived_vars
96)
97
98%mp_assertdsobs(work.derived_vars,
99 desc=Test1 - one row per derived variable,
100 test=EQUALS 3,
101 outds=work.test_results
102)
103
104%mp_assertcols(work.vw_derived,
105 desc=Test1 - the view carries the derived columns,
106 cols=BAND BAND_AGE DOUBLE_AGE,
107 test=ALL,
108 outds=work.test_results
109)
110
111data work.chk1;
112 set work.vw_derived;
113 where BAND='TEEN';
114run;
115%mp_assertdsobs(work.chk1,
116 desc=Test1 - the filter on a derived column selects rows,
117 test=HASOBS,
118 outds=work.test_results
119)
120
121/* the chained value only resolves if the statements run in order */
122data work.chk1b;
123 set work.vw_derived;
124 where BAND='TEEN';
125 if BAND_AGE ne cats(BAND,'-',AGE) then output;
126run;
127%mp_assertdsobs(work.chk1b,
128 desc=Test1 - a derived variable can reference an earlier one,
129 test=EQUALS 0,
130 outds=work.test_results
131)
132
133data work.chk1c;
134 set work.vw_derived;
135 if DOUBLE_AGE ne AGE*2 then output;
136run;
137%mp_assertdsobs(work.chk1c,
138 desc=Test1 - the numeric derived value is correct,
139 test=EQUALS 0,
140 outds=work.test_results
141)
142
143/* proc contents on a view reports a format name without its width, so the
144 format is compared on the name only */
145proc contents noprint data=work.vw_derived
146 out=work.vw_cols(keep=name label format);
147run;
148data work.chk1d;
149 set work.vw_cols;
150 where name='BAND' and label='Age band' and format=:'$';
151run;
152%mp_assertdsobs(work.chk1d,
153 desc=Test1 - the label and default format are applied,
154 test=EQUALS 1,
155 outds=work.test_results
156)
157
158/**
159 * Test 2 - a configured format is used as-is
160 */
161
162proc sql;
163insert into &dclib..mpe_derived set
164 tx_from=0
165 ,tx_to='31DEC5999:23:59:59'dt
166 ,libref='WORK'
167 ,dsn='CLASS'
168 ,var_nm='FLAG_DT'
169 ,var_deps=''
170 ,var_type='N'
171 ,var_len=8
172 ,var_fmt='DATE9.'
173 ,var_logic='mdy(1,1,2020)'
174 ,active=1;
175quit;
176
177%mpe_derived(libds=WORK.CLASS,
178 dclib=&dclib,
179 outref=derref2,
180 outview=work.vw_derived2,
181 outvars=work.derived_vars2
182)
183
184/* asserted on the emitted statement - proc contents on a view does not
185 report the format in full */
186data work.chk2;
187 infile derref2 truncover;
188 input line $4000.;
189 if index(line,'format=DATE9.')>0 then output;
190run;
191%mp_assertdsobs(work.chk2,
192 desc=Test2 - the configured format is applied,
193 test=EQUALS 1,
194 outds=work.test_results
195)
196
197data work.chk2b;
198 set work.vw_derived2;
199 if flag_dt ne mdy(1,1,2020) then output;
200 stop;
201run;
202%mp_assertdsobs(work.chk2b,
203 desc=Test2 - the numeric value is correct,
204 test=EQUALS 0,
205 outds=work.test_results
206)
207
208/**
209 * Test 3 - no configuration
210 */
211
212%mpe_derived(libds=WORK.NOCONFIG,
213 dclib=&dclib,
214 outref=derref3,
215 outview=work.vw_derived3,
216 outvars=work.derived_vars3
217)
218
219%mp_assertdsobs(work.derived_vars3,
220 desc=Test3 - no derived variables returned,
221 test=EQUALS 0,
222 outds=work.test_results
223)
224
225%mp_assert(iftrue=(%mf_existds(libds=work.vw_derived3)=0),
226 desc=Test3 - no view is built when there is no config,
227 outds=work.test_results
228)
229
230data _null_;
231 infile derref3 end=eof;
232 input;
233 if not eof or cats(_infile_) ne '' then do;
234 putlog 'ERR' 'OR: derived statement file is not empty';
235 call symputx('emptycheck','0','l');
236 end;
237run;
238%mp_assert(iftrue=(%symexist(emptycheck)=0),
239 desc=Test3 - no statements are generated,
240 outds=work.test_results
241)
242
243/**
244 * Test 4 - the wrapper scopes the config: only the current, active row of a
245 * table is used
246 */
247
248proc sql;
249insert into &dclib..mpe_derived set
250 tx_from=0
251 ,tx_to='01JAN2020:00:00:00'dt
252 ,libref='WORK'
253 ,dsn='CLASS'
254 ,var_nm='CLOSED_OFF'
255 ,var_deps='AGE'
256 ,var_type='N'
257 ,var_len=8
258 ,var_fmt=''
259 ,var_logic='AGE + 1'
260 ,active=1;
261insert into &dclib..mpe_derived set
262 tx_from=0
263 ,tx_to='31DEC5999:23:59:59'dt
264 ,libref='WORK'
265 ,dsn='CLASS'
266 ,var_nm='NOT_ACTIVE'
267 ,var_deps='AGE'
268 ,var_type='N'
269 ,var_len=8
270 ,var_fmt=''
271 ,var_logic='AGE + 2'
272 ,active=0;
273quit;
274
275%mpe_derived(libds=WORK.CLASS,
276 dclib=&dclib,
277 outref=derref4,
278 outview=work.vw_derived4,
279 outvars=work.derived_vars4
280)
281
282data work.chk4;
283 set work.derived_vars4;
284 where name in ('CLOSED_OFF','NOT_ACTIVE');
285run;
286
287%mp_assertdsobs(work.chk4,
288 desc=Test4 - a closed version and an inactive row are ignored,
289 test=EQUALS 0,
290 outds=work.test_results
291)
292
293/* tidy up so repeated runs start clean */
294proc sql;
295delete from &dclib..mpe_derived where upcase(libref)='WORK';
296quit;
297
298%mp_assertscope(COMPARE,ignorelist=_SASJS_WEBOUT_HEADERS,outds=work.test_results)
299
300/* Test 5 - a format catalog is not a dataset: no derived variables, no abort.
301 Callers pass LIBREF.CATALOGNAME-FC (validatefilter, getrawdata, viewdata), and
302 the engine's existence check would otherwise end the service. */
303proc format library=work.dcfmts;
304 value $band 'A'='Alpha';
305run;
306
307%mpe_derived(libds=work.DCFMTS-FC,
308 dclib=&dclib,
309 outref=derref5,
310 outvars=work.derived_vars5
311)
312
313%mp_assertdsobs(work.derived_vars5,
314 desc=Test5 - a format catalog yields no derived variables,
315 test=EQUALS 0,
316 outds=work.test_results
317)
318
319/* The branch has to create the statement file even though it writes nothing,
320 because callers %include it unconditionally. Two things to prove: the branch created the file, and it left it empty.
321 fopen answers the first (0 when the fileref cannot be opened), the loop the
322 second - a single INPUT would stop the step at once on an empty file and never
323 reach the symputx. */
324%let deropen=0;
325%let derlines=-1;
326data _null_;
327 did=fopen('derref5');
328 call symputx('deropen',ifn(did>0,1,0),'l');
329 if did>0 then rc=fclose(did);
330run;
331data _null_;
332 infile derref5 end=eof;
333 n=0;
334 do while(not eof);
335 input;
336 n+1;
337 end;
338 call symputx('derlines',n,'l');
339run;
340
341%mp_assert(
342 iftrue=(&deropen=1 and &derlines=0),
343 desc=Test5 - a format catalog leaves an empty statement file,
344 outds=work.test_results
345)