Using throws_ok inside functions

30 views
Skip to first unread message

Peter Beischmidt

unread,
Nov 15, 2018, 7:41:50 AM11/15/18
to pgTAP Users
Hi,

is there a way to use throws_ok and its siblings inside tests (written as functions returning setof text) without having to pass queries as strings?

As far as I can see, using prepared statements inside functions doesn't work, because the context (i.e. local variables) will be lost.

As the docs say, passing queries as strings to test functions is quite a PITA, on the other hand writing my tests as functions returning setof text is very convenient, so I would like to stick to that.

Thanks for any help in advance and for maintaining pgTAP!
All the best

David E. Wheeler

unread,
Nov 15, 2018, 6:56:15 PM11/15/18
to Peter Beischmidt, pgTAP Users
On Nov 15, 2018, at 07:41, Peter Beischmidt <peterbe...@gmail.com> wrote:

> is there a way to use throws_ok and its siblings inside tests (written as functions returning setof text) without having to pass queries as strings?

You could put the queries in a table, then select them from the table for execution.

Best,

David

signature.asc

Peter Beischmidt

unread,
Nov 21, 2018, 3:36:45 AM11/21/18
to pgTAP Users
Thanks for your reply!

Unless I misunderstand, that would still mean that I use format() to create the query as a string first, right?
I write my tests as plpgsql functions and to improve readability usually assign the query to a variable of type text first.
I was hoping there's a way to completely do without format() and dynamic queries.

David E. Wheeler

unread,
Nov 21, 2018, 9:13:55 AM11/21/18
to Peter Beischmidt, pgTAP Users
On Nov 21, 2018, at 03:36, Peter Beischmidt <peterbe...@gmail.com> wrote:

> Unless I misunderstand, that would still mean that I use format() to create the query as a string first, right?
> I write my tests as plpgsql functions and to improve readability usually assign the query to a variable of type text first.
> I was hoping there's a way to completely do without format() and dynamic queries.

Well, format() would certainly be useful, yes. But I don’t think there’s any way around the issue: SQL queries are text no matter what you do, FBOFW.

Best,

David

signature.asc
Reply all
Reply to author
Forward
0 new messages