Testing and Debugging Procedures using SQL Developer 3.1

Testing and Debugging Procedures using SQL Developer 3.1

http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/devdays2012/mod2_sqldev/mod2_sqldev.html 

 

 

Overview

    Purpose

    This tutorial covers how to execute a DDL script, review changes to the database objects, create, execute, test and debug a procedure.

    Time to Complete

    Approximately 60 minutes

    Introduction

    Oracle SQL Developer is a free and fully supported graphical tool that enhances productivity and simplifies database development tasks. Using SQL Developer, users can browse, edit and create database objects, run SQL statements, edit and debug PL/SQL statements, build PL/SQL unit tests, run reports, and place files under version control.

    In this tutorial, you use SQL Developer Release 3.1 to examine various tasks.

    Prerequisites

    Before starting this tutorial, you should:

    • Have installed Oracle SQL Developer Release 3.1
    • Have access to an Oracle Database 11g database that has the sample schema installed.
    • Grant HR user DEBUG CONNECT SESSION and DEBUG ANY PROCEDURE privileges.
    • Performed the Re-engineering Your Database Using Data Modeler 3.1 tutorial.
    • Downloaded and unzipped the files.zip into your working directory.
 

Creating a Database Connection

    In this topic, you will create a connection to the HR schema in SQL Developer.

    Double-click the SQL Developer 3.1 icon from the Desktop.

    The first time SQL Developer is open, you may receive a Tip of the Day dialog. You can deselect the Show tips at startup checkbox to turn it off and then click the Close icon in the upper right corner of the dialog.

    In the Connections tab, right-click Connections and select New Connection.

    A New / Update Database Connection dialog opens. Enter the connection details as follows and click Test.

    Connection Name: hr_orcl
    User Name: hr
    Password: oracle (Select the Save Password checkbox) 
    SID: orcl (or your own SID)

    The status of your test is 'Success'. Click Connect in the New/ Update Database Connection dialog to create the connection.

    Expand the hr_orcl connection. Notice all the object types. Expand Tables. In the next section, you examine the objects currently in the hr schema.

 

Reviewing Existing Objects in the HR Schema

    In this topic, you review the existing objects in the hr schema.

    Expand the EMPLOYEES table. Notice that the column definitions are listed.

    If you performed the previous tutorial on Oracle SQL Developer Data Modeler, note that the COST_CENTER column you added to the DEPARTMENTS table in the relational model does not currently exist. Also the PROJECTS and TASKS tables do not exist in the schema currently. In the next topic, you run the script to modify the hr schema.

    Click the DEPARTMENTS table in the navigator.

    Notice that the information in the EMPLOYEES tab was replaced by the DEPARTMENTS table information. If you want the table information in the tab to remain frozen, select the Pin icon to Freeze the pane.

    Then click the EMPLOYEES table again in the navigator.

    Notice this time you have 2 tabs, one for each of the tables because the DEPARTMENTS table pane is frozen.

    You can see the data in the EMPLOYEES table. Click the Data subtab.

    The data in the EMPLOYEES table is displayed. You can also enter a SQL statement in the SQL Worksheet. Click the hr_orcl tab.

    Enter the following SQL statement and select the Execute SQL Statement icon.

    select * from employees 
    where job_id like '%SA%'

    The Query Results are displayed. In the next topic, you run the script you generated in the previous tutorial on Data Modeler.

 

Executing a DDL Script

    In this topic, you execute the DDL script you generated in the Data Modeler tutorial. If you did not complete the previous tutorial, you can access the solution in the /home/oracle/Desktop/solutions/dm_lab directory in the VM.

    Select File > Open.

    Select the Desktop icon, select the file you created in the previous tutorial or navigate to the /home/oracle/Desktop/solutions/dm_lab directory, select the dm_mods.sql file and click Open.

    This SQL file contains all the DDL to change the HR Schema so that it is synchronized with the model changes you made in the previous tutorial. When you execute this script, the PROJECTS and TASKS tables will be created, and the new COST_CENTER column will be added to the DEPARTMENTS table. Scroll down to review the DDL.

    When done reviewing, click the Run Script icon.

    Select the hr_orcl connection from the list and click OK.

    All the statements in the DDL script ran successfully.

    Click the Refresh icon to refresh the list of tables.

    Notice that the new tables for PROJECTS and TASKS are contained in the list. Expand the DEPARTMENTSPROJECTS and TASKS table nodes and review the results. In the next topic, you create a procedure and run it.

 

Creating Table APIs

    In this topic, you create API packages for the DEPARTMENTS table. Table APIs allows you to make PL/SQL calls to insert, update, delete records in your application without writing or managing raw INSERT, UPDATE, and DELETE statements.

    In the navigator, right click the DEPARTMENTS table, select Table then Generate Table API...

    The PL/SQL code to create the API package for the DEPARTMENTS table is displayed. Review the code. You see there is an insert (ins) procedure as well as procedures for delete and update. Click the Run Scripticon to create the package.

    The package was compiled successfully.

 

