Skip to content

Commit 4c4fb74

Browse files
committed
Completed Table Test
1 parent d6bb18d commit 4c4fb74

File tree

7 files changed

+103
-331
lines changed

7 files changed

+103
-331
lines changed

docs/demo/Table-Test.md

Lines changed: 48 additions & 188 deletions
Original file line numberDiff line numberDiff line change
@@ -1,208 +1,85 @@
11
[Demos and Examples](README.md)
22

3-
# Test a Trigger
3+
# Test Table Constraints
44

55
---
66

7-
## Triggers
8-
There are many kinds of triggers. All of them use PL/SQL to define actions taken when the the trigger is activated.
7+
The syntax diagram in Oracle's "Database SQL Language Reference" (11.2) gives the list of [constraints](https://docs.oracle.com/cd/E11882_01/server.112/e41084/clauses002.htm#CJAEDFIB) this way:
98

10-
The "Database PL/SQL Language Reference" (11.2) [groups triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABBJHHG) this way:
11-
* Simple DML Trigger
12-
* Compound DML Trigger
13-
* Instead of DML Trigger
14-
* System Trigger
9+
* Not Null
10+
* Unique (Key)
11+
* Primary Key
12+
* References (Foreign Key)
13+
* Check
1514

16-
[Simple DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABBJHHG):
17-
* before delete
18-
* before insert
19-
* before update
20-
* after delete
21-
* after insert
22-
* after update
15+
Typical unit testing (or white box testing) does not include the testing of constraints. In large part, these constraints are assumed to work without testing. Confirmation of continued function of these constraints is a reason to test them.
2316

24-
[Compound DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABDFIFA):
25-
* before delete statement
26-
* before insert statement
27-
* before update statement
28-
* before each row deleted
29-
* before each row inserted
30-
* before each row updated
31-
* instead of each row deleted
32-
* instead of each row inserted
33-
* instead of each row updated
34-
* after each row deleted
35-
* after each row inserted
36-
* after each row updated
37-
* after delete statement
38-
* after insert statement
39-
* after update statement
17+
## Table with Constraints
4018

41-
[Instead of DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#CIHEIGBE):
42-
* instead of delete
43-
* instead of insert
44-
* instead of update
45-
46-
[System Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABHEFGE):
47-
* before alter statement
48-
* before analyze statement
49-
* before associate statistics statement
50-
* before audit statement
51-
* before comment statement
52-
* before create statement
53-
* before database shutdown
54-
* before disassociate statistics statement
55-
* before drop statement
56-
* before grant statement
57-
* before noaudit statement
58-
* before rename statement
59-
* before revoke statement
60-
* before truncate statement
61-
* before user logoff
62-
* instead of alter statement
63-
* instead of analyze statement
64-
* instead of associate statistics statement
65-
* instead of audit statement
66-
* instead of comment statement
67-
* instead of create statement
68-
* instead of disassociate statistics statement
69-
* instead of drop statement
70-
* instead of grant statement
71-
* instead of noaudit statement
72-
* instead of rename statement
73-
* instead of revoke statement
74-
* instead of truncate statement
75-
* after alter statement
76-
* after analyze statement
77-
* after associate statistics statement
78-
* after audit statement
79-
* after comment statement
80-
* after create statement
81-
* after database startup
82-
* after db role change
83-
* after disassociate statistics statement
84-
* after drop statement
85-
* after grant statement
86-
* after noaudit statement
87-
* after rename statement
88-
* after revoke statement
89-
* after servererror
90-
* after truncate statement
91-
* after user logon
92-
* after user suspend
93-
94-
For brevity, an example is provided for only one of these triggers.
95-
96-
## Table with Insert Trigger
97-
98-
Before a trigger an be created, a table must be created. The table will have a surrogate key, a natural key, and audit data.
19+
A table with constraints is needed for testing. This table has several constraints
9920

10021
Run this:
10122

10223
```
103-
create sequence trigger_test_seq;
104-
105-
create table trigger_test_tab
106-
(id number constraint trigger_test_tab_nn1 not null
107-
,name varchar2(30) constraint trigger_test_tab_nn2 not null
108-
,created_dtm date constraint trigger_test_tab_nn3 not null
109-
,constraint trigger_test_tab_pk primary key (id)
110-
,constraint trigger_test_tab_uk1 unique (name)
24+
create table table_test_tab
25+
(id number constraint table_test_tab_nn1 not null
26+
,name varchar2(10) constraint table_test_tab_nn2 not null
27+
,constraint table_test_tab_pk primary key (id)
28+
,constraint table_test_tab_uk1 unique (name)
29+
,constraint table_test_tab_ck1 check (name = upper(name))
11130
);
11231
```
11332

114-
The trigger to be tested does 2 things
115-
1) Populate the surrogate key, if needed.
116-
2) Overwrite the audit data.
117-
118-
Run this:
119-
120-
```
121-
create or replace trigger trigger_test_bir
122-
before insert on trigger_test_tab
123-
for each row
124-
begin
125-
if :new.id is null
126-
then
127-
:new.id := trigger_test_seq.nextval;
128-
end if;
129-
:new.created_dtm := sysdate;
130-
end;
131-
/
132-
```
33+
For brevity, the check constraint will be the only constraint tested.
13334

