BEGIN;
\i test/helpers/tap_setup.sql

CREATE EXTENSION test_factory;
SET search_path=tap;
\i test/helpers/create.sql

SELECT tf.register(
  'customer'
  , array[
    row(
      'insert'
      , $$INSERT INTO customer VALUES (DEFAULT, 'first', 'last' ) RETURNING *$$
    )::tf.test_set
    , row(
      'function'
      , $$SELECT * FROM customer__add( 'func first', 'func last' )$$
    )::tf.test_set
  ]
);
SELECT tf.register(
  'invoice'
  , array[
      row(
        'insert'
        , $$INSERT INTO invoice VALUES(
            DEFAULT
            , (tf.get( NULL::customer, 'insert' )).customer_id
            , current_date
            , current_date + 30
          ) RETURNING *$$
      )::tf.test_set
      , row(
        'function'
        , $$INSERT INTO invoice VALUES(
            DEFAULT
            , (tf.get( NULL::customer, 'function' )).customer_id
            , current_date
            , current_date + 30
          ) RETURNING *$$
      )::tf.test_set
  ]
);

CREATE OR REPLACE FUNCTION a(
    have refcursor
) RETURNS void LANGUAGE plpgsql AS $body$
DECLARE
    have_rec record;
BEGIN
    FETCH have INTO have_rec;
END
$body$;

CREATE OR REPLACE FUNCTION r(
) RETURNS customer LANGUAGE plpgsql AS $body$
BEGIN
END
$body$;

    --OPEN have FOR EXECUTE $$SELECT * FROM tf.get( NULL::invoice, 'base' )$$;

--SELECT no_plan();
--SELECT a();
--SELECT * FROM tf.get( NULL::invoice, 'base' );

DECLARE h CURSOR FOR SELECT * FROM tf.get( NULL::invoice, 'insert' );
--DECLARE w CURSOR FOR VALUES( 1, 1, current_date, current_date + 30 );

--SET client_min_messages=debug5;
SELECT a(
  --$$SELECT * FROM tf.get( NULL::invoice, 'base' )$$
  'h'::refcursor
);
/*
  --, $$VALUES( 1, 1, current_date, current_date + 30 )$$
  , 'w'::refcursor
  , ''
);
*/
