Skip to content

Instantly share code, notes, and snippets.

@jamesdaily
Last active March 25, 2016 16:20
Show Gist options
  • Save jamesdaily/0c0c7278bb1eae20eddb to your computer and use it in GitHub Desktop.
Save jamesdaily/0c0c7278bb1eae20eddb to your computer and use it in GitHub Desktop.
PL/SQL assertEquals function for comparing values in SQL queries
CREATE OR REPLACE FUNCTION assertEquals (pinInput VARCHAR2, pinExpectedValue VARCHAR2)
RETURN VARCHAR2
IS
/*
James Daily 3/25/2016
Helper function for comparing values in SQL queries
Copyright (c) 2016 James David Daily
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
BEGIN
IF (pinInput IS NULL AND pinExpectedValue IS NULL) THEN
RETURN 'Correct - both are NULL';
ELSIF (pinInput IS NULL) THEN
RETURN 'Wrong - expected [' || pinExpectedValue || '], got NULL';
ELSIF (pinExpectedValue IS NULL) THEN
RETURN 'Wrong - expected NULL, got [' || pinInput || ']';
ELSIF (pinInput != pinExpectedValue) THEN
RETURN 'Wrong - expected [' || pinExpectedValue || '], got [' || pinInput || ']';
ELSIF (pinInput = pinExpectedValue) THEN
RETURN 'Correct [' || pinExpectedValue || ']';
ELSE
RETURN 'Error - situation not handled - expected [' || pinExpectedValue || '] got [' || pinInput || ']';
END IF;
EXCEPTION WHEN OTHERS THEN
RETURN 'Exception occurred ['||SQLERRM||'] - expected [' || pinExpectedValue || '] got [' || pinInput || ']';
END;
/
with myRecordUnderTest as (
select 'James' FirstName, 'Daily' LastName, 'Monkey Wrangler' Occupation, to_date('12/23','mm/dd') ChristmasDay from dual
)
select assertEquals(FirstName, 'James'),
assertEquals(LastName, 'Daily'),
assertEquals(Occupation,'Engineer'),
assertEquals(ChristmasDay,to_date('12/25','mm/dd'))
from myRecordUnderTest;
/
ASSERTEQUALS(FIRSTNAME,'JAMES'): Correct [James]
ASSERTEQUALS(LASTNAME,'DAILY'): Correct [Daily]
ASSERTEQUALS(OCCUPATION,'ENGINEER'): Wrong - expected [Engineer], got [Monkey Wrangler]
ASSERTEQUALS(CHRISTMASDAY,TO_DATE('12/25','MM/DD')): Wrong - expected [25-DEC-16], got [23-DEC-16]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment