Referencing to a Database
Query Generators can be used to query column data from a database when generating test data. Several Query Generators are available, and a complete list can be viewed here.
This lesson will show how to reference the city, state, and zip code values for the first ten records within a user database table. For this example, we will be showing how to reference a MySQL database. However, the same steps can be used for other databases as well.
Prerequisites:
Steps in Lesson 1 of this Training Module must be completed before completing the steps outlined in this lesson:
- Download the appropriate JDBC driver for the database.
- Create a database connection properties file.
- Add or update the resource.jdbc.directory Organization Resource.
Initial Setup
For this example, a User Domain has been added with six Attributes, and the loopCount has been set to 10. A DelimitedFileReceiver and a Scenario have been added to the Domain.
Database Table and Data
For this example, a User Table will be referenced within a user_db Database. The city, state, and zip code for the first ten records will be referenced within the generated data.
Step 1: Assign the QueryBeforeLoopV2Gen Generator to the city Attribute
Within the Domain Dashboard, select the city Attribute.
Enter QueryBeforeLoopV2Gen into the Quick Generator Replacement Menu. Then, click the Replace button.
Change the setLoop Parameter to “True” and enter the resourceName.
The resourceName is the name of the config.properties file. For this example, the file has been named userdb.properties.
Next, enter the query for the query Parameter. Additional Parameters can be changed as needed for the QueryBeforeLoopV2Gen Generator. Remember to click the Save Generator button.
Step 2: Assign the QueryBeforeLoopV2Gen Generator to the state Attribute
The steps are the same for assigning the QueryBeforeLoopV2Gen Generator to the state Attribute. The query for this Attribute is:
select state from user limit 10
Step 3: Assign the QueryBeforeLoopV2Gen Generator to the zipCode Attribute
The steps are the same for assigning the QueryBeforeLoopV2Gen Generator to the zipCode Attribute. The query for this Attribute is:
select zip_code from user limit 10
Step 4: Download the Scenario
Next, the user will need to download the Scenario to their local machine. This can be done by clicking on the Download (Cloud) icon within the Project Dashboard or Domain Dashboard.
Step 5: Run the Scenario
On the user’s local machine, open a Command Prompt or Terminal window.
Then make sure the directory matches the path entered for the resource.home.directory within the GenRocket web platform.
Next, enter genrocket -r <ScenarioName.grs>. Replace <ScenarioName.grs> with the actual Scenario File Name. Then press the Enter key.
Note: The genrocket -r command can also be used to run a Scenario Chain or Scenario Chain Set.
Sample Output Data
For this example, the city, state, and zip code for the first ten records in the User database table are being referenced and included within the generated test data.
The image below is the data within the User Table of the user_db Database:
The image below is the generated output file. The city, state, and zip code match each record in the database table.