Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3cfc16c

Browse files
authored
Merge pull request #502 from utPLSQL/feature/add_exception_handling_example_to_doc
Added example of handling exceptions to documentation.
2 parents 7037319 + 80493a5 commit 3cfc16c

1 file changed

Lines changed: 48 additions & 10 deletions

File tree

docs/userguide/expectations.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,22 @@ begin
247247
end;
248248
```
249249

250+
## match
251+
Validates that the actual value is matching the expected regular expression.
252+
253+
Usage:
254+
```sql
255+
begin
256+
ut.expect( a_actual => '123-456-ABcd' ).to_match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' );
257+
ut.expect( 'some value' ).to_match( '^some.*' );
258+
--or
259+
ut.expect( a_actual => '123-456-ABcd' ).to_( match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' ) );
260+
ut.expect( 'some value' ).to_( match( '^some.*' ) );
261+
end;
262+
```
263+
264+
Parameters `a_pattern` and `a_modifiers` represent a valid regexp pattern accepted by [Oracle REGEXP_LIKE condition](https://docs.oracle.com/database/121/SQLRF/conditions007.htm#SQLRF00501)
265+
250266
## equal
251267

252268
The equal matcher is a very restrictive matcher. It only returns true if the compared data-types are the same.
@@ -458,22 +474,44 @@ end;
458474

459475
This test will fail as `v_actual` is not equal `v_expected`.
460476

461-
## match
462-
Validates that the actual value is matching the expected regular expression.
477+
# Expecting exceptions
478+
479+
Below example illustrates how to write test to check for expected exceptions (thrown by tested code).
463480

464-
Usage:
465481
```sql
466-
begin
467-
ut.expect( a_actual => '123-456-ABcd' ).to_match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' );
468-
ut.expect( 'some value' ).to_match( '^some.*' );
469-
--or
470-
ut.expect( a_actual => '123-456-ABcd' ).to_( match( a_pattern => '\d{3}-\d{3}-[a-z]', a_modifiers => 'i' ) );
471-
ut.expect( 'some value' ).to_( match( '^some.*' ) );
482+
create or replace procedure divide(p_a number, p_b number) is
483+
begin
484+
return p_a / p_b;
472485
end;
486+
/
487+
488+
create or replace package test_divide is
489+
--%suite(Divide functionality)
490+
491+
--%test(Raises exception when divisor is zero)
492+
procedure divide_raises_zero_divisor;
493+
end;
494+
/
495+
create or replace package body test_divide is
496+
procedure divide_raises_zero_divisor is
497+
l_my_number number;
498+
begin
499+
l_my_number := divide(1,0); -- PLSQL call throwing ORA-01476 exception
500+
ut.fail('Expected exception but nothing was raised');
501+
exception
502+
when others then
503+
ut.expect( sqlcode ).to_equal( -1476 );
504+
ut.expect( sqlerrm ).to_match( 'equal to zero' );
505+
end;
506+
end;
507+
/
473508
```
474509

475-
Parameters `a_pattern` and `a_modifiers` represent a valid regexp pattern accepted by [Oracle REGEXP_LIKE condition](https://docs.oracle.com/database/121/SQLRF/conditions007.htm#SQLRF00501)
510+
The call to `ut.fail` is required to make sure that the test fails, if we expect an exception, but the tested code does not throw any.
511+
512+
The call to `ut.expect` uses `equal` matcher to check that the exception that was raised was exactly the one we were expecting to get in particular situation.
476513

514+
Depending on the situation you will want to check for `sqlcode`, `sqlerrm`, both or perform additional expectation checks to make sure nothing was changed by the called procedure in the database.
477515

478516

479517
# Supported data types

0 commit comments

Comments
 (0)