How to work with collections using axunit
Overview
Collections (basic classes) are used to establish links between standalone entities. These collections link two entities to form an entity relationship.
A data set in 'AXUNIT' tests is generally created, which will in turn create a valid entry at the end of every successful run.
Adding line(s) to a collection
To add a single line to a collection, the syntax is as follows:
Local Integer I
I=fmet RECORD.ADDLINE ("XX011ARL",[V]CST_ALASTPOS)
RECORD.XX011ARL (I).ACT_CODE = "E001"
RECORD.XX011ARL(I).ROLE_CODE = "RO001"
Where:
RECORD
is the instance of XX011ACT table.ADDLINE
is the method.“XX011ARL”
is the collection name.[V] CST_ALASTPOS
is the last available position in the collection.- [
RECORD
.collectionName (subscript).fieldName] - Adding values to the fields in the collection table “XX011ARL”.
To add more than one line to a collection, the syntax is as follows:
Local Integer I For I = 1 to 5
I=fmet RECORD.ADDLINE ("XX011ARL",[V]CST_ALASTPOS) RECORD.XX011ARL (I).ACT_CODE = "E001"
RECORD.XX011ARL (I).ROLE_CODE = "RO001" `
Next I
The only addition to the code is to surround the previous code in a “for loop” to enter multiple lines. Example:
Subprog XX011ACT_INSERT
Local Instance RECORD Using C_XX011ACT
RECORD = NewInstance C_XX011ACT AllocGroup null
Local Integer OK,I
OK = fmet RECORD.AINIT()
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
RECORD.CODE = "E001"
RECORD.DESCRIPTION = "Learning Activity"
RECORD.STARTDATE = [09/01/2013]
RECORD.ENDDATE = [09/05/2013]
Call LOG_LINE("Entering roles") From AXUNIT
I=fmet RECORD.ADDLINE("XX011ARL",[V]CST_ALASTPOS)
Call LOG_LINE("Result of ADDLINE I="+num$(I)) From AXUNIT
Call CHECK_TRUE(I>=0) From AXUNIT
RECORD.XX011ARL(I).ACT_CODE = "E001"
RECORD.XX011ARL(I).ROLE_CODE = "RO001"
OK = fmet RECORD.AINSERT
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
FreeGroup RECORD
End
Reading line(s) from a collection
To read one or more lines from a collection, the syntax is as follows:
For I=1 To maxtab(RECORD.XX011ARL)
If RECORD.XX011ARL (I) <>null
Call CHECK_EQUAL (RECORD.XX011ARL (I).ACT_CODE,"E001") From AXUNIT
Call CHECK_EQUAL (RECORD.XX011ARL (I).ROLE_CODE,"RO001") From AXUNIT
Call CHECK_EQUAL (RECORD.XX011ARL (I).ROLE_DESC, "Junior Developer C++") From AXUNIT
Endif
Next I
Where:
RECORD
is the instance of XX011ACT table.“XX011ARL”
is the collection name.- We encapsulate the line and field reads by a “For loop” to read multiple lines. If the collection had one line then it will loop only once.
- [maxtab (
RECORD.XX011ARL
)] will calculate the number of lines in the Collection. RECORD.XX011ARL
(I) <> null will check whether the current line is not Null.- Use of
CHECK_EQUAL
command to validate successful reads. - [
RECORD.XX011ARL
(I).ROLE_CODE,"RO001"] checks the value “RO001” against the field name 'ROLE_CODE' in "XX011ARL" collection.
Subprog XX011ACT_AREAD_AFTER_INSERT
Local Instance RECORD Using C_XX011ACT
RECORD = NewInstance C_XX011ACT AllocGroup null
Local Integer OK
OK = fmet RECORD.AREAD("E001")
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
Call LOG_LINE("Verify Loan Record can be Read") From AXUNIT
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
Call CHECK_EQUAL(RECORD.CODE,"E001") From AXUNIT
Call CHECK_EQUAL(RECORD.DESCRIPTION,"Learning Activity") From AXUNIT
Call CHECK_EQUAL(RECORD.STARTDATE, [09/01/2013]) From AXUNIT
Call CHECK_EQUAL(RECORD.ENDDATE, [09/05/2013]) From AXUNIT
Local Integer I
Call LOG_LINE("Reading collection entries") From AXUNIT
For I=1 To maxtab(RECORD.XX011ARL)
If RECORD.XX011ARL(I)<>null
Call CHECK_EQUAL(RECORD.XX011ARL(I).ACT_CODE,"E001") From AXUNIT
Call CHECK_EQUAL(RECORD.XX011ARL(I).ROLE_CODE,"RO001") From AXUNIT
Call CHECK_EQUAL(RECORD.XX011ARL(I).ROLE_DESC,"Junior Developer C++") From AXUNIT
Endif
Next I
FreeGroup RECORD
End
Deleting line(s) from a collection
To delete a line from a collection, the syntax is as follows:
OK=fmet RECORD.ADELLINE ("XX011ARL", 2)
Where:
RECORD
is the instance of XX011ACT table.ADELLINE
is the method.“XX011ARL”
is the collection name.2
is the position of line in the collection.
Note: 'AXUNIT' expects an integer as the second argument of ADELLINE
. For multiple line deletion, you need to write multiple ADELLINE
statements with integer positions. For example:
OK=fmet RECORD.ADELLINE ("XX011ARL", 1)
OK=fmet RECORD.ADELLINE ("XX011ARL", 2)
Example:
Subprog XX011ACT_UNIQUEENTRY_PASS
Local Instance RECORD Using C_XX011ACT
RECORD = NewInstance C_XX011ACT AllocGroup null
Local Integer OK,I
OK = fmet RECORD.AREAD("E001")
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
Call LOG_LINE("Trying to add a new role") From AXUNIT
I=fmet RECORD.ADDLINE("XX011ARL",[V]CST_ALASTPOS)
Call LOG_LINE("Result of ADDLINE I="+num$(I)) From AXUNIT
Call CHECK_TRUE(I>=0) From AXUNIT
RECORD.XX011ARL(I).ACT_CODE = "E001"
RECORD.XX011ARL(I).ROLE_CODE = "RO002"
#DELETING SINGLE LINE
Call LOG_LINE("Trying to delete the new role") From AXUNIT
OK=fmet RECORD.ADELLINE("XX011ARL",2)
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
OK = fmet RECORD.AUPDATE
Call CHECK_EQUAL(OK,[V]CST_AOK) From AXUNIT
FreeGroup RECORD
End