134-
## Create a Simple Test Runner
35+
## Test Runner
13536

136-
All test runners are written as a PL/SQL package. A simple package is created first. A DBOUT is also identified.
37+
Create a simple test runner.
13738

13839
Run this:
13940

14041
```
141-
create or replace package trigger_test_pkg authid definer
42+
create or replace package table_test_pkg authid definer
14243
as
14344
procedure wtplsql_run;
144-
end trigger_test_pkg;
45+
end table_test_pkg;
14546
/
14647
```
14748

148-
And run this:
149-
150-
```
151-
create or replace package body trigger_test_pkg
152-
as
153-
--% WTPLSQL SET DBOUT "TRIGGER_TEST_BIR:TRIGGER" %--
154-
procedure wtplsql_run
155-
as
156-
begin
157-
null;
158-
end wtplsql_run;
159-
end trigger_test_pkg;
160-
/
161-
```
162-
163-
## Add a Trigger Test Case
164-
165-
The trigger being tested is a table DML trigger. Testing of a table trigger like this requires a modification of the data in the table. The consequences of leaving this modified data after the test must be considered. In this test, the data modification will not be preserved.
49+
The constraint being tested ensures the name is in upper case. Testing of the constraint requires a modification of the data in the table. The consequences of leaving this modified data after the test must be considered. In this test, the data modification will not be preserved.
16650

16751
This test case will only test a happy path.
16852

16953
Run this:
17054

17155
```
172-
create or replace package body trigger_test_pkg
56+
create or replace package body table_test_pkg
17357
as
17458
procedure t_happy_path_1
17559
is
176-
l_rec trigger_test_tab%ROWTYPE;
60+
l_rec table_test_tab%ROWTYPE;
17761
begin
178-
wt_assert.g_testcase := 'Constructor Happy Path 1';
179-
-- This uncommitted DML will ROLLBACK if an exception is raised.
180-
insert into trigger_test_tab (name) values ('Test1')
181-
returning id into l_rec.id;
182-
wt_assert.isnotnull (
183-
msg_in => 'l_rec.id',
184-
check_this_in => l_rec.id);
185-
select * into l_rec from trigger_test_tab where id = l_rec.id;
62+
wt_assert.g_testcase := 'Happy Path 1';
63+
wt_assert.raises (
64+
msg_in => 'Successful Insert',
65+
check_call_in => 'insert into table_test_tab (id, name) values (1, ''TEST1'')',
66+
against_exc_in => '');
67+
select * into l_rec from table_test_tab where id = 1;
18668
wt_assert.eq (
187-
msg_in => 'l_rec.name',
69+
msg_in => 'Confirm l_rec.name',
18870
check_this_in => l_rec.name,
189-
against_this_in => 'Test1');
190-
wt_assert.isnotnull (
191-
msg_in => 'l_rec.created_dtm',
192-
check_this_in => l_rec.created_dtm);
71+
against_this_in => 'TEST1');
19372
rollback;
19473
end t_happy_path_1;
195-
--% WTPLSQL SET DBOUT "TRIGGER_TEST_BIR:TRIGGER" %--
196-
procedure wtplsql_run
197-
is
74+
procedure wtplsql_run is
19875
begin
19976
t_happy_path_1;
20077
end wtplsql_run;
201-
end trigger_test_pkg;
78+
end table_test_pkg;
20279
/
20380
```
20481

