Published on :

Unit test of the mp_storediffs macro

This code is also available in: Deutsch Español Français
Awaiting validation
This program executes a series of unit tests to verify the robustness of the mp_storediffs macro. It generates test data derived from sashelp.class, applies controlled modifications, and uses assertion macros (mp_assert, mp_assertdsobs, mp_assertcolvals) to validate that the reported differences are accurate. Scenarios include mixed changes, no changes, and deletions only.
Data Analysis

Type : MIXTE


Data is generated locally (WORK library) from the sashelp.class system table via DATA steps.

1 Code Block
DATA STEP Data
Explanation :
Creation of test tables for the first scenario: an original table, and tables simulating deletions, modifications, and additions.
Copied!
1DATA work.orig work.deleted work.changed work.appended;
2 SET sashelp.class;
3 IF _n_=1 THEN DO;
4 OUTPUT work.orig work.deleted;
5 END;
6 ELSE IF _n_=2 THEN DO;
7 OUTPUT work.orig;
8 age=99;
9 OUTPUT work.changed;
10 END;
11 ELSE DO;
12 name='Newbie';
13 OUTPUT work.appended;
14 stop;
15 END;
16RUN;
2 Code Block
MACRO CALL
Explanation :
Call to the mp_storediffs macro to compare sashelp.class with work.orig and generate the differences in work.final.
Copied!
1%mp_storediffs(sashelp.class,work.orig,NAME
2 ,delds=work.deleted
3 ,modds=work.changed
4 ,appds=work.appended
5 ,outds=work.final
6 ,mdebug=1
7)
3 Code Block
MACRO CALL
Explanation :
Verification of no system errors and assertion that the result table contains exactly 15 observations.
Copied!
1%mp_assert(
2 iftrue=(
3 %str(&syscc)=%str(0)
4 ),
5 desc=ensure no errors,
6 outds=work.test_results
7)
8 
9%mp_assertdsobs(work.final,
10 desc=Has 15 records,
11 test=EQUALS 15,
12 outds=work.test_results
13)
4 Code Block
DATA STEP Data
Explanation :
Creation of a reference table to validate that the 'tgtvar_type' column of the final table contains only the expected values via mp_assertcolvals.
Copied!
1DATA work.check;
2 LENGTH val $10;
3 DO val='C','N';
4 OUTPUT;
5 END;
6RUN;
7%mp_assertcolvals(work.final.tgtvar_type,
8 checkvals=work.check.val,
9 desc=All values have a match,
10 test=ALLVALS
11)
5 Code Block
DATA STEP Data
Explanation :
Test scenario 'No changes': verifies that comparing identical tables produces an empty differences table (0 observations).
Copied!
1/* Test for when there are no actual changes */
2DATA work.orig work.deleted work.changed work.appended;
3 SET sashelp.class;
4 OUTPUT work.orig;
5RUN;
6%mp_storediffs(sashelp.class,work.orig,NAME
7 ,delds=work.deleted
8 ,modds=work.changed
9 ,appds=work.appended
10 ,outds=work.final2
11 ,mdebug=1
12)
13%mp_assertdsobs(work.final2,
14 desc=No changes produces 0 records,
15 test=EQUALS 0,
16 outds=work.test_results
17)
6 Code Block
DATA STEP Data
Explanation :
Test scenario 'Deletions only': simulates the deletion of rows and verifies that the number of detected differences matches expectations (70 records).
Copied!
1/* Test for deletes only */
2DATA work.orig work.deleted work.changed work.appended;
3 SET sashelp.class;
4 OUTPUT work.orig;
5 IF _n_>5 THEN OUTPUT work.deleted;
6RUN;
7 
8%mp_storediffs(sashelp.class,work.orig,NAME
9 ,delds=work.deleted
10 ,modds=work.changed
11 ,appds=work.appended
12 ,outds=work.final3
13 ,mdebug=1
14)
15%mp_assertdsobs(work.final3,
16 desc=Delete has 70 records,
17 test=EQUALS 70,
18 outds=work.test_results
19)
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.