Changeset 5
- Timestamp:
- 08/16/08 12:38:29 (4 years ago)
- Location:
- trunk/epic
- Files:
-
- 2 modified
-
epic.sql (modified) (4 diffs)
-
test/test_asserts.sql (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/epic/epic.sql
r4 r5 95 95 to assert anything that can be evaluated to a boolean. For example, 96 96 PERFORM test.assert(substring(a from b), b||" not found in "||a); 97 * test.assert_equal(1 anyelement, 2 anyelement) 98 * test.assert_not_equal(1 anyelement, 2 anyelement) 99 * test.assert_less_than(1 anyelement, 2 anyelement) 97 * test.assert_void(call text) 98 * test.assert_equal(elem_1 anyelement, elem_2 anyelement) 99 * test.assert_not_equal(elem_1 anyelement, elem_2 anyelement) 100 * test.assert_greater_than(elem_1 anyelement, elem_2 anyelement) 101 * test.assert_greater_than_or_equal(elem_1 anyelement, elem_2 anyelement) 102 * test.assert_less_than(elem_1 anyelement, elem_2 anyelement) 103 * test.assert_less_than_or_equal(elem_1 anyelement, elem_2 anyelement) 100 104 * test.assert_values(column text, source text, expected anyarray): 101 105 Raises an exception if SELECT column FROM source != expected. 102 106 103 107 * test.assert_raises(call text, errm text, state text): Raises an 104 exception if 'SELECT * FROM [call];' does not raise errm. 108 exception if 'SELECT * FROM [call];' does not raise errm 109 (if provided) or state (if provided). 105 110 106 111 Some return dynamic SQL: … … 290 295 291 296 297 CREATE OR REPLACE FUNCTION test.assert_void(call text) RETURNS VOID AS $$ 298 -- Raises an exception if SELECT * FROM call != void. 299 DECLARE 300 retval text; 301 BEGIN 302 EXECUTE ('SELECT * FROM ' || call || ';') INTO retval; 303 IF retval != '' THEN 304 RAISE EXCEPTION 'Call: ''%'' did not return void. Got ''%'' instead.', call, retval; 305 END IF; 306 END; 307 $$ LANGUAGE plpgsql; 308 309 292 310 CREATE OR REPLACE FUNCTION test.assert(assertion boolean, msg text) RETURNS VOID AS $$ 293 311 -- Raises an exception (msg) if assertion is false. … … 334 352 335 353 354 CREATE OR REPLACE FUNCTION test.assert_less_than(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 355 -- Raises an exception if elem_1 >= elem_2 356 -- 357 -- The two arguments must be of the same type. If they are not, 358 -- you will receive "ERROR: invalid input syntax ..." 359 BEGIN 360 IF (elem_1 IS NULL or elem_2 IS NULL) THEN 361 RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 362 END IF; 363 IF NOT (elem_1 < elem_2) THEN 364 RAISE EXCEPTION '% not < %', elem_1, elem_2; 365 END IF; 366 END; 367 $$ LANGUAGE plpgsql; 368 369 336 370 CREATE OR REPLACE FUNCTION test.assert_less_than_or_equal(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 337 371 -- Raises an exception if elem_1 > elem_2 … … 345 379 IF NOT (elem_1 <= elem_2) THEN 346 380 RAISE EXCEPTION '% not <= %', elem_1, elem_2; 381 END IF; 382 END; 383 $$ LANGUAGE plpgsql; 384 385 386 CREATE OR REPLACE FUNCTION test.assert_greater_than(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 387 -- Raises an exception if elem_1 <= elem_2 388 -- 389 -- The two arguments must be of the same type. If they are not, 390 -- you will receive "ERROR: invalid input syntax ..." 391 BEGIN 392 IF (elem_1 IS NULL or elem_2 IS NULL) THEN 393 RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 394 END IF; 395 IF NOT (elem_1 > elem_2) THEN 396 RAISE EXCEPTION '% not > %', elem_1, elem_2; 397 END IF; 398 END; 399 $$ LANGUAGE plpgsql; 400 401 402 CREATE OR REPLACE FUNCTION test.assert_greater_than_or_equal(elem_1 anyelement, elem_2 anyelement) RETURNS VOID AS $$ 403 -- Raises an exception if elem_1 < elem_2 404 -- 405 -- The two arguments must be of the same type. If they are not, 406 -- you will receive "ERROR: invalid input syntax ..." 407 BEGIN 408 IF (elem_1 IS NULL or elem_2 IS NULL) THEN 409 RAISE EXCEPTION 'Assertion arguments may not be NULL.'; 410 END IF; 411 IF NOT (elem_1 >= elem_2) THEN 412 RAISE EXCEPTION '% not >= %', elem_1, elem_2; 347 413 END IF; 348 414 END; -
trunk/epic/test/test_asserts.sql
r4 r5 1 -- Tests for the various assert_* functions which Epic provides. 2 -- To run, execute epic.sql, then this script, then test.run_module('test_asserts'). 3 1 4 SET search_path = test, public, pg_catalog; 2 5 … … 94 97 95 98 99 CREATE OR REPLACE FUNCTION test.test_assert_void() RETURNS VOID AS $$ 100 -- Assert the correct operation of test.assert_void 101 -- module: test_asserts 102 DECLARE 103 retval text; 104 BEGIN 105 -- assert_void() MUST return VOID if the given call returns VOID. 106 SELECT INTO retval * FROM test.assert_void('pg_sleep(0.1)'); 107 IF retval != '' THEN 108 RAISE EXCEPTION 'assert_void did not itself return void. Got ''%'' instead.', retval; 109 END IF; 110 111 -- assert_void() MUST raise an exception if the given call does not return void. 112 PERFORM test.assert_raises('test.assert_void(''pg_namespace'')', 113 'Call: ''pg_namespace'' did not return void. Got ''pg_toast'' instead.', 114 'P0001'); 115 116 RAISE EXCEPTION '[OK]'; 117 END; 118 $$ LANGUAGE plpgsql; 119 120 96 121 CREATE OR REPLACE FUNCTION test.test_assert_equal() RETURNS VOID AS $$ 97 122 -- Assert the correct operation of test.assert_equal … … 101 126 BEGIN 102 127 -- assert_equal() MUST return VOID if the given assertion holds. 103 SELECT INTO retval * FROM test.assert_equal(1, 1); 104 IF retval != '' THEN 105 RAISE EXCEPTION 'test.assert_equal() did not return void for (1, 1).'; 106 END IF; 107 SELECT INTO retval * FROM test.assert_equal('abc'::text, 'abc'); 108 IF retval != '' THEN 109 RAISE EXCEPTION 'test.assert_equal() did not return void for (''abc'', ''abc'').'; 110 END IF; 128 PERFORM test.assert_void('test.assert_equal(1, 1)'); 129 PERFORM test.assert_void('test.assert_equal(''abc''::text, ''abc'');'); 130 131 -- assert_equal() MUST return VOID if both args are null. 132 PERFORM test.assert_void('test.assert_equal(NULL::int, NULL);'); 111 133 112 134 -- assert_equal() MUST raise an exception if the given assertion does not hold. 113 135 PERFORM test.assert_raises('test.assert_equal(1, 2)', '1 != 2', 'P0001'); 114 115 136 PERFORM test.assert_raises('test.assert_equal(''abc''::text, ''xyz'')', 'abc != xyz', 'P0001'); 116 117 -- assert_equal() MUST return VOID if both args are null.118 SELECT INTO retval * FROM test.assert_equal(NULL::int, NULL);119 IF retval != '' THEN120 RAISE EXCEPTION 'test.assert_equal() did not return void for (null, null).';121 END IF;122 137 123 138 -- assert_equal() MUST raise an exception if only one arg is NULL. … … 142 157 BEGIN 143 158 -- assert_not_equal() MUST return VOID if the given assertion does not hold. 144 SELECT INTO retval * FROM test.assert_not_equal(1, 2); 145 IF retval != '' THEN 146 RAISE EXCEPTION 'test.assert_not_equal() did not return void for (1, 2).'; 147 END IF; 148 SELECT INTO retval * FROM test.assert_not_equal('abc'::text, 'xyz'); 149 IF retval != '' THEN 150 RAISE EXCEPTION 'test.assert_not_equal() did not return void for (''abc'', ''xyz'').'; 151 END IF; 159 PERFORM test.assert_void('test.assert_not_equal(1, 2);'); 160 PERFORM test.assert_void('test.assert_not_equal(''abc''::text, ''xyz'');'); 161 162 -- assert_not_equal() MUST return VOID if only one arg is NULL. 163 PERFORM test.assert_void('test.assert_not_equal(8, NULL);'); 164 PERFORM test.assert_void('test.assert_not_equal(NULL, 7);'); 152 165 153 166 -- assert_not_equal() MUST raise an exception if the given assertion holds. … … 155 168 PERFORM test.assert_raises('test.assert_not_equal(''abc''::text, ''abc'')', 'abc = abc', 'P0001'); 156 169 157 -- assert_not_equal() MUST return VOID if only one arg is NULL.158 SELECT INTO retval * FROM test.assert_not_equal(8, NULL);159 IF retval != '' THEN160 RAISE EXCEPTION 'test.assert_not_equal() did not return void for (8, null).';161 END IF;162 SELECT INTO retval * FROM test.assert_not_equal(NULL, 7);163 IF retval != '' THEN164 RAISE EXCEPTION 'test.assert_not_equal() did not return void for (null, 7).';165 END IF;166 167 170 -- assert_not_equal() MUST raise an exception if both args are NULL. 168 171 PERFORM test.assert_raises('test.assert_not_equal(NULL::int, NULL)', '<NULL> = <NULL>', 'P0001'); … … 178 181 179 182 183 CREATE OR REPLACE FUNCTION test.test_assert_less_than() RETURNS VOID AS $$ 184 -- Assert the correct operation of test.assert_less_than 185 -- module: test_asserts 186 DECLARE 187 retval text; 188 BEGIN 189 -- assert_less_than() MUST return VOID if a < b. 190 PERFORM test.assert_void('test.assert_less_than(1, 2);'); 191 PERFORM test.assert_void('test.assert_less_than(''abc''::text, ''xyz'');'); 192 193 -- assert_less_than() MUST raise an exception if a >= b. 194 PERFORM test.assert_raises('test.assert_less_than(2, 1)', '2 not < 1', 'P0001'); 195 PERFORM test.assert_raises('test.assert_less_than(1, 1)', '1 not < 1', 'P0001'); 196 PERFORM test.assert_raises('test.assert_less_than(''abc''::text, ''abc'')', 197 'abc not < abc', 'P0001'); 198 PERFORM test.assert_raises('test.assert_less_than(''xyz''::text, ''abc'')', 199 'xyz not < abc', 'P0001'); 200 201 -- assert_less_than() MUST raise an exception if either arg is NULL. 202 PERFORM test.assert_raises('test.assert_less_than(NULL::int, NULL)', 203 'Assertion arguments may not be NULL.', 'P0001'); 204 205 -- assert_less_than() will raise an undefined_function exception if the args have different types. 206 -- It would be nice to find a way around this (without writing M x N overloaded funcs). 207 PERFORM test.assert_raises('test.assert_less_than(8, ''abc''::text)', 208 'function test.assert_less_than(integer, text) does not exist', '42883'); 209 210 RAISE EXCEPTION '[OK]'; 211 END; 212 $$ LANGUAGE plpgsql; 213 214 180 215 CREATE OR REPLACE FUNCTION test.test_assert_less_than_or_equal() RETURNS VOID AS $$ 181 216 -- Assert the correct operation of test.assert_less_than_or_equal … … 185 220 BEGIN 186 221 -- assert_less_than_or_equal() MUST return VOID if a <= b. 187 SELECT INTO retval * FROM test.assert_less_than_or_equal(1, 2); 188 IF retval != '' THEN 189 RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (1, 2).'; 190 END IF; 191 SELECT INTO retval * FROM test.assert_less_than_or_equal(1, 1); 192 IF retval != '' THEN 193 RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (1, 1).'; 194 END IF; 195 SELECT INTO retval * FROM test.assert_less_than_or_equal('abc'::text, 'xyz'); 196 IF retval != '' THEN 197 RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (''abc'', ''xyz'').'; 198 END IF; 199 SELECT INTO retval * FROM test.assert_less_than_or_equal('abc'::text, 'abc'); 200 IF retval != '' THEN 201 RAISE EXCEPTION 'test.assert_less_than_or_equal() did not return void for (''abc'', ''abc'').'; 202 END IF; 222 PERFORM test.assert_void('test.assert_less_than_or_equal(1, 2);'); 223 PERFORM test.assert_void('test.assert_less_than_or_equal(1, 1);'); 224 PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''xyz'');'); 225 PERFORM test.assert_void('test.assert_less_than_or_equal(''abc''::text, ''abc'');'); 203 226 204 227 -- assert_less_than_or_equal() MUST raise an exception if a > b. … … 215 238 PERFORM test.assert_raises('test.assert_less_than_or_equal(8, ''abc''::text)', 216 239 'function test.assert_less_than_or_equal(integer, text) does not exist', '42883'); 240 241 RAISE EXCEPTION '[OK]'; 242 END; 243 $$ LANGUAGE plpgsql; 244 245 246 CREATE OR REPLACE FUNCTION test.test_assert_greater_than() RETURNS VOID AS $$ 247 -- Assert the correct operation of test.assert_greater_than 248 -- module: test_asserts 249 DECLARE 250 retval text; 251 BEGIN 252 -- assert_greater_than() MUST return VOID if a > b. 253 PERFORM test.assert_void('test.assert_greater_than(2, 1);'); 254 PERFORM test.assert_void('test.assert_greater_than(''xyz''::text, ''abc'');'); 255 256 -- assert_greater_than() MUST raise an exception if a <= b. 257 PERFORM test.assert_raises('test.assert_greater_than(1, 2)', '1 not > 2', 'P0001'); 258 PERFORM test.assert_raises('test.assert_greater_than(1, 1)', '1 not > 1', 'P0001'); 259 PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''abc'')', 260 'abc not > abc', 'P0001'); 261 PERFORM test.assert_raises('test.assert_greater_than(''abc''::text, ''xyz'')', 262 'abc not > xyz', 'P0001'); 263 264 -- assert_greater_than() MUST raise an exception if either arg is NULL. 265 PERFORM test.assert_raises('test.assert_greater_than(NULL::int, NULL)', 266 'Assertion arguments may not be NULL.', 'P0001'); 267 268 -- assert_greater_than() will raise an undefined_function exception if the args have different types. 269 -- It would be nice to find a way around this (without writing M x N overloaded funcs). 270 PERFORM test.assert_raises('test.assert_greater_than(8, ''abc''::text)', 271 'function test.assert_greater_than(integer, text) does not exist', '42883'); 272 273 RAISE EXCEPTION '[OK]'; 274 END; 275 $$ LANGUAGE plpgsql; 276 277 278 CREATE OR REPLACE FUNCTION test.test_assert_greater_than_or_equal() RETURNS VOID AS $$ 279 -- Assert the correct operation of test.assert_greater_than_or_equal 280 -- module: test_asserts 281 DECLARE 282 retval text; 283 BEGIN 284 -- assert_greater_than_or_equal() MUST return VOID if a >= b. 285 PERFORM test.assert_void('test.assert_greater_than_or_equal(2, 1);'); 286 PERFORM test.assert_void('test.assert_greater_than_or_equal(1, 1);'); 287 PERFORM test.assert_void('test.assert_greater_than_or_equal(''xyz''::text, ''abc'');'); 288 PERFORM test.assert_void('test.assert_greater_than_or_equal(''abc''::text, ''abc'');'); 289 290 -- assert_greater_than_or_equal() MUST raise an exception if a < b. 291 PERFORM test.assert_raises('test.assert_greater_than_or_equal(1, 2)', '1 not >= 2', 'P0001'); 292 PERFORM test.assert_raises('test.assert_greater_than_or_equal(''abc''::text, ''xyz'')', 293 'abc not >= xyz', 'P0001'); 294 295 -- assert_greater_than_or_equal() MUST raise an exception if either arg is NULL. 296 PERFORM test.assert_raises('test.assert_greater_than_or_equal(NULL::int, NULL)', 297 'Assertion arguments may not be NULL.', 'P0001'); 298 299 -- assert_greater_than_or_equal() will raise an undefined_function exception if the args have different types. 300 -- It would be nice to find a way around this (without writing M x N overloaded funcs). 301 PERFORM test.assert_raises('test.assert_greater_than_or_equal(8, ''abc''::text)', 302 'function test.assert_greater_than_or_equal(integer, text) does not exist', '42883'); 217 303 218 304 RAISE EXCEPTION '[OK]';