205-
Check the results of the
82+
## Check the results
20683

20784
Run this:
20885

@@ -219,42 +96,25 @@ end;
21996
And Get This:
22097

22198
```
222-
wtPLSQL 1.1.0 - Run ID 58: 23-Jun-2018 12:04:20 PM
99+
wtPLSQL 1.1.0 - Run ID 70: 23-Jun-2018 07:30:47 PM
223100
224-
Test Results for WTP_DEMO.TRIGGER_TEST_PKG
225-
Total Test Cases: 1 Total Assertions: 3
101+
Test Results for WTP_DEMO.TABLE_TEST_PKG
102+
Total Test Cases: 1 Total Assertions: 2
226103
Minimum Interval msec: 0 Failed Assertions: 0
227-
Average Interval msec: 76 Error Assertions: 0
228-
Maximum Interval msec: 228 Test Yield: 100.00%
229-
Total Run Time (sec): 0.2
104+
Average Interval msec: 443 Error Assertions: 0
105+
Maximum Interval msec: 886 Test Yield: 100.00%
106+
Total Run Time (sec): 0.9
230107
231-
Code Coverage for TRIGGER WTP_DEMO.TRIGGER_TEST_BIR
232-
Ignored Lines: 0 Total Profiled Lines: 5
233-
Excluded Lines: 0 Total Executed Lines: 4
234-
Minimum LineExec usec: 1 Not Executed Lines: 0
235-
Average LineExec usec: 137 Unknown Lines: 1
236-
Maximum LineExec usec: 326 Code Coverage: 100.00%
237-
Trigger Source Offset: 3
238-
239-
- WTP_DEMO.TRIGGER_TEST_PKG Test Result Details (Test Run ID 58)
108+
- WTP_DEMO.TABLE_TEST_PKG Test Result Details (Test Run ID 70)
240109
-----------------------------------------------------------
241-
---- Test Case: Constructor Happy Path 1
242-
PASS 228ms l_rec.id. ISNOTNULL - Expected NOT NULL and got "15"
243-
PASS 0ms l_rec.name. EQ - Expected "Test1" and got "Test1"
244-
PASS 0ms l_rec.created_dtm. ISNOTNULL - Expected NOT NULL and got "23-JUN-2018 12:04:20"
245-
246-
- WTP_DEMO.TRIGGER_TEST_BIR TRIGGER Code Coverage Details (Test Run ID 58)
247-
Source TotTime MinTime MaxTime
248-
Line Stat Occurs (usec) (usec) (usec) Text
249-
------ ---- ------ --------- ------- --------- ------------
250-
4 UNKN 0 11 11 11 begin
251-
5 EXEC 1 216 216 216 if :new.id is null
252-
7 EXEC 1 326 326 326 :new.id := trigger_test_seq.nextval;
253-
9 EXEC 1 4 1 3 :new.created_dtm := sysdate;
254-
10 EXEC 1 2 2 2 end;
110+
---- Test Case: Happy Path 1
111+
PASS 886ms Successful Insert. RAISES/THROWS - No exception was expected. Exception raised was "". Exception raised by: "insert into table_test_tab (id, name) values (1, 'TEST1')".
112+
PASS 0ms Confirm l_rec.name. EQ - Expected "TEST1" and got "TEST1"
255113
```
256114

