Rocket U2 | UniVerse & UniData

 View Only
  • 1.  Simple unit test in XDEMO account

    Posted 22 days ago

    Hi Team, 

    I have written a sample program to update the data file.

    PROGRAM UPDATE.DATA.DRV
    * Declare variables
        CUSTOMER.ID = ""
        NOTE = ""
        RECORD = ""

    * Get the CUSTOMER ID (2nd word from the sentence)
        CUSTOMER.ID = FIELD(@SENTENCE, " ", 2)

    * Get the NOTE (3rd word and onwards from the sentence)
        NOTE = FIELD(@SENTENCE, " ", 3, LEN(@SENTENCE))

    * Strip double quotes if present
        IF NOTE[1,1] = '"' THEN NOTE = NOTE[2,LEN(NOTE) - 2]

    * Open the CUSTOMER file
        OPEN "CUSTOMER" TO CUSTOMER.FILE ELSE
            PRINT "ERROR: Unable to open CUSTOMER data file."
            RETURN
        END

    * Read the record for the given CUSTOMER ID
        READ RECORD FROM CUSTOMER.FILE, CUSTOMER.ID ELSE
            PRINT "ERROR: No record found for CUSTOMER ID: ": CUSTOMER.ID
            RETURN
        END

    * Update the NOTE attribute (assuming the note is in attribute 2)
        RECORD<2> = NOTE

    * Write the updated record back to the file
        WRITE RECORD ON CUSTOMER.FILE, CUSTOMER.ID

        PRINT "CUSTOMER ID ": CUSTOMER.ID: " updated successfully with note: ": NOTE
    RETURN
    END
    I am currently trying to create the unit test case for this in the XDEMO account, but SYSTEM(0) is always returning 0. Is there any way to capture the PROGRAM UPDATE.DATA.DRV message in the unit test file? I would appreciate it if you could help with this.


    SHOULD.NOT.UPDATE.NOTE.IF.NOT.FOUND.CUSTOMER.ID:
    **********************************  
        * Test 2: CUSTOMER ID not found
         PRINT "Test 2: Attempt to update non-existent CUSTOMER ID"
        @SENTENCE = 'UPDATE.DATA.DRV NONEXISTENT "Non-existent ID test"'
     
        CALL UPDATE.DATA.DRV
         EXPECTED.OUTPUT = "ERROR: No record found for CUSTOMER ID: NONEXISTENT"
        * Assuming that the error message is in SYSTEM(0)
        ACTUAL.OUTPUT = SYSTEM(0)
     
        * Compare expected and actual results
        IF ACTUAL.OUTPUT = EXPECTED.OUTPUT THEN
            PRINT "PASS: Test 2 passed successfully."
        END ELSE
            PRINT "FAIL: Test 2 failed."
            PRINT "Expected: ": EXPECTED.OUTPUT
            PRINT "Actual: ": ACTUAL.OUTPUT
            TEST.PASSED = 0
        END
    PRINT "***********************************************************************************************************"
    RETURN


    ------------------------------
    Gnanaseelan gnanapirakasam
    SSE
    Rocket Forum Shared Account
    ------------------------------


  • 2.  RE: Simple unit test in XDEMO account

    Posted 21 days ago

    Hi,

    I have no idea what you expect to get back from a call to SYSTEM(0). I can find no mention of this key in the documentation.

    On to the more general question of how you would test this, I would convert your program to a subroutine with a declaration of:

      SUBROUTINE UPDATE.DATA.DRV(AMSG)

    Then, replace your PRINT statements with:  AMSG = "Return text"

    In your TEST program, you would call the subroutine:  CALL UPDATE.DATA.DRV(AMSG)

    And test the output with:  IF (AMSG = EXPECTED.OUTPUT) THEN ...

    There are other ways - like calling your update program as a PHANTOM and then parsing the PRINT output from the &PH& file ... but structuring it as a subroutine is much easier.

    HTH,

    Brian



    ------------------------------
    Brian Speirs
    Wellington NZ
    ------------------------------



  • 3.  RE: Simple unit test in XDEMO account

    Posted 17 days ago

    Thanks @Brian Speirs. I followed your guidance, and it's now working perfectly. I really appreciate your help-thank you!



    ------------------------------
    Gnanaseelan gnanapirakasam
    SSE
    Rocket Forum Shared Account
    ------------------------------