From 68c521eb92c3515e3306f51a7fd3f32d16c97524 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 1 Mar 2016 20:01:07 -0500
Subject: [PATCH] Improve coverage of pltcl regression tests.

Test composite-type arguments and the argisnull and spi_lastoid Tcl
commmands.  This stuff was not covered before, but needs to be exercised
since the upcoming Tcl object-conversion patch changes these code paths
(and broke at least one of them).
---
 src/pl/tcl/expected/pltcl_queries.out   | 47 +++++++++++++++++++++++++
 src/pl/tcl/expected/pltcl_queries_1.out | 47 +++++++++++++++++++++++++
 src/pl/tcl/expected/pltcl_setup.out     | 13 +++++++
 src/pl/tcl/sql/pltcl_queries.sql        | 15 ++++++++
 src/pl/tcl/sql/pltcl_setup.sql          | 18 ++++++++++
 5 files changed, 140 insertions(+)

diff --git a/src/pl/tcl/expected/pltcl_queries.out b/src/pl/tcl/expected/pltcl_queries.out
index b585736f711..ba250a218b2 100644
--- a/src/pl/tcl/expected/pltcl_queries.out
+++ b/src/pl/tcl/expected/pltcl_queries.out
@@ -256,3 +256,50 @@ NOTICE:  TG_table_name: trigger_test
 NOTICE:  TG_table_schema: public
 NOTICE:  TG_when: BEFORE
 NOTICE:  args: {23 skidoo}
+-- Test composite-type arguments
+select tcl_composite_arg_ref1(row('tkey', 42, 'ref2'));
+ tcl_composite_arg_ref1 
+------------------------
+                     42
+(1 row)
+
+select tcl_composite_arg_ref2(row('tkey', 42, 'ref2'));
+ tcl_composite_arg_ref2 
+------------------------
+ ref2                
+(1 row)
+
+-- Test argisnull primitive
+select tcl_argisnull('foo');
+ tcl_argisnull 
+---------------
+ f
+(1 row)
+
+select tcl_argisnull('');
+ tcl_argisnull 
+---------------
+ f
+(1 row)
+
+select tcl_argisnull(null);
+ tcl_argisnull 
+---------------
+ t
+(1 row)
+
+-- Test spi_lastoid primitive
+create temp table t1 (f1 int);
+select tcl_lastoid('t1');
+ tcl_lastoid 
+-------------
+           0
+(1 row)
+
+create temp table t2 (f1 int) with oids;
+select tcl_lastoid('t2') > 0;
+ ?column? 
+----------
+ t
+(1 row)
+
diff --git a/src/pl/tcl/expected/pltcl_queries_1.out b/src/pl/tcl/expected/pltcl_queries_1.out
index e3fd24d6b10..20efdb4104c 100644
--- a/src/pl/tcl/expected/pltcl_queries_1.out
+++ b/src/pl/tcl/expected/pltcl_queries_1.out
@@ -256,3 +256,50 @@ NOTICE:  TG_table_name: trigger_test
 NOTICE:  TG_table_schema: public
 NOTICE:  TG_when: BEFORE
 NOTICE:  args: {23 skidoo}
+-- Test composite-type arguments
+select tcl_composite_arg_ref1(row('tkey', 42, 'ref2'));
+ tcl_composite_arg_ref1 
+------------------------
+                     42
+(1 row)
+
+select tcl_composite_arg_ref2(row('tkey', 42, 'ref2'));
+ tcl_composite_arg_ref2 
+------------------------
+ ref2                
+(1 row)
+
+-- Test argisnull primitive
+select tcl_argisnull('foo');
+ tcl_argisnull 
+---------------
+ f
+(1 row)
+
+select tcl_argisnull('');
+ tcl_argisnull 
+---------------
+ f
+(1 row)
+
+select tcl_argisnull(null);
+ tcl_argisnull 
+---------------
+ t
+(1 row)
+
+-- Test spi_lastoid primitive
+create temp table t1 (f1 int);
+select tcl_lastoid('t1');
+ tcl_lastoid 
+-------------
+           0
+(1 row)
+
+create temp table t2 (f1 int) with oids;
+select tcl_lastoid('t2') > 0;
+ ?column? 
+----------
+ t
+(1 row)
+
diff --git a/src/pl/tcl/expected/pltcl_setup.out b/src/pl/tcl/expected/pltcl_setup.out
index 4183c14b28a..e11718c64b3 100644
--- a/src/pl/tcl/expected/pltcl_setup.out
+++ b/src/pl/tcl/expected/pltcl_setup.out
@@ -398,6 +398,19 @@ create trigger dta1_before before insert or update on T_dta1
 create trigger dta2_before before insert or update on T_dta2
 	for each row execute procedure
 	check_primkey('ref1', 'ref2', 'T_pkey2', 'key1', 'key2');
+create function tcl_composite_arg_ref1(T_dta1) returns int as '
+    return $1(ref1)
+' language pltcl;
+create function tcl_composite_arg_ref2(T_dta1) returns text as '
+    return $1(ref2)
+' language pltcl;
+create function tcl_argisnull(text) returns bool as '
+    argisnull 1
+' language pltcl;
+create function tcl_lastoid(tabname text) returns int8 as '
+    spi_exec "insert into $1 default values"
+    spi_lastoid
+' language pltcl;
 create function tcl_int4add(int4,int4) returns int4 as '
     return [expr $1 + $2]
 ' language pltcl;
diff --git a/src/pl/tcl/sql/pltcl_queries.sql b/src/pl/tcl/sql/pltcl_queries.sql
index ee711d5170a..205bc4266ae 100644
--- a/src/pl/tcl/sql/pltcl_queries.sql
+++ b/src/pl/tcl/sql/pltcl_queries.sql
@@ -82,3 +82,18 @@ delete from trigger_test_view;
 
 update trigger_test set v = 'update' where i = 1;
 delete from trigger_test;
+
+-- Test composite-type arguments
+select tcl_composite_arg_ref1(row('tkey', 42, 'ref2'));
+select tcl_composite_arg_ref2(row('tkey', 42, 'ref2'));
+
+-- Test argisnull primitive
+select tcl_argisnull('foo');
+select tcl_argisnull('');
+select tcl_argisnull(null);
+
+-- Test spi_lastoid primitive
+create temp table t1 (f1 int);
+select tcl_lastoid('t1');
+create temp table t2 (f1 int) with oids;
+select tcl_lastoid('t2') > 0;
diff --git a/src/pl/tcl/sql/pltcl_setup.sql b/src/pl/tcl/sql/pltcl_setup.sql
index 84629963229..53358ea361f 100644
--- a/src/pl/tcl/sql/pltcl_setup.sql
+++ b/src/pl/tcl/sql/pltcl_setup.sql
@@ -429,6 +429,24 @@ create trigger dta2_before before insert or update on T_dta2
 	check_primkey('ref1', 'ref2', 'T_pkey2', 'key1', 'key2');
 
 
+create function tcl_composite_arg_ref1(T_dta1) returns int as '
+    return $1(ref1)
+' language pltcl;
+
+create function tcl_composite_arg_ref2(T_dta1) returns text as '
+    return $1(ref2)
+' language pltcl;
+
+create function tcl_argisnull(text) returns bool as '
+    argisnull 1
+' language pltcl;
+
+create function tcl_lastoid(tabname text) returns int8 as '
+    spi_exec "insert into $1 default values"
+    spi_lastoid
+' language pltcl;
+
+
 create function tcl_int4add(int4,int4) returns int4 as '
     return [expr $1 + $2]
 ' language pltcl;
-- 
GitLab