257-
This is report level 30, the most detailed level of reporting. Starting from the top, we find the test runner executed 1 test case and 3 assertions. All tests passed for a 100% yield. The code coverage for the trigger shows 5 profiles, 4 executed, and a code coverage of 100%. Notice the trigger offset of 3 which aligns the source code with the profiled lines.
115+
This is report level 30, the most detailed level of reporting. Starting from the top, we find the test runner executed 1 test case and 2 assertions. All tests passed for a 100% yield. There is no code coverage for the constraints.
116+
117+
This is not a complete test. More test cases are needed to confirm other constraints and sad path .
258118

259119
---
260120
[Demos and Examples](README.md)

docs/demo/Trigger-Test.md

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
---
66

77
## Triggers
8-
There are many kinds of triggers. All of them use PL/SQL to define actions taken when the the trigger is activated.
8+
There are many kinds of triggers. All of them use PL/SQL to define actions taken when the trigger is activated.
99

1010
The "Database PL/SQL Language Reference" (11.2) [groups triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABBJHHG) this way:
1111
* Simple DML Trigger
@@ -15,11 +15,11 @@ The "Database PL/SQL Language Reference" (11.2) [groups triggers](https://docs.o
1515

1616
[Simple DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABBJHHG):
1717

18-
Before | After
19-
--------|-------------
20-
Delete | Delete
21-
Insert | Insert
22-
Update | Update
18+
Before | After
19+
---------------|--------------
20+
Before Delete | After Delete
21+
Before Insert | After Insert
22+
Before Update | After Update
2323

2424
[Compound DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABDFIFA):
2525

@@ -33,9 +33,12 @@ The "Database PL/SQL Language Reference" (11.2) [groups triggers](https://docs.o
3333
Update Statement | | Update Statement
3434

3535
[Instead of DML Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#CIHEIGBE):
36-
* instead of delete
37-
* instead of insert
38-
* instead of update
36+
37+
Instead of |
38+
-------------------|
39+
Instead of Delete |
40+
Instead of Insert |
41+
Instead of Update |
3942

4043
[System Triggers](https://docs.oracle.com/cd/E11882_01/appdev.112/e25519/create_trigger.htm#BABHEFGE):
4144

@@ -114,23 +117,6 @@ end trigger_test_pkg;
114117
/
115118
```
116119

117-
And run this:
118-
119-
```
120-
create or replace package body trigger_test_pkg
121-
as
122-
--% WTPLSQL SET DBOUT "TRIGGER_TEST_BIR:TRIGGER" %--
123-
procedure wtplsql_run
124-
as
125-
begin
126-
null;
127-
end wtplsql_run;
128-
end trigger_test_pkg;
129-
/
130-
```
131-
132-
## Add a Trigger Test Case
133-
134120
The trigger being tested is a table DML trigger. Testing of a table trigger like this requires a modification of the data in the table. The consequences of leaving this modified data after the test must be considered. In this test, the data modification will not be preserved.
135121

136122
This test case will only test a happy path.
@@ -171,7 +157,7 @@ end trigger_test_pkg;
171157
/
172158
```
173159

174-
Check the results of the
160+
## Check the results
175161

176162
Run this:
177163

@@ -225,5 +211,7 @@ Source TotTime MinTime MaxTime
225211

226212
This is report level 30, the most detailed level of reporting. Starting from the top, we find the test runner executed 1 test case and 3 assertions. All tests passed for a 100% yield. The code coverage for the trigger shows 5 profiles, 4 executed, and a code coverage of 100%. Notice the trigger offset of 3 which aligns the source code with the profiled lines.
227213

214+
This is not a complete test. More test cases are needed to confirm various values are handled correctly when inserted.
215+
228216
---
229217
[Demos and Examples](README.md)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy