Skip to content

Commit

Permalink
Test is_partition_of().
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Nov 6, 2017
1 parent 9b6994a commit c1a40f9
Show file tree
Hide file tree
Showing 5 changed files with 302 additions and 2 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -25,6 +25,7 @@ Revision history for pgTAP
* Added partition-testing assert functions:
+ `is_partitioned()`
+ `isnt_partitioned()`
+ `is_partition_of()`

0.97.0 2016-11-28T22:18:29Z
---------------------------
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Expand Up @@ -79,6 +79,12 @@ TESTS := $(filter-out test/sql/enumtap.sql sql/valueset.sql,$(TESTS))
REGRESS := $(filter-out enumtap valueset,$(REGRESS))
endif

# Partition tests tests not supported by 9.x and earlier.
ifeq ($(shell echo $(VERSION) | grep -qE "[89][.]" && echo yes || echo no),yes)
TESTS := $(filter-out test/sql/partitions.sql,$(TESTS))
REGRESS := $(filter-out partitions,$(REGRESS))
endif

# Determine the OS. Borrowed from Perl's Configure.
OSNAME := $(shell $(SHELL) ./getos.sh)

Expand Down
4 changes: 2 additions & 2 deletions sql/pgtap.sql.in
Expand Up @@ -9943,7 +9943,7 @@ CREATE OR REPLACE FUNCTION is_partition_of ( NAME, NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT ok(
_partof($1, $2, $3, $4),
'Table ' || quote_ident($1) || '.' || quote_ident($2) || ' should be a partition of'
'Table ' || quote_ident($1) || '.' || quote_ident($2) || ' should be a partition of '
|| quote_ident($3) || '.' || quote_ident($4)
);
$$ LANGUAGE sql;
Expand All @@ -9959,6 +9959,6 @@ CREATE OR REPLACE FUNCTION is_partition_of ( NAME, NAME )
RETURNS TEXT AS $$
SELECT ok(
_partof($1, $2),
'Table ' || quote_ident($1) || ' should be a partition of' || quote_ident($2)
'Table ' || quote_ident($1) || ' should be a partition of ' || quote_ident($2)
);
$$ LANGUAGE sql;
68 changes: 68 additions & 0 deletions test/expected/partitions.out
@@ -0,0 +1,68 @@
\unset ECHO
1..66
ok 1 - is_partition_of( csch, ctab, psch, ptab, desc ) should pass
ok 2 - is_partition_of( csch, ctab, psch, ptab, desc ) should have the proper description
ok 3 - is_partition_of( csch, ctab, psch, ptab, desc ) should have the proper diagnostics
ok 4 - is_partition_of( csch, ctab, psch, ptab ) should pass
ok 5 - is_partition_of( csch, ctab, psch, ptab ) should have the proper description
ok 6 - is_partition_of( csch, ctab, psch, ptab ) should have the proper diagnostics
ok 7 - is_partition_of( ctab, ptab, desc ) should pass
ok 8 - is_partition_of( ctab, ptab, desc ) should have the proper description
ok 9 - is_partition_of( ctab, ptab, desc ) should have the proper diagnostics
ok 10 - is_partition_of( ctab, ptab ) should pass
ok 11 - is_partition_of( ctab, ptab ) should have the proper description
ok 12 - is_partition_of( ctab, ptab ) should have the proper diagnostics
ok 13 - is_partition_of( csch, non-part ctab, psch, non-part ptab, desc ) should fail
ok 14 - is_partition_of( csch, non-part ctab, psch, non-part ptab, desc ) should have the proper description
ok 15 - is_partition_of( csch, non-part ctab, psch, non-part ptab, desc ) should have the proper diagnostics
ok 16 - is_partition_of( non-part ctab, non-part ptab, desc ) should fail
ok 17 - is_partition_of( non-part ctab, non-part ptab, desc ) should have the proper description
ok 18 - is_partition_of( non-part ctab, non-part ptab, desc ) should have the proper diagnostics
ok 19 - is_partition_of( csch, non-part ctab, psch, ptab, desc ) should fail
ok 20 - is_partition_of( csch, non-part ctab, psch, ptab, desc ) should have the proper description
ok 21 - is_partition_of( csch, non-part ctab, psch, ptab, desc ) should have the proper diagnostics
ok 22 - is_partition_of( non-part ctab, ptab, desc ) should fail
ok 23 - is_partition_of( non-part ctab, ptab, desc ) should have the proper description
ok 24 - is_partition_of( non-part ctab, ptab, desc ) should have the proper diagnostics
ok 25 - is_partition_of( csch, ctab, psch, non-part ptab, desc ) should fail
ok 26 - is_partition_of( csch, ctab, psch, non-part ptab, desc ) should have the proper description
ok 27 - is_partition_of( csch, ctab, psch, non-part ptab, desc ) should have the proper diagnostics
ok 28 - is_partition_of( ctab, non-part ptab, desc ) should fail
ok 29 - is_partition_of( ctab, non-part ptab, desc ) should have the proper description
ok 30 - is_partition_of( ctab, non-part ptab, desc ) should have the proper diagnostics
ok 31 - is_partition_of( priv csch, ctab, priv psch, ptab, desc ) should pass
ok 32 - is_partition_of( priv csch, ctab, priv psch, ptab, desc ) should have the proper description
ok 33 - is_partition_of( priv csch, ctab, priv psch, ptab, desc ) should have the proper diagnostics
ok 34 - is_partition_of( priv ctab, priv ptab, desc ) should fail
ok 35 - is_partition_of( priv ctab, priv ptab, desc ) should have the proper description
ok 36 - is_partition_of( priv ctab, priv ptab, desc ) should have the proper diagnostics
ok 37 - is_partition_of( priv csch, ctab, psch, ptab, desc ) should pass
ok 38 - is_partition_of( priv csch, ctab, psch, ptab, desc ) should have the proper description
ok 39 - is_partition_of( priv csch, ctab, psch, ptab, desc ) should have the proper diagnostics
ok 40 - is_partition_of( priv ctab, ptab, desc ) should fail
ok 41 - is_partition_of( priv ctab, ptab, desc ) should have the proper description
ok 42 - is_partition_of( priv ctab, ptab, desc ) should have the proper diagnostics
ok 43 - is_partition_of( csch, ctab, priv psch, ptab, desc ) should pass
ok 44 - is_partition_of( csch, ctab, priv psch, ptab, desc ) should have the proper description
ok 45 - is_partition_of( csch, ctab, priv psch, ptab, desc ) should have the proper diagnostics
ok 46 - is_partition_of( ctab, priv ptab, desc ) should fail
ok 47 - is_partition_of( ctab, priv ptab, desc ) should have the proper description
ok 48 - is_partition_of( ctab, priv ptab, desc ) should have the proper diagnostics
ok 49 - is_partition_of( csch, non-ctab, psch, non-ptab, desc ) should fail
ok 50 - is_partition_of( csch, non-ctab, psch, non-ptab, desc ) should have the proper description
ok 51 - is_partition_of( csch, non-ctab, psch, non-ptab, desc ) should have the proper diagnostics
ok 52 - is_partition_of( non-ctab, non-ptab, desc ) should fail
ok 53 - is_partition_of( non-ctab, non-ptab, desc ) should have the proper description
ok 54 - is_partition_of( non-ctab, non-ptab, desc ) should have the proper diagnostics
ok 55 - is_partition_of( csch, ctab, psch, non-ptab, desc ) should fail
ok 56 - is_partition_of( csch, ctab, psch, non-ptab, desc ) should have the proper description
ok 57 - is_partition_of( csch, ctab, psch, non-ptab, desc ) should have the proper diagnostics
ok 58 - is_partition_of( ctab, non-ptab, desc ) should fail
ok 59 - is_partition_of( ctab, non-ptab, desc ) should have the proper description
ok 60 - is_partition_of( ctab, non-ptab, desc ) should have the proper diagnostics
ok 61 - is_partition_of( csch, non-ctab, psch, ptab, desc ) should fail
ok 62 - is_partition_of( csch, non-ctab, psch, ptab, desc ) should have the proper description
ok 63 - is_partition_of( csch, non-ctab, psch, ptab, desc ) should have the proper diagnostics
ok 64 - is_partition_of( non-ctab, ptab, desc ) should fail
ok 65 - is_partition_of( non-ctab, ptab, desc ) should have the proper description
ok 66 - is_partition_of( non-ctab, ptab, desc ) should have the proper diagnostics
225 changes: 225 additions & 0 deletions test/sql/partitions.sql
@@ -0,0 +1,225 @@
\unset ECHO
\i test/setup.sql

SELECT plan(66);
--SELECT * FROM no_plan();

-- This will be rolled back. :-)
SET client_min_messages = warning;

-- Create inherited tables (not partitions).
CREATE TABLE public.parent(id INT PRIMARY KEY);
CREATE TABLE public.child(id INT PRIMARY KEY) INHERITS (public.parent);

-- Create a partitioned table with two partitions.
CREATE TABLE public.parted(id INT NOT NULL) PARTITION BY RANGE (id);
CREATE TABLE public.part1 PARTITION OF public.parted FOR VALUES FROM (1) TO (10);
CREATE TABLE public.part2 PARTITION OF public.parted FOR VALUES FROM (11) TO (20);

-- Create partitions outside of search path.
CREATE SCHEMA hide;
CREATE TABLE hide.hidden_parted(id INT NOT NULL) PARTITION BY RANGE (id);
CREATE TABLE hide.hidden_part1 PARTITION OF hide.hidden_parted FOR VALUES FROM (1) TO (10);
CREATE TABLE hide.hidden_part2 PARTITION OF hide.hidden_parted FOR VALUES FROM (11) TO (20);

-- Put a partition for the public table in the hidden schema.
CREATE TABLE hide.part3 PARTITION OF public.parted FOR VALUES FROM (21) TO (30);

-- Put a partition for the hidden table in the public schema.
CREATE TABLE public.not_hidden_part3 PARTITION OF hide.hidden_parted
FOR VALUES FROM (21) TO (30);

