Macros for SAS Application Developers
https://github.com/sasjs/core
mp_hashdirectory.test.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Testing mp_hashdirectory.sas macro
4 
5 
6  <h4> SAS Macros </h4>
7  @li mf_mkdir.sas
8  @li mf_nobs.sas
9  @li mp_assert.sas
10  @li mp_assertscope.sas
11  @li mp_hashdirectory.sas
12 
13 **/
14 
15 /* set up a directory to hash */
16 %let fpath=%sysfunc(pathname(work))/testdir;
17 
18 %mf_mkdir(&fpath)
19 %mf_mkdir(&fpath/sub1)
20 %mf_mkdir(&fpath/sub2)
21 %mf_mkdir(&fpath/sub1/subsub)
22 
23 /* note - the path in the file means the hash is different in each run */
24 %macro makefile(path,name);
25  data _null_;
26  file "&path/&name" termstr=lf;
27  put "This file is located at:";
28  put "&path";
29  put "and it is called:";
30  put "&name";
31  run;
32 %mend makefile;
33 
34 %macro spawner(path);
35  %do x=1 %to 5;
36  %makefile(&path,file&x..txt)
37  %end;
38 %mend spawner;
39 
40 %spawner(&fpath)
41 %spawner(&fpath/sub1)
42 %spawner(&fpath/sub1/subsub)
43 
44 
45 %mp_assertscope(SNAPSHOT)
46 %mp_hashdirectory(&fpath,outds=work.hashes,maxdepth=MAX)
47 %mp_assertscope(COMPARE)
48 
49 %mp_assert(
50  iftrue=(&syscc=0),
51  desc=No errors,
52  outds=work.test_results
53 )
54 
55 %mp_assert(
56  iftrue=(%mf_nobs(work.hashes)=19),
57  desc=record created for each entry,
58  outds=work.test_results
59 )
60 
61 proc sql;
62 select count(*) into: misscheck
63  from work.hashes
64  where file_hash is missing;
65 
66 %mp_assert(
67  iftrue=(&misscheck=1),
68  desc=Only one missing hash - the empty directory,
69  outds=work.test_results
70 )
71 
72 data _null_;
73  set work.hashes;
74  if directory=file_path then call symputx('tophash',file_hash);
75 run;
76 
77 %mp_assert(
78  iftrue=(%length(&tophash)=32),
79  desc=ensure valid top level hash created,
80  outds=work.test_results
81 )
82 
83 /* now change a file and re-hash */
84 data _null_;
85  file "&fpath/sub1/subsub/file1.txt" termstr=lf;
86  put "This file has changed!";
87 run;
88 
89 %mp_hashdirectory(&fpath,outds=work.hashes2,maxdepth=MAX)
90 
91 data _null_;
92  set work.hashes2;
93  if directory=file_path then call symputx('tophash2',file_hash);
94 run;
95 
96 %mp_assert(
97  iftrue=(&tophash ne &tophash2),
98  desc=ensure the changing of the hash results in a new value,
99  outds=work.test_results
100 )
101 
102 /* now change it back and see if it matches */
103 data _null_;
104  file "&fpath/sub1/subsub/file1.txt" termstr=lf;
105  put "This file is located at:";
106  put "&fpath/sub1/subsub";
107  put "and it is called:";
108  put "file1.txt";
109  run;
110 run;
111 
112 %mp_hashdirectory(&fpath,outds=work.hashes3,maxdepth=MAX)
113 
114 data _null_;
115  set work.hashes3;
116  if directory=file_path then call symputx('tophash3',file_hash);
117 run;
118 
119 %mp_assert(
120  iftrue=(&tophash=&tophash3),
121  desc=ensure the same files result in the same hash,
122  outds=work.test_results
123 )
124 
125 /* dump contents for debugging */
126 data _null_;
127  set work.hashes;
128  put file_hash file_path;
129 run;
130 data _null_;
131  set work.hashes2;
132  put file_hash file_path;
133 run;
134 
135 /* check that it works when the target directory is missing */
136 
137 %mp_hashdirectory(&fpath/doesnotexist,outds=work.hashes3,maxdepth=MAX)
138 
139 %mp_assert(
140  iftrue=(&syscc=0),
141  desc=No errors when directory is missing,
142  outds=work.test_results
143 )
144 
145 %mp_assert(
146  iftrue=(%mf_nobs(work.hashes3)=0),
147  desc=no records created when directory is missing,
148  outds=work.test_results
149 )