Published on :
Test / Macro INTERNAL_CREATION

Unit tests for mp_zip and mp_unzip

This code is also available in: Deutsch Español Français
Awaiting validation
This program executes a series of three tests to verify the robustness of SASjs compression macros (mp_zip and mp_unzip). The scenarios covered are: 1) Compressing a single text file. 2) Compressing a list of files referenced in a SAS© table. 3) Compressing an entire directory. Each test is followed by a decompression step and an assertion (mp_assert) comparing the content of the extracted files with the original content to ensure data integrity.
Data Analysis

Type : INTERNAL_CREATION


All test data and files are dynamically generated in the WORK directory or a temporary sub-directory ('zipme') during script execution.

1 Code Block
DATA STEP Data
Explanation :
Initialization of the test environment (paths) and creation of a dummy text file for the first scenario.
Copied!
1%let work=%sysfunc(pathname(work));
2%let root=&work/zipme;
3 
4/* TEST 1 - zip a file */
5%mf_mkdir(&root)
6 
7DATA _null_;
8 file "&root/test.txt";
9 put "houston, this is a test";
10RUN;
2 Code Block
MACRO
Explanation :
Execution of Test 1: Compression of the single file, decompression, reading the result, and validation via mp_assert.
Copied!
1%mp_zip(in=&root/test.txt
2 ,type=FILE
3 ,outpath=&work
4 ,outname=myFile
5)
6 
7%mp_unzip(ziploc="&work/myFile.zip",outdir=&work)
8 
9DATA _null_;
10 INFILE "&work/test.txt";
11 INPUT;
12 call symputx('content1',_infile_);
13 putlog _infile_;
14RUN;
15 
16%mp_assert(
17 iftrue=(
18 %str(&content1)=%str(houston, this is a test)
19 ),
20 desc=Checking IF file zip / unzip works,
21 outds=work.test_results
22)
3 Code Block
DATA STEP Data
Explanation :
Preparation of Test 2: Creation of a second file and a SAS table containing the list of file paths to compress.
Copied!
1/* TEST 2 - zip a dataset of files */
2DATA _null_;
3 file "&root/test2.txt";
4 put "houston, this is test2";
5RUN;
6LIBNAME tmp "&root";
7DATA tmp.test;
8 filepath="&root/test2.txt";
9RUN;
4 Code Block
MACRO
Explanation :
Execution of Test 2: Compression based on the SAS table (type=DATASET), decompression, and content validation.
Copied!
1%mp_zip(in=tmp.test
2 ,incol=filepath
3 ,type=DATASET
4 ,outpath=&work
5 ,outname=myFile2
6)
7 
8%mp_unzip(ziploc="&work/myFile2.zip",outdir=&work)
9 
10DATA _null_;
11 INFILE "&work/test2.txt";
12 INPUT;
13 call symputx('content2',_infile_);
14 putlog _infile_;
15RUN;
16 
17%mp_assert(
18 iftrue=(
19 %str(&content2)=%str(houston, this is test2)
20 ),
21 desc=Checking IF file zip / unzip from a dataset works,
22 outds=work.test_results
23)
5 Code Block
MACRO
Explanation :
Execution of Test 3: Compression of an entire directory (type=DIRECTORY), decompression into a new 'out3' folder, and validation of the presence of the two original files.
Copied!
1/* TEST 3 - zip a dataset of files */
2%mf_mkdir(&work/out3)
3 
4%mp_zip(in=&root
5 ,type=DIRECTORY
6 ,outpath=&work
7 ,outname=myFile3
8)
9 
10%mp_unzip(ziploc="&work/myFile3.zip",outdir=&work/out3)
11 
12DATA _null_;
13 INFILE "&work/out3/test.txt";
14 INPUT;
15 call symputx('content3a',_infile_);
16 putlog _infile_;
17RUN;
18DATA _null_;
19 INFILE "&work/out3/test2.txt";
20 INPUT;
21 call symputx('content3b',_infile_);
22 putlog _infile_;
23RUN;
24 
25%mp_assert(
26 iftrue=(
27 %str(&content3a)=%str(houston, this is a test)
28 and
29 %str(&content3b)=%str(houston, this is test2)
30 ),
31 desc=Checking IF file zip / unzip from a directory works,
32 outds=work.test_results
33)
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.