RESET client_min_messages;

/****************************************************************************/
-- Test is_partition_of().
SELECT * FROM check_test(
is_partition_of( 'public', 'part1', 'public', 'parted', 'whatevs' ),
true,
'is_partition_of( csch, ctab, psch, ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'public', 'part1', 'public', 'parted' ),
true,
'is_partition_of( csch, ctab, psch, ptab )',
'Table public.part1 should be a partition of public.parted',
''
);

SELECT * FROM check_test(
is_partition_of( 'part1', 'parted', 'whatevs' ),
true,
'is_partition_of( ctab, ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'part1', 'parted' ),
true,
'is_partition_of( ctab, ptab )',
'Table part1 should be a partition of parted',
''
);

-- is_partition_of() should fail for inherited but not partitioned tables.
SELECT * FROM check_test(
is_partition_of( 'public', 'child', 'public', 'parent', 'whatevs' ),
false,
'is_partition_of( csch, non-part ctab, psch, non-part ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'child', 'parent', 'whatevs' ),
false,
'is_partition_of( non-part ctab, non-part ptab, desc )',
'whatevs',
''
);

-- is_partition_of() should fail for parted table and non-part child.
SELECT * FROM check_test(
is_partition_of( 'public', 'child', 'public', 'parted', 'whatevs' ),
false,
'is_partition_of( csch, non-part ctab, psch, ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'child', 'parted', 'whatevs' ),
false,
'is_partition_of( non-part ctab, ptab, desc )',
'whatevs',
''
);

-- is_partition_of() should fail for partition child but wrong parent.
SELECT * FROM check_test(
is_partition_of( 'public', 'part1', 'public', 'parent', 'whatevs' ),
false,
'is_partition_of( csch, ctab, psch, non-part ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'part1', 'parent', 'whatevs' ),
false,
'is_partition_of( ctab, non-part ptab, desc )',
'whatevs',
''
);

-- Should find tables outside search path for explicit schema.
SELECT * FROM check_test(
is_partition_of( 'hide', 'hidden_part1', 'hide', 'hidden_parted', 'whatevs' ),
true,
'is_partition_of( priv csch, ctab, priv psch, ptab, desc )',
'whatevs',
''
);

-- But not when the schema is not specified.
SELECT * FROM check_test(
is_partition_of( 'hidden_part1', 'hidden_parted', 'whatevs' ),
false,
'is_partition_of( priv ctab, priv ptab, desc )',
'whatevs',
''
);

-- Should find explicit hidden table for public partition.
SELECT * FROM check_test(
is_partition_of( 'hide', 'part3', 'public', 'parted', 'whatevs' ),
true,
'is_partition_of( priv csch, ctab, psch, ptab, desc )',
'whatevs',
''
);

-- But still not when schemas not specified.
SELECT * FROM check_test(
is_partition_of( 'part3', 'hidden', 'whatevs' ),
false,
'is_partition_of( priv ctab, ptab, desc )',
'whatevs',
''
);

-- Should find public partition for hidden parent.
SELECT * FROM check_test(
is_partition_of( 'public', 'not_hidden_part3', 'hide', 'hidden_parted', 'whatevs' ),
true,
'is_partition_of( csch, ctab, priv psch, ptab, desc )',
'whatevs',
''
);

-- But not if no schemas are specified.
SELECT * FROM check_test(
is_partition_of( 'not_hidden_part3', 'hidden_parted', 'whatevs' ),
false,
'is_partition_of( ctab, priv ptab, desc )',
'whatevs',
''
);

-- And of course, it should not work for nonexistent partitions.
SELECT * FROM check_test(
is_partition_of( 'public', 'nonesuch', 'public', 'nothing', 'whatevs' ),
false,
'is_partition_of( csch, non-ctab, psch, non-ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'nonesuch', 'nothing', 'whatevs' ),
false,
'is_partition_of( non-ctab, non-ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'public', 'part1', 'public', 'nothing', 'whatevs' ),
false,
'is_partition_of( csch, ctab, psch, non-ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'nonesuch', 'part1', 'whatevs' ),
false,
'is_partition_of( ctab, non-ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'public', 'nonesuch', 'public', 'parted', 'whatevs' ),
false,
'is_partition_of( csch, non-ctab, psch, ptab, desc )',
'whatevs',
''
);

SELECT * FROM check_test(
is_partition_of( 'nonesuch', 'parted', 'whatevs' ),
false,
'is_partition_of( non-ctab, ptab, desc )',
'whatevs',
''
);

/****************************************************************************/
-- Finish the tests and clean up.
SELECT * FROM finish();
ROLLBACK;

0 comments on commit c1a40f9

Please sign in to comment.