Published on :
Macro CREATION_INTERNE

Table Locking Mechanism Test via Macros

This code is also available in: Deutsch Español Français
Awaiting validation
Attention : This code requires administrator privileges.
The script runs a series of tests to validate the behavior of the `%mp_lockanytable` macro. It starts by creating a control table (`work.controller`) and validating its structure. Then, it creates a test table (`tmp.sometable`), locks it, and verifies that the 'LOCKED' status is correctly recorded. It then unlocks the table and ensures that the status changes to 'UNLOCKED'. An error case is also tested by attempting to unlock a non-existent table, ensuring that the program does not stop. Finally, it checks for macro variable leakage after calling the main macro.
Data Analysis

Type : CREATION_INTERNE


All data used is created by the script itself. This includes the table to be locked (`tmp.sometable`), the lock control table (`work.controller`), and several small verification tables (`work.checkds*`) used for assertions.

1 Code Block
Macro Data
Explanation :
The `%mp_coretable` macro creates the `work.controller` table intended to track lock status. The `%mp_assertcols` macro is then used to validate that this control table contains all required columns.
Copied!
1%mp_coretable(LOCKTABLE,libds=work.controller)
2 
3%mp_assertcols(work.controller,
4 cols=lock_status_cd lock_lib lock_ds lock_user_nm lock_ref lock_pid
5 lock_start_dttm lock_end_dttm,
6 test=ALL,
7 desc=check all control columns exist
8)
2 Code Block
DATA STEP Data
Explanation :
This block enables on-the-fly directory creation, allocates a `tmp` library in the session's temporary folder, and then creates a `tmp.sometable` table for testing purposes.
Copied!
1options dlcreatedir;
2LIBNAME tmp "%sysfunc(pathname(work))/tmp";
3DATA tmp.sometable;
4 x=1;
5RUN;
3 Code Block
Macro Data
Explanation :
The `%mp_lockanytable` macro is called to lock the `tmp.sometable` table. Validation tables are created (`work.checkds1`, `work.checkds2`) and the `%mp_assertcolvals` macro verifies that the table name and 'LOCKED' status are correctly recorded in the control table.
Copied!
1%mp_lockanytable(LOCK,lib=tmp,ds=sometable,ref=This Ref, ctl_ds=work.controller)
2 
3DATA work.checkds1;
4 checkval='SOMETABLE';
5RUN;
6%mp_assertcolvals(work.controller.lock_ds,
7 checkvals=work.checkds1.checkval,
8 desc=TABLE is captured in lock,
9 test=ANYVAL
10)
11 
12DATA work.checkds2;
13 checkval='LOCKED';
14RUN;
15%mp_assertcolvals(work.controller.lock_status_cd,
16 checkvals=work.checkds2.checkval,
17 desc=code is captured in lock,
18 test=ANYVAL
19)
4 Code Block
Macro Data
Explanation :
The `tmp.sometable` table is unlocked via `%mp_lockanytable`. The script then uses `%mp_assertcolvals` to verify that the status in the control table is updated to 'UNLOCKED'.
Copied!
1%mp_lockanytable(UNLOCK,lib=tmp,ds=sometable,ref=bye, ctl_ds=work.controller)
2 
3DATA work.checkds3;
4 checkval='UNLOCKED';
5RUN;
6%mp_assertcolvals(work.controller.lock_status_cd,
7 checkvals=work.checkds3.checkval,
8 desc=Ref is captured in unlock,
9 test=ANYVAL
10)
5 Code Block
Macro
Explanation :
This block tests an error case by attempting to unlock a non-existent table. The `%mp_assert` macro validates that this operation executes without fatal error, by checking that the system return code `&syscc` remains 0.
Copied!
1%mp_lockanytable(UNLOCK,lib=no,ds=doesnotexist,ref=bye, ctl_ds=work.controller)
2 
3%mp_assert(
4 iftrue=(&syscc=0),
5 desc=Ability to unlock a TABLE that was never locked,
6 outds=work.test_results
7)
6 Code Block
Macro
Explanation :
This block tests for macro variable 'leakage'. `%mp_assertscope(SNAPSHOT)` takes a snapshot of existing macro variables, `%mp_lockanytable` is executed, then `%mp_assertscope(COMPARE)` compares the new state to the previous one to ensure no local macro variables have been created globally.
Copied!
1%mp_assertscope(SNAPSHOT)
2%mp_lockanytable(LOCK,lib=tmp,ds=testscope,ref=This Ref, ctl_ds=work.controller)
3%mp_assertscope(COMPARE)
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.
Copyright Info : Copyright 2010-2023 HMS Analytical Software GmbH, http://www.analytical-software.de; Copyright © 2021, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. SPDX-License-Identifier: Apache-2.0