Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_assertmodel.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Test dataset model and primary key assertions
4
5 <h4> SAS Macros </h4>
6 @li mp_assert.sas
7 @li mp_assertmodel.sas
8 @li mp_assertscope.sas
9
10**/
11
12data work.model;
13 length id 8 region $4 value $12;
14 call missing(id,region,value);
15 stop;
16run;
17
18data work.match;
19 length value $12 region $4 id 8;
20 id=1; region='A'; value='one'; output;
21 id=1; region='B'; value='two'; output;
22run;
23
24data work.extra;
25 set work.match;
26 extra=1;
27run;
28
29data work.wronglen;
30 length id 8 region $4 value $11;
31 call missing(id,region,value);
32 stop;
33run;
34
35data work.duplicate;
36 set work.match;
37 output;
38 if id=1 then output;
39run;
40
41data work.missingkey;
42 set work.match;
43 if id=1 then region=' ';
44run;
45
46/* Keep expected FAIL rows separate from the test harness results. */
47%mp_assertscope(SNAPSHOT)
48%mp_assertmodel(work.match,base=work.model,key=id region,
49 desc=match,outds=work.model_results)
50%mp_assertscope(COMPARE,desc=Model assertion does not leak macro variables)
51%mp_assertmodel(work.model,base=work.model,key=id,
52 desc=empty,outds=work.model_results)
53%mp_assertmodel(work.extra,base=work.model,
54 desc=extra,outds=work.model_results)
55%mp_assertmodel(work.wronglen,base=work.model,
56 desc=length,outds=work.model_results)
57%mp_assertmodel(work.duplicate,base=work.model,key=id region,
58 desc=duplicate,outds=work.model_results)
59%mp_assertmodel(work.missingkey,base=work.model,key=id region,
60 desc=missing_key,outds=work.model_results)
61%mp_assertmodel(work.match,base=work.model,key=absent,
62 desc=unknown_key,outds=work.model_results)
63%mp_assertmodel(work.notfound,base=work.model,
64 desc=missing_input,outds=work.model_results)
65
66data work.expected;
67 length test_description $256 test_result $5 reason $80;
68 infile datalines dlm='|' truncover;
69 input test_description $ test_result $ reason $;
70 datalines;
71match|PASS|Column model and key match
72empty|PASS|Column model and key match
73extra|FAIL|Column count differs
74length|FAIL|Type or length differs
75duplicate|FAIL|Key contains missing or duplicate
76missing_key|FAIL|Key contains missing or duplicate
77unknown_key|FAIL|Key column not found
78missing_input|FAIL|Input dataset cannot be opened
79;
80run;
81
82proc sql noprint;
83 select count(*),count(distinct e.test_description)
84 into :model_rows trimmed,:model_matches trimmed
85 from work.model_results as a left join work.expected as e
86 on a.test_description=e.test_description
87 and a.test_result=e.test_result
88 and index(a.test_comments,strip(e.reason))>0;
89quit;
90%mp_assert(iftrue=(&model_rows=8 and &model_matches=8),
91 desc=Model results match expected pass and fail cases)
92
93/* An overlapping destination is rejected without changing the input. */
94%let model_syntax=%sysfunc(getoption(syntaxcheck,keyword));
95options nosyntaxcheck;
96%mp_assertmodel(work.match,base=work.model,outds=work.match)
97%let model_errorcc=&syscc;
98%let syscc=0;
99options &model_syntax;
100proc sql noprint;
101 select count(*) into :model_input_rows trimmed from work.match;
102quit;
103%mp_assert(iftrue=(&model_errorcc>4 and &model_input_rows=2),
104 desc=Overlapping output is rejected and input preserved)