Mar
9
Written by:
QCTO Blog
Tuesday, March 09, 2010
by Steven Feuerstein
An object type is Oracle's version of an object-oriented class. It shares some of the features of a class, but falls a bit short. In Code Tester 1.9 and higher you can define and run tests for methods of an object type. You can also directly test the contents of an instance of an object type.
Click
here to watch a video demonstrating the features described below.
Download Code Tester 1.9 here.
Set Value for Object Type IN Argument
Prior to Code Tester 1.9, the only way you could specify the value of an object type instance was to write initialization code to assign values to the attributes of that type.
Now, you can enter values for these attributes in the Attribute Grid of the Properties Window.
Here, for example, is the Properties Window of the "compare to" IN parameter for the ORDER function of the Quick Start object type, qctod#test_object_type:

If the attribute has a scalar datatype, you can enter the value directly in the grid. You can only specify single values (as literals, expressions or queries). Multiple values (random generation, from lists, etc.) are not allowed.
If an attribute of your object type is a complex type, then you will not be able to type a value in the grid. Instead, you will need to open the Properties Window for that type and then write code in the initialization section to populate this attribute.
How to Test Object Type Methods
To select an object type for testing, simply click on "New Test" on the toolbar or use the Dashboard menu. You will then see the "Choose Program to Test" list, which now includes types. Simply choose the type of interest and you will be taken to the Test Builder, at which time you then select the method that you want to test.
You can define several different types of methods in an object type: constructor, static and member methods. You can also test the contents of instances of an object type. You will find links below to details on how to test each of these types.
You can tell the type of your method in the combo list at the top of Test Builder:

Test Constructors
A constructor is a function whose name matches the type itself and is used to construct a new instance of that type. Oracle provides a default constructor for each type. You must, however, provide a value for each attribute when you call the constructor. As there are no private attributes in an Oracle object type, this means that a programmer will have to be aware of and provide sensible values for all attributes.
Often, this is very much not how an instance should be initialized, so you can define your own constructor in which you specify precisely the input values desired. A constructor must be of the form:
CONSTRUCTOR FUNCTION object_type_name (
SELF IN OUT NOCOPY object_type_name (your_parameter_list)
RETURN SELF AS RESULT
To test a constructor, you choose that method from the program list at the top of Test Builder. The Input Grid will contain a row for every parameter in your function's parameter list. The Outcome Grid will contain the following:
- A row for the object type instance returned by the function. How to Test Object Type Instances explains how you can complete the definition of this outcome.
- A row for each attribute of the object type. The outcome that you can defined for each attribute is determined by its datatype.
Generally, you will not want to set up an outcome for both the instance returned by the function and its attributes. Simply remove those outcomes that do not apply for your particular test.
Here, for example, is the default outcome grid for the constructor function of the Quick Start object type, qctod#test_object_type:

Test Static Methods
A static method is a function or method defined in an object type that is not connected to an instance of that object type. Every IN and IN OUT parameter will appear in the Input Grid. You can define one or more outcomes for the OUT and IN OUT parameters (and return value, if the static method is a function).
You will test static methods in exactly the same fashion as you would a procedure or function in a package.
Test Type Member Methods
A member method is a procedure or function that operates on a particular instance of an object type. It is executed in the form TYPE.METHOD, that is using dot notation.
A member method has associated with it a special parameter named SELF, which is the instance of the object type on which the method is invoked. For functions, SELF is by default IN only (the body of the function cannot change the values of an attribute of the instance). If a procedure, SELF is IN OUT by default, but that can be over-ridden as well (by explicitly specifying SELF as a parameter with the non-default parameter mode).
When you choose to test a member, each parameter of the member is displayed in the Input Grid, as occurs with a package-based subprogram.
If SELF is an IN or IN OUT parameter, each attribute of the object type is also displayed in the Input Grid, so that you can directly specify values for those attributes. Here, for example, is the Input Grid for the member procedure of the Quick Start object type, qctod#test_object_type:
Notice that each input's name is prefaced by "SELF" when it is the attribute of the instance.
If SELF is an OUT or IN OUT parameter, then Code Tester displays in the Outcome Grid a starting point of an outcome for each attribute of the object type. An outcome is also provided for the object type as a whole. You can remove any outcomes for which you do not need or want to provide a test. Here, for example, is the Input Grid for the member function, return_as_attr2_self_inout, of the Quick Start object type, qctod#test_object_type (it defines SELF as IN OUT):

Notice that each outcome's name is prefaced by "SELF" when it is the attribute of the instance.
Test MAP and ORDER Methods
MAP and ORDER methods are defined so that Oracle can perform native equality and inequality checks, as well as be able to compare two object types (as in, "Is object type 1 > object type 2?").
The MAP method returns a number. That number determines the relative "value" of the object type and is used in the comparisons.
The ORDER method returns one of three integer values: -1, 0 and 1. A 0 return value means that the two types are equal. Returning 1 means that the current (SELF) instance is greater than the instance passed as the ORDER method's only argument. Returning -1 indicates that the instance passed as the ORDER method's only argument is greater than the SELF instance.
To test a map method, you simply provide input values for each attribute of the type and then define the appropriate outcome on the number returned by the method.
To test an order method, Code Tester provides in the Input Grid one row for each of the attributes of the SELF instance, and one row for the ORDER's comparison object type.
Here, for example, is the default outcome grid for the ORDER member function of the Quick Start object type, qctod#test_object_type:

How to Test Object Type Instances
An instance of an object type is a variable (or, in the context of Code Tester, an expression) or a parameter whose datatype is the object type. You can test an object type instance by comparing one instance to another using an equality and inequality check. You can also provide values for each attribute and these will be compared individually.
Compare Object Type Instances
If the program you are testing returns an object type instance, or you define an Expression outcome and the type of the expression is an object type, then you will need to set up a test that compares the contents of an actual (changed by the program) object type instance and an expected instance.
Only two types of tests are defined for object type instances: equality and inequality, as you can see in this window from the Outcome Grid:

If the object type does not have a MAP or ORDER member defined in it, then Code Tester will test for equality by comparing each of the comparable attributes for equality. If the values of each of the attributes of the actual and expected instances are all the same (or both are NULL), then Code Tester records the test as a success. If the value of at least one attribute differs, the test has failed.
If the object type does have a MAP or ORDER member defined in it, then Code Tester will test for equality by calling that member function. In this case, success or failure is determined by the logic that the author of the instance has put into those functions.
Extending the Reach of Quest Code Tester for Oracle
The ultimate objective of Code Tester is to make it possible for PL/SQL developers to test their programs without having to write any code at all.
Version 1.9 takes Code Tester a big step closer to this objective, by making it possible to declaratively test the methods and attributes of an object type.