ProUnit
 

Developing your Tests with Database and N-Tiers support

What can I do with database?

ProUnit lets you easily handle data with database. The goal is to be able to control database records generated by a program according to specific input data.

For instance, ProUnit can work like this:

  • import records from file "initial_data.d" to a physical table (overwriting existing records)
  • run a procedure to test (that may change database records)
  • extract some records from a physical table to a temp-table (A)
  • load records from file "expected_result.d" to a temp-table (B)
  • compare temp-tables (A and B)


In Client / AppServer environment with full N tiers (client not connected to any database), ProUnit can work like this:

  • connect same databases on client than AppServer has connected to
  • import records from file "initial_data.d" to physical table (overwriting existing records)
  • disconnect databases from client
  • run a procedure to test on AppServer (this procedure may change some database records)
  • connect same database on client than AppServer has connected to
  • extract some records from a physical table to a temp-table (A)
  • load records from file "expected_result.d" to a temp-table (B)
  • compare temp-tables (A and B)
  • disconnect databases from client

Source code sample


DEFINE VARIABLE hTTExpected   AS HANDLE.
DEFINE VARIABLE hTTGot        AS HANDLE.


/*------------------------------------------------------------------------------
Some code to initialize the environment or database before running the test.
------------------------------------------------------------------------------*/
PROCEDURE initialize:
END.


/*------------------------------------------------------------------------------
Some code run before every test to reset internal states, if needed.
------------------------------------------------------------------------------*/
PROCEDURE setUp:
END.


/*------------------------------------------------------------------------------
Some code run after a test to restore, log or something else.
------------------------------------------------------------------------------*/
PROCEDURE tearDown:
END.


/*------------------------------------------------------------------------------
Dispose everything, free resource, close files, disconnect databases, etc.
------------------------------------------------------------------------------*/
PROCEDURE dispose:
END.


/*------------------------------------------------------------------------------
Full database test
------------------------------------------------------------------------------*/
PROCEDURE testDatabase:
    /* Connect to database, inject initial data, then disconnect from database */
    RUN LocalConnectAppServerDB(INPUT hAppServer, "myDBLogicalName") NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    RUN LoadDFileToDBTable(INPUT "c:\initial.d", INPUT "MY_TABLE", INPUT NO, INPUT YES) NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    RUN LocalDisconnectAppServerDB NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.



    /* Run our program to be tested. This program may change MY_TABLE records. */
    RUN my_test.p ON hAppServer (INPUT ....) NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.



    /* Connect to database, gather changed data, then disconnect from database */
    RUN LocalConnectAppServerDB(INPUT hAppServer, "myDBLogicalName") NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    /* Create temp-tables like physical table */
    RUN CreateTTLikePhysicalDBTable(INPUT "MY_TABLE", INPUT "a_name_for_your_tt", OUTPUT hTTExpected) NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.
    RUN CreateTTLikePhysicalDBTable(INPUT "MY_TABLE", INPUT "another_name_for_your_tt", OUTPUT hTTGot) NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    RUN ExportDBTableToTempTableViaQuery(INPUT "MY_TABLE", INPUT hTTGot, INPUT YES, INPUT NO, INPUT "where somefield = 12") NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    RUN LoadDFileToTempTable(INPUT "c:\expected.d", INPUT hTTExpected, INPUT YES, INPUT NO) NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.

    RUN assertEqualsTT(hTTExpected, hTTGot, "").

    RUN DeleteTTByHandle(INPUT hTTExpected).
    RUN DeleteTTByHandle(INPUT hTTGot).

    RUN LocalDisconnectAppServerDB NO-ERROR.
    IF ERROR-STATUS:ERROR THEN
        MESSAGE RETURN-VALUE SKIP ERROR-STATUS:GET-MESSAGE(1) VIEW-AS ALERT-BOX WARNING.
END.

        

Procedures

During your test you can use all of these procedures:

ProUnit Database Handling Methods
Method Description Example
LoadDFileToTempTable Dynamically load data from .d file to a temp-table.
Parameters:
  1. The .d file name
  2. Handle to the target temp-table
  3. Empty the table before injecting .d data?
  4. Overwrite records if already existing?
RUN LoadDFileToTempTable(INPUT "c:\expected.d", INPUT hTTExpected,    
INPUT YES, INPUT NO) NO-ERROR.
LoadDFileToDBTable Dynamically load data from .d file to a physical table.
Parameters:
  1. The .d file name
  2. The name of target table
  3. Empty the table before injecting .d data?
  4. Overwrite records if already existing?
RUN LoadDFileToDBTable(INPUT "c:\expected.d", INPUT "MY_TABLE",    
INPUT YES, INPUT NO) NO-ERROR.
LoadDFilesToDBTablesFromDirectory Dynamically load a set of .d files to many physical table.
Parameters:
  1. The directory path name
  2. Empty the table before injecting .d data?
  3. Overwrite records if already existing?
RUN LoadDFilesToDBTablesFromDirectory(INPUT "c:\data", INPUT NO,    
INPUT YES) NO-ERROR.
ExportDBTableToTempTableViaQuery Dynamically export data from a physical table to a temp-table, according to a query.
Parameters:
  1. The name of target table - be aware of database logical name identifier
  2. The target temp-table
  3. Where-clause for selecting records
RUN ExportDBTableToTempTableViaQuery(INPUT "MY_TABLE", INPUT hTTGot,    
INPUT YES, INPUT NO, INPUT "where somefield = 12") NO-ERROR.
CreateTTLikePhysicalDBTable Create a dynamic temp-table like a physical database table.
Parameters:
  1. Physical table name
  2. Target temp-table name to be used in queries
  3. Handle receiving the TT handle
RUN CreateTTLikePhysicalDBTable(INPUT "MY_TABLE", INPUT "a_name_for_your_tt",    
OUTPUT hTTExpected) NO-ERROR.
DeleteTTByHandle Delete a dynamic temp-table from its handle.
Parameters:
  1. Handle to TT to be destroyed
RUN DeleteTTByHandle(INPUT hTTExpected).
DeleteDBTableRecordsViaQuery Delete records from physical table according to a query.
Parameters:
  1. Physical table name
  2. Where-clause for selecting records
RUN DeleteDBTableRecordsViaQuery(INPUT "MY_TABLE",    
INPUT "where something = 17") NO-ERROR.
LocalConnectAppServerDB Query AppServer for connected databases and connect some of them to current session
Parameters:
  1. Handle to AppServer
  2. List of logical database names to connect (search is performed using AppServer logical names and aliases)
RUN LocalConnectAppServerDB(INPUT hAppServer, "myDBLogicalName") NO-ERROR.
LocalDisconnectAppServerDB Disconnect previously connected databases got from AppServer.
RUN LocalDisconnectAppServerDB NO-ERROR.

Architecture and integration to N-Tiers Progress Sessions

In this part, you can see a diagram detailing ProUnit integration to your own framework (if any) in a N-Tiers architecture.

Here are details of steps:

  1. AppServer session starts
  2. AppServer session connects to databases
  3. Client session starts
  4. Client session connects to AppServer session
  5. Your ProUnit test program use ProUnit framework to ask AppServer for database connection list and connect to some databases using same parameters as AppServer did
  6. Once the ProUnit framework is connected to database, your test program acces it and disconnectusing ProUnit framework