How can I include sql file in pgTAP unittest?

From: Stéphane Klein <contact(at)stephane-klein(dot)info>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: How can I include sql file in pgTAP unittest?
Date: 2018-03-02 09:17:22
Message-ID: CADKxhpd0Y4oJHy-O=TAdnzTgs-UB1H5FShmx5qWQuKT3FmLndQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

context: I would like to write UnitTest to test pgSQL triggers which use
postgres_fdw extension.
I use pgTAP <http://pgtap.org/> to write this UnitTest (I use this Docker
environment poc-postgresql-pgTAP
<https://github.com/harobed/poc-postgresql-pgTAP>).

All works perfectly with this test file:

BEGIN;
SELECT plan(1);

CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS db2 CASCADE;
CREATE SERVER kea FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'db2',
dbname 'db2');
CREATE USER MAPPING FOR USER SERVER db2 OPTIONS (user 'db2', password
'password');

CREATE SCHEMA IF NOT EXISTS db2;

IMPORT FOREIGN SCHEMA public FROM SERVER db2 INTO db2;

SELECT ok(
(SELECT COUNT(host_id) FROM db2.hosts) = 1,
'foobar'
);

-- ;
ROLLBACK;

Now, I would like to extract db2 initialization in separate file
"/test/init.sql" with this content:

CREATE EXTENSION IF NOT EXISTS postgres_fdw;
DROP SERVER IF EXISTS db2 CASCADE;
CREATE SERVER kea FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'db2',
dbname 'db2');
CREATE USER MAPPING FOR USER SERVER db2 OPTIONS (user 'db2', password
'password');

CREATE SCHEMA IF NOT EXISTS db2;

IMPORT FOREIGN SCHEMA public FROM SERVER db2 INTO db2;

Now, my test file is:

BEGIN;
SELECT plan(1);

\i /test/init.sql

SELECT ok(
(SELECT COUNT(host_id) FROM db2.hosts) = 1,
'foobar'
);

In log I see that "init.sql" file is loaded with success:

Running tests: /test/*.sql -v
/test/init.sql ................... No subtests run

But I have this error:

ERROR: user mapping not found for "db2"

Question: where is my mistake? How can I include some sql file in my test?

Best regards,
Stéphane
--
Stéphane Klein <contact(at)stephane-klein(dot)info>
blog: http://stephane-klein.info
cv : http://cv.stephane-klein.info
Twitter: http://twitter.com/klein_stephane

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Łukasz Jarych 2018-03-02 11:15:03 Tracking changes DML in history log table
Previous Message Thomas Kellerer 2018-03-02 09:01:08 Re: PG 10 logical replication version dependency?