Skip to content

Commit

Permalink
* Add display_type().
Browse files Browse the repository at this point in the history
Replaced all internal use of `format_type()` with it. This is so that schemas are always displayed properly.
  • Loading branch information
theory committed Dec 4, 2009
1 parent 2e657a7 commit c28e715
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 51 deletions.
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -32,6 +32,8 @@ Revision history for pgTAP
implementation by Bob Lunney.
* Added variants of `col_is_type()` to allow a schema name to be specified for
the type.
* Added `display_type()` and replaced all internal use of `format_type()` with
it. This is so that schemas are always displayed properly.

0.22 2009-07-31T00:26:16
-------------------------
Expand Down
10 changes: 10 additions & 0 deletions README.pgtap
Expand Up @@ -3693,6 +3693,16 @@ On PostgreSQL 8.4 and higher, it can take any number of arguments. Lower than
ok(false, 'This should fail)
]);

### `display_type( schema, type_oid, typemod )` ###
### `display_type( type_oid, typemod )` ###

SELECT display_type('public', 'varchar'::regtype, NULL );
SELECT display_type('numeric'::regtype, 196612 );

Like `pg_catalot.format_type()`, except that the returned value is not
prepended with the schema name unless it is passed as the first argument. Used
internally by pgTAP to compare type names, but may be more generally useful.

### `pg_typeof(any)` ###

SELECT pg_typeof(:value);
Expand Down
12 changes: 11 additions & 1 deletion expected/util.out
@@ -1,5 +1,5 @@
\unset ECHO
1..13
1..23
ok 1 - pg_type(int) should work
ok 2 - pg_type(numeric) should work
ok 3 - pg_type(text) should work
Expand All @@ -13,3 +13,13 @@ ok 10 - os_name() should output something like an OS name
ok 11 - findfincs() should return distinct values
ok 12 - pgtap_version() should work
ok 13 - collect_tap() should simply collect tap
ok 14 - display_type(int4)
ok 15 - display_type(numeric)
ok 16 - display_type(numeric, typmod)
ok 17 - display_type("char")
ok 18 - display_type(char)
ok 19 - display_type(timestamp)
ok 20 - display_type(timestamptz)
ok 21 - display_type(foo, int4)
ok 22 - display_type(HEY, numeric)
ok 23 - display_type(t z, int4)
98 changes: 50 additions & 48 deletions pgtap.sql.in
Expand Up @@ -1081,9 +1081,21 @@ RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, 'Column ' || quote_ident($1) || '.' || quote_ident($2) || ' should allow NULL', false );
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION display_type ( OID, INTEGER )
RETURNS TEXT AS $$
SELECT $1::regtype
|| COALESCE(substring(pg_catalog.format_type($1, $2), '[(][^)]+[)]$'), '')
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION display_type ( NAME, OID, INTEGER )
RETURNS TEXT AS $$
SELECT CASE WHEN $1 IS NULL THEN '' ELSE quote_ident($1) || '.' END
|| display_type($2, $3)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _get_col_type ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT pg_catalog.format_type(a.atttypid, a.atttypmod)
SELECT display_type(a.atttypid, a.atttypmod)
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON n.oid = c.relnamespace
JOIN pg_catalog.pg_attribute a ON c.oid = a.attrelid
Expand All @@ -1096,7 +1108,7 @@ $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _get_col_type ( NAME, NAME )
RETURNS TEXT AS $$
SELECT pg_catalog.format_type(a.atttypid, a.atttypmod)
SELECT display_type(a.atttypid, a.atttypmod)
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
WHERE pg_table_is_visible(c.oid)
Expand All @@ -1109,7 +1121,7 @@ $$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _get_col_ns_type ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT quote_ident(tn.nspname) || '.' || pg_catalog.format_type(a.atttypid, a.atttypmod)
SELECT display_type(tn.nspname, a.atttypid, a.atttypmod)
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON n.oid = c.relnamespace
JOIN pg_catalog.pg_attribute a ON c.oid = a.attrelid
Expand Down Expand Up @@ -1325,7 +1337,7 @@ BEGIN

RETURN _def_is(
pg_catalog.pg_get_expr(d.adbin, d.adrelid),
pg_catalog.format_type(a.atttypid, a.atttypmod),
display_type(a.atttypid, a.atttypmod),
$4, $5
)
FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c, pg_catalog.pg_attribute a,
Expand Down Expand Up @@ -1359,7 +1371,7 @@ BEGIN

RETURN _def_is(
pg_catalog.pg_get_expr(d.adbin, d.adrelid),
pg_catalog.format_type(a.atttypid, a.atttypmod),
display_type(a.atttypid, a.atttypmod),
$3, $4
)
FROM pg_catalog.pg_class c, pg_catalog.pg_attribute a, pg_catalog.pg_attrdef d
Expand Down Expand Up @@ -3606,8 +3618,8 @@ RETURNS BOOLEAN AS $$
FROM pg_catalog.pg_cast c
JOIN pg_catalog.pg_proc p ON c.castfunc = p.oid
JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
WHERE pg_catalog.format_type(castsource, NULL) = $1
AND pg_catalog.format_type(casttarget, NULL) = $2
WHERE display_type(castsource, NULL) = $1
AND display_type(casttarget, NULL) = $2
AND n.nspname = $3
AND p.proname = $4
);
Expand All @@ -3619,8 +3631,8 @@ RETURNS BOOLEAN AS $$
SELECT TRUE
FROM pg_catalog.pg_cast c
JOIN pg_catalog.pg_proc p ON c.castfunc = p.oid
WHERE pg_catalog.format_type(castsource, NULL) = $1
AND pg_catalog.format_type(casttarget, NULL) = $2
WHERE display_type(castsource, NULL) = $1
AND display_type(casttarget, NULL) = $2
AND p.proname = $3
);
$$ LANGUAGE SQL;
Expand All @@ -3630,8 +3642,8 @@ RETURNS BOOLEAN AS $$
SELECT EXISTS (
SELECT TRUE
FROM pg_catalog.pg_cast c
WHERE pg_catalog.format_type(castsource, NULL) = $1
AND pg_catalog.format_type(casttarget, NULL) = $2
WHERE display_type(castsource, NULL) = $1
AND display_type(casttarget, NULL) = $2
);
$$ LANGUAGE SQL;

Expand Down Expand Up @@ -3746,8 +3758,8 @@ CREATE OR REPLACE FUNCTION _get_context( NAME, NAME )
RETURNS "char" AS $$
SELECT c.castcontext
FROM pg_catalog.pg_cast c
WHERE pg_catalog.format_type(castsource, NULL) = $1
AND pg_catalog.format_type(casttarget, NULL) = $2
WHERE display_type(castsource, NULL) = $1
AND display_type(casttarget, NULL) = $2
$$ LANGUAGE SQL;

-- cast_context_is( source_type, target_type, context, description )
Expand Down Expand Up @@ -3787,10 +3799,10 @@ RETURNS BOOLEAN AS $$
WHERE n.nspname = $2
AND o.oprname = $3
AND CASE o.oprkind WHEN 'l' THEN $1 IS NULL
ELSE pg_catalog.format_type(o.oprleft, NULL) = $1 END
ELSE display_type(o.oprleft, NULL) = $1 END
AND CASE o.oprkind WHEN 'r' THEN $4 IS NULL
ELSE pg_catalog.format_type(o.oprright, NULL) = $4 END
AND pg_catalog.format_type(o.oprresult, NULL) = $5
ELSE display_type(o.oprright, NULL) = $4 END
AND display_type(o.oprresult, NULL) = $5
);
$$ LANGUAGE SQL;

Expand All @@ -3802,10 +3814,10 @@ RETURNS BOOLEAN AS $$
WHERE pg_catalog.pg_operator_is_visible(o.oid)
AND o.oprname = $2
AND CASE o.oprkind WHEN 'l' THEN $1 IS NULL
ELSE pg_catalog.format_type(o.oprleft, NULL) = $1 END
ELSE display_type(o.oprleft, NULL) = $1 END
AND CASE o.oprkind WHEN 'r' THEN $3 IS NULL
ELSE pg_catalog.format_type(o.oprright, NULL) = $3 END
AND pg_catalog.format_type(o.oprresult, NULL) = $4
ELSE display_type(o.oprright, NULL) = $3 END
AND display_type(o.oprresult, NULL) = $4
);
$$ LANGUAGE SQL;

Expand All @@ -3817,9 +3829,9 @@ RETURNS BOOLEAN AS $$
WHERE pg_catalog.pg_operator_is_visible(o.oid)
AND o.oprname = $2
AND CASE o.oprkind WHEN 'l' THEN $1 IS NULL
ELSE pg_catalog.format_type(o.oprleft, NULL) = $1 END
ELSE display_type(o.oprleft, NULL) = $1 END
AND CASE o.oprkind WHEN 'r' THEN $3 IS NULL
ELSE pg_catalog.format_type(o.oprright, NULL) = $3 END
ELSE display_type(o.oprright, NULL) = $3 END
);
$$ LANGUAGE SQL;

Expand Down Expand Up @@ -5844,7 +5856,7 @@ $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION _temptypes( TEXT )
RETURNS TEXT AS $$
SELECT array_to_string(ARRAY(
SELECT pg_catalog.format_type(a.atttypid, a.atttypmod)
SELECT display_type(a.atttypid, a.atttypmod)
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON a.attrelid = c.oid
WHERE c.relname = $1
Expand Down Expand Up @@ -6800,7 +6812,6 @@ RETURNS TEXT AS $$
SELECT _types_are( $1, 'Search path ' || pg_catalog.current_setting('search_path') || ' should have the correct enums', ARRAY['e'] );
$$ LANGUAGE SQL;


-- _dexists( schema, domain )
CREATE OR REPLACE FUNCTION _dexists ( NAME, NAME )
RETURNS BOOLEAN AS $$
Expand All @@ -6823,9 +6834,9 @@ RETURNS BOOLEAN AS $$
);
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _get_dtype( NAME, NAME )
RETURNS TEXT[] AS $$
SELECT ARRAY[quote_ident(tn.nspname), pg_catalog.format_type(t.oid, t.typtypmod)]
CREATE OR REPLACE FUNCTION _get_dtype( NAME, TEXT, BOOLEAN )
RETURNS TEXT AS $$
SELECT display_type(CASE WHEN $3 THEN tn.nspname ELSE NULL END, t.oid, t.typtypmod)
FROM pg_catalog.pg_type d
JOIN pg_catalog.pg_namespace dn ON d.typnamespace = dn.oid
JOIN pg_catalog.pg_type t ON d.typbasetype = t.oid
Expand All @@ -6838,7 +6849,7 @@ $$ LANGUAGE sql;

CREATE OR REPLACE FUNCTION _get_dtype( NAME )
RETURNS TEXT AS $$
SELECT pg_catalog.format_type(t.oid, t.typtypmod)
SELECT display_type(t.oid, t.typtypmod)
FROM pg_catalog.pg_type d
JOIN pg_catalog.pg_type t ON d.typbasetype = t.oid
WHERE d.typisdefined
Expand All @@ -6850,20 +6861,16 @@ $$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION domain_type_is( NAME, TEXT, NAME, TEXT, TEXT )
RETURNS TEXT AS $$
DECLARE
actual_type TEXT[] := _get_dtype($1, $2);
actual_type TEXT := _get_dtype($1, $2, true);
BEGIN
IF actual_type[1] IS NULL THEN
IF actual_type IS NULL THEN
RETURN fail( $5 ) || E'\n' || diag (
' Domain ' || quote_ident($1) || '.' || $2
|| ' does not exist'
);
END IF;

RETURN is(
actual_type[1] || '.' || actual_type[2],
quote_ident($3) || '.' || LOWER($4),
$5
);
RETURN is( actual_type, quote_ident($3) || '.' || LOWER($4), $5 );
END;
$$ LANGUAGE plpgsql;

Expand All @@ -6881,16 +6888,16 @@ $$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION domain_type_is( NAME, TEXT, TEXT, TEXT )
RETURNS TEXT AS $$
DECLARE
actual_type TEXT[] := _get_dtype($1, $2);
actual_type TEXT := _get_dtype($1, $2, false);
BEGIN
IF actual_type[1] IS NULL THEN
IF actual_type IS NULL THEN
RETURN fail( $4 ) || E'\n' || diag (
' Domain ' || quote_ident($1) || '.' || $2
|| ' does not exist'
);
END IF;

RETURN is( actual_type[2], LOWER($3), $4 );
RETURN is( actual_type, LOWER($3), $4 );
END;
$$ LANGUAGE plpgsql;

Expand Down Expand Up @@ -6933,21 +6940,16 @@ $$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION domain_type_isnt( NAME, TEXT, NAME, TEXT, TEXT )
RETURNS TEXT AS $$
DECLARE
actual_type TEXT[] := _get_dtype($1, $2);
actual_type TEXT := _get_dtype($1, $2, true);
BEGIN
IF actual_type[1] IS NULL THEN
IF actual_type IS NULL THEN
RETURN fail( $5 ) || E'\n' || diag (
' Domain ' || quote_ident($1) || '.' || $2
|| ' does not exist'
);
END IF;

RETURN isnt(
actual_type[1] || '.' || actual_type[2],
quote_ident($3) || '.' || LOWER($4),

$5
);
RETURN isnt( actual_type, quote_ident($3) || '.' || LOWER($4), $5 );
END;
$$ LANGUAGE plpgsql;

Expand All @@ -6965,16 +6967,16 @@ $$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION domain_type_isnt( NAME, TEXT, TEXT, TEXT )
RETURNS TEXT AS $$
DECLARE
actual_type TEXT[] := _get_dtype($1, $2);
actual_type TEXT := _get_dtype($1, $2, false);
BEGIN
IF actual_type[1] IS NULL THEN
IF actual_type IS NULL THEN
RETURN fail( $4 ) || E'\n' || diag (
' Domain ' || quote_ident($1) || '.' || $2
|| ' does not exist'
);
END IF;

RETURN isnt( actual_type[2], LOWER($3), $4 );
RETURN isnt( actual_type, LOWER($3), $4 );
END;
$$ LANGUAGE plpgsql;

Expand Down
16 changes: 15 additions & 1 deletion sql/util.sql
@@ -1,7 +1,7 @@
\unset ECHO
\i test_setup.sql

SELECT plan(13);
SELECT plan(23);
--SELECT * FROM no_plan();

SELECT is( pg_typeof(42), 'integer', 'pg_type(int) should work' );
Expand Down Expand Up @@ -71,6 +71,20 @@ baz',
'collect_tap() should simply collect tap'
);

