Loading...
Searching...
No Matches
stagedata.test.4.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief testing the derived-variable submit guards (MPE_DERIVED post edit hook)
4 @details Backend validations should abort when:
5 1. an active row's VAR_DEPS names an inactive (active=0) row of the same
6 table - the engine reads only active rows, so the dependency can never
7 resolve at run time
8 2. a row is staged with active=0 while a surviving active row depends on it
9 3. a staged row breaks a field rule of the shared scan (core's
10 mp_derivedvarcheck), or carries an ACTIVE value the EDIT screen's picker
11 cannot produce
12
13 They should NOT abort when:
14 4. a new active row is added to a table that also holds an inactive row -
15 the inactive row is not part of the dependency graph, so it cannot
16 create a cycle or a stale reference
17 5. a delete-marked RLS rule naming a derived variable is submitted - the
18 rule is being removed, so its variables are not validated
19 6. a delete and re-add of the same variable is submitted together - the
20 re-added active row resolves the dependency
21 7. an inactive row is set active again while another active row depends on
22 it - the re-activation is what makes the dependency resolvable
23
24 The staged loads are aborted by the post edit hook. The assertion fetches the
25 sasjsAbort object that mp_abort writes to the webout before the hook aborts.
26
27 <h4> SAS Macros </h4>
28 @li mpe_execute.sas
29 @li mpe_assert_abort.sas
30 @li mp_assert.sas
31
32
33**/
34
35%let _program=&appLoc/services/editors/stagedata;
36
37/**
38 * Prep - a target table registration for the derived configuration, and the
39 * hook registration (some estates were initialised before the hook existed)
40 */
41proc sql noprint;
42update &dc_libref..mpe_tables
43 set post_edit_hook='services/hooks/mpe_derived_postedit'
44 where upcase(dsn)='MPE_DERIVED' and post_edit_hook='';
45delete from &dc_libref..mpe_derived
46 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
47quit;
48
49/**
50 * Test 1 - a new active row depending on an inactive row must be rejected
51 */
52%let t1=%sysfunc(datetime());
53
54proc sql noprint;
55insert into &dc_libref..mpe_derived
56 set tx_from=0
57 ,tx_to='31DEC5999:23:59:59'dt
58 ,libref='DCTEST'
59 ,dsn='DC_DERIV'
60 ,var_nm='INACTIVE_DEP'
61 ,var_deps=''
62 ,var_type='N'
63 ,var_len=8
64 ,var_fmt=''
65 ,var_logic='1'
66 ,active=0;
67quit;
68
69data work.sascontroltable;
70 action='LOAD';
71 message='Derived var depending on an inactive row should abort';
72 libds="&dc_libref..MPE_DERIVED";
73 output;
74 stop;
75run;
76
77data work.jsdata;
78 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
79 VAR_LOGIC $4000 VAR_FMT $32
80 _____DELETE__THIS__RECORD_____ $3;
81 LIBREF='DCTEST';
82 DSN='DC_DERIV';
83 VAR_NM='USES_INACTIVE';
84 VAR_DEPS='INACTIVE_DEP';
85 VAR_TYPE='N';
86 VAR_LEN=8;
87 VAR_FMT='';
88 VAR_LOGIC='INACTIVE_DEP + 1';
89 ACTIVE=1;
90 _____DELETE__THIS__RECORD_____='No';
91 output;
92run;
93
94%mpe_execute(&_program,
95 viyacontext=&defaultcontext,
96 inputdatasets=work.sascontroltable work.jsdata,
97 outlib=web1,
98 outref=wb1,
99 outstatus=work.ab1,
100 failfast=0,
101 mdebug=&sasjs_mdebug
102)
103
104%mpe_assert_abort(
105 desc=Checking a dependency on an inactive derived row is rejected,
106 outstatus=work.ab1
107)
108
109/**
110 * Test 2 - deactivating a row that a surviving active row depends on must be
111 * rejected: the control table still shows the row as active, so the check has
112 * to read the staged row too
113 */
114
115proc sql noprint;
116delete from &dc_libref..mpe_derived
117 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
118insert into &dc_libref..mpe_derived
119 set tx_from=0
120 ,tx_to='31DEC5999:23:59:59'dt
121 ,libref='DCTEST'
122 ,dsn='DC_DERIV'
123 ,var_nm='BASE_X'
124 ,var_deps=''
125 ,var_type='N'
126 ,var_len=8
127 ,var_fmt=''
128 ,var_logic='1'
129 ,active=1;
130insert into &dc_libref..mpe_derived
131 set tx_from=0
132 ,tx_to='31DEC5999:23:59:59'dt
133 ,libref='DCTEST'
134 ,dsn='DC_DERIV'
135 ,var_nm='USES_BASE'
136 ,var_deps='BASE_X'
137 ,var_type='N'
138 ,var_len=8
139 ,var_fmt=''
140 ,var_logic='BASE_X + 1'
141 ,active=1;
142quit;
143
144data work.sascontroltable;
145 action='LOAD';
146 message='Deactivating a relied-on derived row should abort';
147 libds="&dc_libref..MPE_DERIVED";
148 output;
149 stop;
150run;
151
152data work.jsdata;
153 set &dc_libref..mpe_derived(where=(upcase(libref)='DCTEST'
154 and upcase(var_nm)='BASE_X'));
155 length _____DELETE__THIS__RECORD_____ $3;
156 ACTIVE=0;
157 _____DELETE__THIS__RECORD_____='No';
158run;
159
160%mpe_execute(&_program,
161 viyacontext=&defaultcontext,
162 inputdatasets=work.sascontroltable work.jsdata,
163 outlib=web2,
164 outref=wb2,
165 outstatus=work.ab2,
166 failfast=0,
167 mdebug=&sasjs_mdebug
168)
169
170%mpe_assert_abort(
171 desc=Checking deactivating a relied-on derived row is rejected,
172 outstatus=work.ab2
173)
174
175/**
176 * Test 3 - an inactive row must not block a submission that does not depend
177 * on it: with INACTIVE_DEP present (active=0) a new independent row loads
178 */
179%let t3=%sysfunc(datetime());
180
181proc sql noprint;
182delete from &dc_libref..mpe_derived
183 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
184insert into &dc_libref..mpe_derived
185 set tx_from=0
186 ,tx_to='31DEC5999:23:59:59'dt
187 ,libref='DCTEST'
188 ,dsn='DC_DERIV'
189 ,var_nm='INACTIVE_DEP'
190 ,var_deps=''
191 ,var_type='N'
192 ,var_len=8
193 ,var_fmt=''
194 ,var_logic='1'
195 ,active=0;
196quit;
197
198data work.sascontroltable;
199 action='LOAD';
200 message='Active row next to an inactive row should load';
201 libds="&dc_libref..MPE_DERIVED";
202 output;
203 stop;
204run;
205
206data work.jsdata;
207 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
208 VAR_LOGIC $4000 VAR_FMT $32
209 _____DELETE__THIS__RECORD_____ $3;
210 LIBREF='DCTEST';
211 DSN='DC_DERIV';
212 VAR_NM='FRESH';
213 VAR_DEPS='X';
214 VAR_TYPE='N';
215 VAR_LEN=8;
216 VAR_FMT='';
217 VAR_LOGIC='X + 1';
218 ACTIVE=1;
219 _____DELETE__THIS__RECORD_____='No';
220 output;
221run;
222
223%mpe_execute(&_program,
224 viyacontext=&defaultcontext,
225 inputdatasets=work.sascontroltable work.jsdata,
226 outlib=web3,
227 outref=wb3,
228 mdebug=&sasjs_mdebug
229)
230
231%let status3=0;
232data _null_;
233 set web3.sasparams;
234 putlog (_all_)(=);
235 if status='SUCCESS' then call symputx('status3',1);
236run;
237
238%mp_assert(
239 iftrue=(&status3=1),
240 desc=Checking an inactive row does not block an unrelated derived variable,
241 outds=work.test_results
242)
243
244/**
245 * Test 4 - a delete-marked RLS rule naming a derived variable is a removal,
246 * so it must not abort: the rule is gone by the time the guards run
247 */
248%let t4=%sysfunc(datetime());
249
250proc sql noprint;
251delete from &dc_libref..mpe_derived
252 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
253insert into &dc_libref..mpe_derived
254 set tx_from=0
255 ,tx_to='31DEC5999:23:59:59'dt
256 ,libref='DCTEST'
257 ,dsn='DC_DERIV'
258 ,var_nm='DERIVED_X'
259 ,var_deps=''
260 ,var_type='N'
261 ,var_len=8
262 ,var_fmt=''
263 ,var_logic='1'
264 ,active=1;
265delete from &dc_libref..mpe_row_level_security
266 where upcase(rls_libref)='DCTEST' and upcase(rls_table)='DC_DERIV';
267select coalesce(max(rls_rk),0)+1 into: rlsrk
268 from &dc_libref..mpe_row_level_security;
269insert into &dc_libref..mpe_row_level_security
270 set tx_from=0
271 ,tx_to='31DEC5999:23:59:59'dt
272 ,rls_rk=&rlsrk
273 ,rls_scope='EDIT'
274 ,rls_group='SASAdministrators'
275 ,rls_libref='DCTEST'
276 ,rls_table='DC_DERIV'
277 ,rls_group_logic='AND'
278 ,rls_subgroup_logic='OR'
279 ,rls_subgroup_id=0
280 ,rls_variable_nm='DERIVED_X'
281 ,rls_operator_nm='NE'
282 ,rls_raw_value='1'
283 ,rls_active=1;
284quit;
285
286data work.sascontroltable;
287 action='LOAD';
288 message='Delete-marked RLS rule on a derived variable should load';
289 libds="&dc_libref..MPE_ROW_LEVEL_SECURITY";
290 output;
291 stop;
292run;
293
294data work.jsdata;
295 set &dc_libref..mpe_row_level_security(where=(upcase(rls_libref)='DCTEST'
296 and upcase(rls_table)='DC_DERIV'));
297 length _____DELETE__THIS__RECORD_____ $3;
298 _____DELETE__THIS__RECORD_____='Yes';
299run;
300
301%mpe_execute(&_program,
302 viyacontext=&defaultcontext,
303 inputdatasets=work.sascontroltable work.jsdata,
304 outlib=web4,
305 outref=wb4,
306 mdebug=&sasjs_mdebug
307)
308
309%let status4=0;
310data _null_;
311 set web4.sasparams;
312 putlog (_all_)(=);
313 if status='SUCCESS' then call symputx('status4',1);
314run;
315
316%mp_assert(
317 iftrue=(&status4=1),
318 desc=Checking a delete-marked RLS rule on a derived variable is not rejected,
319 outds=work.test_results
320)
321
322/**
323 * Test 5 - deleting a derived row that a surviving active row depends on must
324 * be rejected: the delete is not in the control table yet and the staged copy
325 * is delete-marked, so the inactive set has to admit it
326 */
327%let t5=%sysfunc(datetime());
328
329proc sql noprint;
330delete from &dc_libref..mpe_derived
331 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
332insert into &dc_libref..mpe_derived
333 set tx_from=0
334 ,tx_to='31DEC5999:23:59:59'dt
335 ,libref='DCTEST'
336 ,dsn='DC_DERIV'
337 ,var_nm='BASE_X'
338 ,var_deps=''
339 ,var_type='N'
340 ,var_len=8
341 ,var_fmt=''
342 ,var_logic='1'
343 ,active=1;
344insert into &dc_libref..mpe_derived
345 set tx_from=0
346 ,tx_to='31DEC5999:23:59:59'dt
347 ,libref='DCTEST'
348 ,dsn='DC_DERIV'
349 ,var_nm='USES_BASE'
350 ,var_deps='BASE_X'
351 ,var_type='N'
352 ,var_len=8
353 ,var_fmt=''
354 ,var_logic='BASE_X + 1'
355 ,active=1;
356quit;
357
358data work.sascontroltable;
359 action='LOAD';
360 message='Deleting a relied-on derived row should abort';
361 libds="&dc_libref..MPE_DERIVED";
362 output;
363 stop;
364run;
365
366data work.jsdata;
367 set &dc_libref..mpe_derived(where=(upcase(libref)='DCTEST'
368 and upcase(var_nm)='BASE_X'));
369 length _____DELETE__THIS__RECORD_____ $3;
370 _____DELETE__THIS__RECORD_____='Yes';
371run;
372
373%mpe_execute(&_program,
374 viyacontext=&defaultcontext,
375 inputdatasets=work.sascontroltable work.jsdata,
376 outlib=web5,
377 outref=wb5,
378 outstatus=work.ab5,
379 failfast=0,
380 mdebug=&sasjs_mdebug
381)
382
383%mpe_assert_abort(
384 desc=Checking deleting a relied-on derived row is rejected,
385 outstatus=work.ab5
386)
387
388/**
389 * Test 6 - a delete and re-add of the same variable in one submission must be
390 * accepted: the re-added active row resolves the dependency. Only a stored
391 * name that is not already uppercase reaches submit this way - the grid's
392 * CASE=UPCASE rule is a validator, and the post edit hook upcases every
393 * non-delete staged row, so a value typed in the grid arrives uppercase and a
394 * delete plus re-add of it is a duplicate primary key (rejected at approval by
395 * mpe_loader's raw-PK dup check). The case variant therefore pins the shape
396 * for a name written outside the editor.
397 */
398proc sql noprint;
399delete from &dc_libref..mpe_derived
400 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
401insert into &dc_libref..mpe_derived
402 set tx_from=0
403 ,tx_to='31DEC5999:23:59:59'dt
404 ,libref='DCTEST'
405 ,dsn='DC_DERIV'
406 ,var_nm='BASE_X'
407 ,var_deps=''
408 ,var_type='N'
409 ,var_len=8
410 ,var_fmt=''
411 ,var_logic='1'
412 ,active=1;
413insert into &dc_libref..mpe_derived
414 set tx_from=0
415 ,tx_to='31DEC5999:23:59:59'dt
416 ,libref='DCTEST'
417 ,dsn='DC_DERIV'
418 ,var_nm='USES_BASE'
419 ,var_deps='BASE_X'
420 ,var_type='N'
421 ,var_len=8
422 ,var_fmt=''
423 ,var_logic='BASE_X + 1'
424 ,active=1;
425quit;
426
427data work.sascontroltable;
428 action='LOAD';
429 message='Deleting and re-adding a relied-on derived row should not abort';
430 libds="&dc_libref..MPE_DERIVED";
431 output;
432 stop;
433run;
434
435data work.jsdata;
436 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
437 VAR_LOGIC $4000 VAR_FMT $32
438 _____DELETE__THIS__RECORD_____ $3;
439 LIBREF='DCTEST';
440 DSN='DC_DERIV';
441 VAR_NM='Base_X';
442 VAR_DEPS='';
443 VAR_TYPE='N';
444 VAR_LEN=8;
445 VAR_FMT='';
446 VAR_LOGIC='1';
447 ACTIVE=1;
448 _____DELETE__THIS__RECORD_____='Yes';
449 output;
450 VAR_NM='BASE_X';
451 _____DELETE__THIS__RECORD_____='No';
452 output;
453run;
454
455%mpe_execute(&_program,
456 viyacontext=&defaultcontext,
457 inputdatasets=work.sascontroltable work.jsdata,
458 outlib=web6,
459 outref=wb6,
460 mdebug=&sasjs_mdebug
461)
462
463%let status6=0;
464data _null_;
465 set web6.sasparams;
466 putlog (_all_)(=);
467 if status='SUCCESS' then call symputx('status6',1);
468run;
469
470%mp_assert(
471 iftrue=(&status6=1),
472 desc=Checking a delete and re-add of the same derived variable is accepted,
473 outds=work.test_results
474)
475
476/**
477 * Test 7 - re-activating an inactive row must be accepted when another active
478 * row depends on it: the control table still holds the inactive copy, and the
479 * dependency resolves against the row this submission stages. Like Test 6 the
480 * starting state is out of band - the hook refuses to deactivate a row an
481 * active row depends on - so the check must not block the submission that
482 * repairs it.
483 */
484proc sql noprint;
485delete from &dc_libref..mpe_derived
486 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
487insert into &dc_libref..mpe_derived
488 set tx_from=0
489 ,tx_to='31DEC5999:23:59:59'dt
490 ,libref='DCTEST'
491 ,dsn='DC_DERIV'
492 ,var_nm='BASE_X'
493 ,var_deps=''
494 ,var_type='N'
495 ,var_len=8
496 ,var_fmt=''
497 ,var_logic='1'
498 ,active=0;
499insert into &dc_libref..mpe_derived
500 set tx_from=0
501 ,tx_to='31DEC5999:23:59:59'dt
502 ,libref='DCTEST'
503 ,dsn='DC_DERIV'
504 ,var_nm='USES_BASE'
505 ,var_deps='BASE_X'
506 ,var_type='N'
507 ,var_len=8
508 ,var_fmt=''
509 ,var_logic='BASE_X + 1'
510 ,active=1;
511quit;
512
513data work.sascontroltable;
514 action='LOAD';
515 message='Re-activating a relied-on derived row should not abort';
516 libds="&dc_libref..MPE_DERIVED";
517 output;
518 stop;
519run;
520
521data work.jsdata;
522 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
523 VAR_LOGIC $4000 VAR_FMT $32
524 _____DELETE__THIS__RECORD_____ $3;
525 LIBREF='DCTEST';
526 DSN='DC_DERIV';
527 VAR_NM='BASE_X';
528 VAR_DEPS='';
529 VAR_TYPE='N';
530 VAR_LEN=8;
531 VAR_FMT='';
532 VAR_LOGIC='1';
533 ACTIVE=1;
534 _____DELETE__THIS__RECORD_____='No';
535run;
536
537%mpe_execute(&_program,
538 viyacontext=&defaultcontext,
539 inputdatasets=work.sascontroltable work.jsdata,
540 outlib=web7,
541 outref=wb7,
542 mdebug=&sasjs_mdebug
543)
544
545%let status7=0;
546data _null_;
547 set web7.sasparams;
548 putlog (_all_)(=);
549 if status='SUCCESS' then call symputx('status7',1);
550run;
551
552%mp_assert(
553 iftrue=(&status7=1),
554 desc=Checking re-activating a relied-on derived row is accepted,
555 outds=work.test_results
556)
557
558
559/**
560 * Test 8 - a field rule of the shared scan (mp_derivedvarcheck, core) is
561 * applied at submit time: an expression that could submit code of its own is
562 * refused by name, with the reason the scan gives. This is the same scan the
563 * engine runs at read time, so the case pins that the hook calls it rather
564 * than carrying a rule list of its own.
565 */
566data work.sascontroltable;
567 action='LOAD';
568 message='A refused function in VAR_LOGIC should abort';
569 libds="&dc_libref..MPE_DERIVED";
570 output;
571 stop;
572run;
573
574data work.jsdata;
575 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
576 VAR_LOGIC $4000 VAR_FMT $32
577 _____DELETE__THIS__RECORD_____ $3;
578 LIBREF='DCTEST';
579 DSN='DC_DERIV';
580 VAR_NM='CALLER';
581 VAR_DEPS='';
582 VAR_TYPE='N';
583 VAR_LEN=8;
584 VAR_FMT='';
585 /* no semicolon in the payload: the single-expression rule is tested before
586 the function-name rule, so a quoted semicolon would abort on that first */
587 VAR_LOGIC='dosubl("data _null_")';
588 ACTIVE=1;
589 _____DELETE__THIS__RECORD_____='No';
590run;
591
592/* the child is expected to abort here, so the failure must be asserted rather
593 than aborting this program - hence failfast=0 */
594%mpe_execute(&_program,
595 viyacontext=&defaultcontext,
596 inputdatasets=work.sascontroltable work.jsdata,
597 outlib=web8,
598 outref=wb8,
599 outstatus=work.ab8,
600 failfast=0,
601 mdebug=&sasjs_mdebug
602)
603
604/* the assertion compares the abort's MSG exactly, so the expected value is
605 assembled on one line: a literal wrapped across two source lines carries the
606 newline and the continuation indent with it.
607 The text has no space after either colon because that is how the child builds
608 it - mpe_derived emits &mac.:&VAR_NM.:&reason, and the reason itself is
609 'VAR_LOGIC may not call a function that' || ' reaches the file system, ...'.
610 Spacing here is the child's, not a formatting choice. */
611%let exp8=MPE_DERIVED:CALLER:VAR_LOGIC may not call a function that;
612%let exp8=&exp8 submits code or reaches the file system,;
613%let exp8=&exp8 the environment or the macro symbol table;
614
615%mpe_assert_abort(
616 desc=Checking a refused function in VAR_LOGIC is rejected,
617 outstatus=work.ab8,
618 msg=%superq(exp8)
619)
620
621/**
622 * Test 9 - the fields the hook owns rather than the scan: ACTIVE is a picker
623 * in the EDIT screen (HARDSELECT over the MPE_SELECTBOX pair), so a submission
624 * that bypasses the grid cannot land a value the screen would never offer.
625 */
626data work.sascontroltable;
627 action='LOAD';
628 message='An ACTIVE value the screen cannot offer should abort';
629 libds="&dc_libref..MPE_DERIVED";
630 output;
631 stop;
632run;
633
634data work.jsdata;
635 length LIBREF $8 DSN $32 VAR_NM $32 VAR_DEPS $4000 VAR_TYPE $1
636 VAR_LOGIC $4000 VAR_FMT $32
637 _____DELETE__THIS__RECORD_____ $3;
638 LIBREF='DCTEST';
639 DSN='DC_DERIV';
640 VAR_NM='BADACT';
641 VAR_DEPS='';
642 VAR_TYPE='N';
643 VAR_LEN=8;
644 VAR_FMT='';
645 VAR_LOGIC='1';
646 ACTIVE=2;
647 _____DELETE__THIS__RECORD_____='No';
648run;
649
650/* this child is expected to abort too - assert the failure rather than
651 cancelling this program */
652%mpe_execute(&_program,
653 viyacontext=&defaultcontext,
654 inputdatasets=work.sascontroltable work.jsdata,
655 outlib=web9,
656 outref=wb9,
657 outstatus=work.ab9,
658 failfast=0,
659 mdebug=&sasjs_mdebug
660)
661
662%mpe_assert_abort(
663 desc=Checking an ACTIVE value outside the picker pair is rejected,
664 outstatus=work.ab9,
665 msg=MPE_DERIVED:BADACT:ACTIVE must be 1 or 0
666)
667
668/* tidy up so repeated runs start clean */
669proc sql noprint;
670delete from &dc_libref..mpe_derived
671 where upcase(libref)='DCTEST' and upcase(dsn)='DC_DERIV';
672delete from &dc_libref..mpe_row_level_security
673 where upcase(rls_libref)='DCTEST' and upcase(rls_table)='DC_DERIV';
674quit;