forked from KenRoytman/utPLSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsamepack.html
executable file
·189 lines (166 loc) · 6.98 KB
/
samepack.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<!-- WARNING! This file is generated. -->
<!-- To alter documentation, edit files in src directory -->
<html><head>
<title>Put Test Code in Same Package</title>
<link rel="stylesheet" href="utplsql.css" content="text/css">
<meta name="keywords" content="utPLSQL, PL\SQL, Unit Testing, Framework, Oracle"/>
<meta name="description" content="Unit Testing PL\SQL"/>
<meta name="title" content="Put Test Code in Same Package"/>
<meta name="author" content="Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
<meta name="copyright" content="(C) 2000-2005 Steven Feuerstein, Chris Rimmer, Patrick Barel"/>
</head><body>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p>[ <A href="index.html">Home</A>
| <A href="started.html">Getting Started</A>
| <A href="buildpack.html">Build Test Packages</A>
| <A href="examples.html">Examples</A>
| <A href="userguide.html">User Guide</A>
| <A href="release.html">Release Notes</A>
| <A href="map.html">Document Map</A> ]</p>
<p><A href="testapi.html">< Previous Section: Test an Entire Package API</A> | <A href="prefix.html">Next Section: Use Non-Default Prefix ></A></p>
<!-- Begin utPLSQL Body -->
<!-- $Id: samepack.html,v 1.3 2002/07/25 12:16:44 chrisrimmer Exp $ -->
<h1>
Put Test Code in Same Package</h1>
<p>In some cases (usually when your packages are small and the code you
need to write to construct your tests is also constrained), you will not
want to bother with creating a separate package to test your code. To do
this, you will put the setup, teardown and unit test procedures inside
the package specification and body. We look at two examples:
<ul>
<li>
<a href="#str">Testing a simple string function</a></li>
<li>
<a href="#coll">Testing the population of a collection</a></li>
</ul>
<h3>
<a name="str">
Testing a simple string function</a></h3>
Suppose I have my basic sting package, containing (for now at least) just
a single function:
<pre>/*file str.pks */
CREATE OR REPLACE PACKAGE str
IS
FUNCTION betwn (
string_in IN VARCHAR2,
start_in IN PLS_INTEGER,
end_in IN PLS_INTEGER
)
RETURN VARCHAR2;
END str;
/</pre>
Now it is time to test the function. I really don't want to bother with
a separate package; let's keep it together. To do this, I change the specification
to:
<pre>CREATE OR REPLACE PACKAGE str
IS
FUNCTION betwn (
string_in IN VARCHAR2,
start_in IN PLS_INTEGER,
end_in IN PLS_INTEGER
)
RETURN VARCHAR2;
PROCEDURE ut_setup;
PROCEDURE ut_teardown;
-- For each program to test...
PROCEDURE ut_betwn;
END str;
/</pre>
The package body contains nothing unusual; it is the same test for str.betwn
that you can find in the <a href="testfunc.html#ScalarFunction">Testing
a Scalar Function</a> example. But when I execute my test, I need to tell
utPLSQL that my test code is located in the same package:
<pre>SQL> exec utconfig.showconfig
=============================================================
utPLSQL Configuration for SCOTT
Directory: e:\openoracle\utplsql\utinstall\examples
Autcompile? Y
Manual test registration? N
Prefix = ut_
=============================================================
PL/SQL procedure successfully completed.
SQL> exec utPLSQL.test ('str', samepackage_in => TRUE)
.
> SSSS U U CCC CCC EEEEEEE SSSS SSSS
> S S U U C C C C E S S S S
> S U U C C C C E S S
> S U U C C E S S
> SSSS U U C C EEEE SSSS SSSS
> S U U C C E S S
> S U U C C C C E S S
> S S U U C C C C E S S S S
> SSSS UUU CCC CCC EEEEEEE SSSS SSSS
.
SUCCESS: "str"</pre>
<h3><a name="coll">
Testing the population of a collection</a></h3>
Collections are very useful structures, but they can be difficult to analyze
and compare. utPLSQL provides the utAssert.eqColl and utAssert.eqCollAPI
programs to help you do this.
<p>For this example, consider the fileIO package: it implements a path
feature for the UTL_FILE package. In other words, you request to open a
file and your file-opening program will search through each of the directories
in the path in sequence until it finds the file or exhausts the list. Here
is the specification of this package:
<pre>/*file filepath1.pkg */
CREATE OR REPLACE PACKAGE fileIO
IS
c_delim CHAR(1) := ';';
dirs dirs_tabtype := dirs_tabtype ();
-- Unit test list
ut_dirs dirs_tabtype := dirs_tabtype ();
PROCEDURE setpath (str IN VARCHAR2, delim IN VARCHAR2 := c_delim);
FUNCTION path RETURN VARCHAR2;
FUNCTION pathlist RETURN dirs_tabtype;
FUNCTION open (file IN VARCHAR2, loc IN VARCHAR2 := NULL) RETURN UTL_FILE.FILE_TYPE;
-- Unit test code in same package
PROCEDURE ut_setup;
PROCEDURE ut_teardown;
PROCEDURE ut_setpath;
END;
/</pre>
A few things to notice about this package:
<ul>
<li>
It declares a publicly available collection, fileIO.dirs, in which the
path is deposited. Because it is declared in the package specification,
I can use the utAssert.eqColl procedure (if the collection was hidden in
the package body, I would have to use utAssert.eqCollAPI).</li>
The test code is in the same package: the setup,
teardown and test pogram for fileIO.setpath.
I declare a second, publicly available collection,
fileIO.ut_dirs, against which I will compare the path that is populated
by fileIO.setpath.
</ul>
Given that, let's take a look at the implementation of the test program:
<pre><a name="useseqcoll"></a>PROCEDURE ut_setpath
IS
BEGIN
/* Populate base collection */
ut_dirs.DELETE;
ut_dirs.EXTEND(2);
ut_dirs(1) := 'c:\temp';
ut_dirs(2) := 'e:\demo';
/* Call setpath to do the work */
setpath ('c:\temp;e:\demo');
utAssert.eqColl (
'Valid double entry',
'fileio.dirs',
'fileio.ut_dirs'
);
END;</pre>
This program consists of three steps:
<blockquote>
<li>
Populate the test collection with direct assignments. Call the setPath
program to populate the actual collection (fileIO.dirs). Call the assertion
program to compare the two. Notice that I pass the <i>names</i> of the
collections to the assertion program. utAssert uses dynamic SQL to build
a PL/SQL block "on the fly" that compares values from the collections.</li>
</blockquote>
<!-- End utPLSQL Body -->
<p><A href="testapi.html">< Previous Section: Test an Entire Package API</A> | <A href="prefix.html">Next Section: Use Non-Default Prefix ></A></p>
<div class="purple_bar"><a href="index.html"><img src="utplsql.jpg" border=0></a></div>
<p class="copyright">Copyright (C) 2000-2005 <A href="mailto:[email protected]">Steven Feuerstein<A>, <A href="mailto:[email protected]">Chris Rimmer<A>, <A href="mailto:[email protected]">Patrick Barel<A> All rights reserved</p>
</body></html>