Loading...
Searching...
No Matches
mpe_targetloader.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mpe_targetloader macro - REPLACE loadtype
4 @details Covers the REPLACE branch of mpe_targetloader.sas:
5
6 * LOADTARGET=NO (diff screen preparation) - all staged records classed as
7 new, all existing records classed as deleted, target table unchanged
8 * LOADTARGET=YES (actual load) - target table fully replaced by the
9 staging table, delete flag column dropped, processed column stamped,
10 per-record delete flags ignored (everything is loaded)
11
12 A dedicated DCTEST.DC_REPLACE table is registered in MPE_TABLES (and the
13 registration removed again in cleanup). The DCTEST library is BASE engine,
14 so the CAS-specific branch (deleteRows truncation / varchar casting) is not
15 exercised here.
16
17 <h4> SAS Macros </h4>
18 @li mp_assert.sas
19 @li mp_assertdsobs.sas
20 @li mp_assertscope.sas
21 @li mpe_targetloader.sas
22
23 @author 4GL Apps Ltd
24 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
25 and may not be re-distributed or re-sold without the express permission of
26 4GL Apps Ltd.
27**/
28
29%let syscc=0;
30
31/**
32 * Prep - physical target table and MPE_TABLES registration
33 */
34data dctest.dc_replace;
35 length pk $8 val $20;
36 pk='OLD1'; val='oldvalue1'; processed_dttm=0; output;
37 pk='OLD2'; val='oldvalue2'; processed_dttm=0; output;
38 format processed_dttm datetime19.;
39run;
40
41proc sql noprint;
42delete from &dc_libref..mpe_tables where libref="DCTEST" and dsn='DC_REPLACE';
43insert into &dc_libref..mpe_tables
44 set tx_from=0
45 ,tx_to='31DEC5999:23:59:59'dt
46 ,libref="DCTEST"
47 ,dsn='DC_REPLACE'
48 ,buskey='PK'
49 ,loadtype='REPLACE'
50 ,var_processed='PROCESSED_DTTM'
51 ,num_of_approvals_required=1;
52quit;
53
54/* staging table, as it would arrive from the approval package */
55data work.staging_ds;
56 length pk $8 val $20 _____DELETE__THIS__RECORD_____ $3;
57 pk='NEW1'; val='newvalue1'; _____DELETE__THIS__RECORD_____='No'; output;
58 pk='NEW2'; val='newvalue2'; _____DELETE__THIS__RECORD_____='Yes'; output;
59 pk='NEW3'; val='newvalue3'; _____DELETE__THIS__RECORD_____='No'; output;
60run;
61
62/**
63 * Test 1 - LOADTARGET=NO builds the diff tables without touching the target
64 */
65%mp_assertscope(SNAPSHOT)
66%mpe_targetloader(libds=DCTEST.DC_REPLACE
67 ,etlsource=mpe_targetloader.test
68 ,STAGING_DS=STAGING_DS
69 ,LOADTARGET=NO
70 ,dclib=&dc_libref
71 ,dc_dttmtfmt=&dc_dttmtfmt.
72)
73%mp_assertscope(COMPARE,
74 desc=%str(Test 1 - checking macro variables against previous snapshot)
75)
76
77%mp_assert(iftrue=(&syscc=0),
78 desc=%str(Test 1 - REPLACE LOADTARGET=NO completed without errors),
79 outds=work.test_results
80)
81
82%mp_assertdsobs(work.outds_add,
83 desc=%str(Test 1 - all staged records classed as new),
84 test=EQUALS 3,
85 outds=work.test_results
86)
87%mp_assertdsobs(work.outds_del,
88 desc=%str(Test 1 - all existing records classed as deleted),
89 test=EQUALS 2,
90 outds=work.test_results
91)
92%mp_assertdsobs(work.outds_mod,
93 desc=%str(Test 1 - no records classed as modified),
94 test=EQUALS 0,
95 outds=work.test_results
96)
97%mp_assertdsobs(dctest.dc_replace,
98 desc=%str(Test 1 - target table not modified),
99 test=EQUALS 2,
100 outds=work.test_results
101)
102
103/**
104 * Test 2 - LOADTARGET=YES replaces the target table with the staged data
105 */
106%mp_assertscope(SNAPSHOT)
107%mpe_targetloader(libds=DCTEST.DC_REPLACE
108 ,etlsource=mpe_targetloader.test
109 ,STAGING_DS=STAGING_DS
110 ,LOADTARGET=YES
111 ,dclib=&dc_libref
112 ,dc_dttmtfmt=&dc_dttmtfmt.
113)
114%mp_assertscope(COMPARE,
115 desc=%str(Test 2 - checking macro variables against previous snapshot)
116)
117
118%mp_assert(iftrue=(&syscc=0),
119 desc=%str(Test 2 - REPLACE LOADTARGET=YES completed without errors),
120 outds=work.test_results
121)
122
123%mp_assertdsobs(dctest.dc_replace,
124 desc=%str(Test 2 - target row count matches staging table),
125 test=EQUALS 3,
126 outds=work.test_results
127)
128
129proc sql noprint;
130select count(*) into: oldrows
131 from dctest.dc_replace where pk in ('OLD1','OLD2');
132select count(*) into: newrows
133 from dctest.dc_replace where pk in ('NEW1','NEW2','NEW3');
134select count(*) into: delcol from dictionary.columns
135 where libname='DCTEST' and memname='DC_REPLACE'
136 and upcase(name)='_____DELETE__THIS__RECORD_____';
137select count(*) into: notstamped
138 from dctest.dc_replace where missing(processed_dttm) or processed_dttm=0;
139quit;
140
141%mp_assert(iftrue=(&oldrows=0),
142 desc=%str(Test 2 - pre-existing records removed from target),
143 outds=work.test_results
144)
145%mp_assert(iftrue=(&newrows=3),
146 desc=%str(Test 2 - all staged records loaded, delete flags ignored),
147 outds=work.test_results
148)
149%mp_assert(iftrue=(&delcol=0),
150 desc=%str(Test 2 - delete flag column not loaded to target),
151 outds=work.test_results
152)
153%mp_assert(iftrue=(&notstamped=0),
154 desc=%str(Test 2 - processed_dttm stamped on every loaded record),
155 outds=work.test_results
156)
157
158/**
159 * Cleanup - remove all persistent state created by this test, so that a
160 * subsequent run starts from the same position (including a run that
161 * previously failed partway through)
162 */
163proc sql noprint;
164/* REPLACE table registration */
165delete from &dc_libref..mpe_tables where libref="DCTEST" and dsn='DC_REPLACE';
166/* lock record (may be left as LOCKED after an aborted run) */
167delete from &dc_libref..mpe_lockanytable
168 where lock_lib="DCTEST" and lock_ds="DC_REPLACE";
169quit;
170
171/* physical target table */
172proc datasets lib=dctest nolist;
173 delete dc_replace;
174run;
175quit;
176
177/* assertion macro variables */
178%symdel oldrows newrows delcol notstamped;