Creating, Executing and Debugging a Procedure

    In this topic, you create, execute and debug a procedure that determines the commission any employee receives based on a sales amount and the employees commission percentage.

    A script with the procedure has already been created so you want to open the file. Select File > Open.

    Navigate to the /home/oracle/Desktop/solutions/sqldev_lab directory, select the proc.sql file and click Open.

    Click the Run Script icon to create the AWARD_BONUS procedure.

    Select the hr_orcl connection and click OK.

    The procedure was created and compiled with a warning. To see the warning, expand Procedures in the navigator.

    Select AWARD_BONUS from the list of procedures. Notice the warning icon which indicates that the procedure did not compile successfully.

    Select the Compile icon.

    The error indicates that Line 15 has the issue. You can turn on line numbers. Right click in the white space before the code and select Toggle Line Numbers.

    The reason the error occurred is because you are missing a ';' after the RAISE comm_missing code on line 14. Enter the ';' and select the Compile icon again.

    This time the procedure compiled successfully. Select the Run icon to run the procedure.

    The Run PL/SQL dialog window appears. Notice that the values for EMP_ID and SALES_AMT are currently set to null.

    Change NULL to 149 for EMP_ID and 2000 for SALES_AMT and click OK.

    Note that the procedure ran successfully and the value for salary was changed. To see how debug works, you want to create a breakpoint. Click the line number 8.

    The red icon indicates that a breakpoint was created so when you run the procedure in debug mode, the execution will break when it gets to line 8. Click the Debug icon.

    Click OK to accept the same input values as before.

    The debugger is running and has stopped at line 8. Click the Data tab.

    The current values for each variable is displayed. Click the Debugging tab.

    Click the Step Over icon to move to the next statement in the procedure.

    The SELECT statement was executed. You want to check the values for l_salary and l_commission. Click the Data tab again.

    Notice the values for l_salary and l_commission have changed to the existing values in the database. Click the Debugging tab.

    Click the Step Over icon again to move to the next statement.

    Notice that the debugger moved to the next statement in the procedure. You want to run the rest of the procedure, click the Resume icon.

    The procedure execution completed. Note the value for salary. In the next topic, you create a test repository so that you can create and run a unit test.

 

Creating a Unit Test Repository

    In this topic, you create a database user called UNIT_TEST_REPOS. You create this user to hold the Unit Testing Repository data. You will then create the repository in the schema of the user that you created. 

    Create a connection for the SYS User. Right-click Connections and select New Connection.

    Enter the following information and click Connect.

    Connection Name: sys_orcl
    Username: sys
    Password: oracle
    Select Save Password checkbox
    Role: SYSDBA
    SID: orcl

    Your connection was created successfully. Collapse the hr_orcl connection. Expand the sys_orcl connection and right-click Other Users and select Create User.

    Enter the following information and select the Roles tab.

    Username: unit_test_repos
    Password: oracle
    Default Tablespace: USERS
    Temporary Tablespace: TEMP

    Select the Connect and Resource roles and click Apply.

    The unit_test_repos user was created successfully. Click Close.

    You now need to create a connection to the unit_test_repos user. This user will hold the unit testing repository data. Right-click Connections and select New Connection.

    Enter the following information and click Connect.

    Connection Name: unit_test_repos_orcl
    Username: unit_test_repos
    Password: oracle
    Select Save Password checkbox
    SID: orcl

    The unit_test_repos user and unit_test_repos_orcl connection were created successfully.

    Select Tools > Unit Test > Repository, then select Select Current Repository.

    Select the unit_test_repos_orcl connection and click OK.

    You would like to create a new repository. Click Yes.

    This connection does not have the permissions it needs to create the repository. Click OK to show the permissions that will be applied.

    Enter oracle for the sys password and click OK.

    The grant statement is shown. Click Yes.

    The UNIT_TEST_REPOS user needs select access to some required tables. Click OK.

    The grant statements are displayed. Click Yes.

    The UNIT_TEST_REPOS user does not currently have the ability to manage repository owners. Click OK to see the grant statements that will be executed.

    The grant statements are displayed. Click Yes.

    A progress window appears while the repository is created.

    Your repository was created successfully. Click OK.

 

Creating and Running a Unit Test

    Now that the Unit Testing Repository has been created, you will create a unit test for the PL/SQL procedure you created earlier in this tutorial. Then you will run the unit test to see if various values will work

    Select View > Unit Test.

    In the Unit Test navigator, right-click Tests and select Create Test.

    In Select Operation, select the hr_orcl connection that you used to create the AWARD_BONUS procedure.

    Expand Procedures, select AWARD_BONUS and click Next.

    In Specify Test Name window, make sure that AWARD_BONUS is specified for Test Name and that Create with single Dummy implementation is selected, then click Next.

    In Specify Startup window, click '+' icon and select Table or Row Copy from the drop down list box.

    Enter EMPLOYEES for Source Table and click OK. Note that the table affected by the test will be saved to a temporary table and the query to the table is automatically generated.

    Click Next.

    In the Specify Parameters window, change the Input string for EMP_ID to 149 and SALES_AMT to 2000 and click Next.

    Select the '+' icon to add a validation and select Query returning row(s) from the drop down list.

    Specify the following query and click OK. This query will test the results of the change that the unit test performed.

    SELECT * FROM employees    WHERE employee_id = 149
    AND salary = 11700;

    Click Next.

    In the Specify Teardown window,click the '+' icon and select Table or Row Restore from the drop down list.

    Leave the Row Identifier as Primary Key and click OK.

    Click Next.

    Click Finish.

    Expand Tests. Your test appears in the list.

    Select the AWARD_BONUS test in the left navigator. Notice that the test details are displayed on the right panel.

    Run the test by clicking the Debug Implementation icon. 

    The Progress window appears.

    When the test completes, the results are displayed. Click Close.

 

Summary

    In this tutorial, you have learned how to:

    • Create a database connection
    • Review existing objects in the HR schema
    • Execute a DDL script
    • Create table APIs
    • Create and execute a procedure
    • Create a unit test repository
    • Create and run a unit test

    Resources

posted @ 2012-06-27 15:49  *新*新*  阅读(506)  评论(0编辑  收藏  举报