/****************************************************************************/
-- Test display_type().
SELECT is( display_type('int4'::regtype, NULL), 'integer', 'display_type(int4)');
SELECT is( display_type('numeric'::regtype, NULL), 'numeric', 'display_type(numeric)');
SELECT is( display_type('numeric'::regtype, 196612), 'numeric(3,0)', 'display_type(numeric, typmod)');
SELECT is( display_type('"char"'::regtype, NULL), '"char"', 'display_type("char")');
SELECT is( display_type('char'::regtype, NULL), 'character', 'display_type(char)');
SELECT is( display_type('timestamp'::regtype, NULL), 'timestamp without time zone', 'display_type(timestamp)');
SELECT is( display_type('timestamptz'::regtype, NULL), 'timestamp with time zone', 'display_type(timestamptz)');

SELECT is( display_type('foo', 'int4'::regtype, NULL), 'foo.integer', 'display_type(foo, int4)');
SELECT is( display_type('HEY', 'numeric'::regtype, NULL), '"HEY".numeric', 'display_type(HEY, numeric)');
SELECT is( display_type('t z', 'int4'::regtype, NULL), '"t z".integer', 'display_type(t z, int4)');

/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();
Expand Down
4 changes: 3 additions & 1 deletion uninstall_pgtap.sql.in
Expand Up @@ -12,7 +12,7 @@ DROP FUNCTION domain_type_is( NAME, TEXT, TEXT, TEXT );
DROP FUNCTION domain_type_is( NAME, TEXT, NAME, TEXT );
DROP FUNCTION domain_type_is( NAME, TEXT, NAME, TEXT, TEXT );
DROP FUNCTION _get_dtype( NAME );
DROP FUNCTION _get_dtype( NAME, NAME );
DROP FUNCTION _get_dtype( NAME, TEXT, BOOLEAN );
DROP FUNCTION _dexists ( NAME );
DROP FUNCTION _dexists ( NAME, NAME );
DROP FUNCTION enums_are ( NAME[] );
Expand Down Expand Up @@ -570,6 +570,8 @@ DROP FUNCTION col_type_is ( NAME, NAME, NAME, NAME, TEXT, TEXT );
DROP FUNCTION _get_col_ns_type ( NAME, NAME, NAME );
DROP FUNCTION _get_col_type ( NAME, NAME );
DROP FUNCTION _get_col_type ( NAME, NAME, NAME );
DROP FUNCTION display_type ( NAME, OID, INTEGER );
DROP FUNCTION display_type ( OID, INTEGER );
DROP FUNCTION col_is_null ( NAME, NAME );
DROP FUNCTION col_is_null ( NAME, NAME, NAME );
DROP FUNCTION col_is_null ( NAME, NAME, NAME, TEXT );
Expand Down

0 comments on commit c28e715

Please sign in to comment.