Newbie question on lives_ok()

35 views
Skip to first unread message

Alejandro Imass

unread,
Nov 24, 2022, 8:38:57 AM11/24/22
to pgTAP Users
Hi there!

I'm trying to test a trigger/function pair that updates a timestamp and user on update.
Just getting started on pgTAP and perhaps this is a dumb question but in the code below shouldn't lives_ok raise an exception if there is no record with id = 1??
Does lives_ok actually execute the statement ?
Also, can I execute arbitrary SQL around tests, or is better to always wrap them in lives_ok/throws_ok?

BEGIN;
SELECT plan(2);
PREPARE updatedp AS UPDATE processor set name = 'YUCA' where id = 1;
SELECT lives_ok('updatedp', 'update a processor');
SELECT ok((SELECT updated_ts from processor where id = 1) != NULL, 'updated_ts is not null');
ROLLBACK;

Thanks in advance!

-- 
Alex

Robert L. Kirby

unread,
Nov 24, 2022, 10:40:34 PM11/24/22
to pgTAP Users
An UPDATE statement with no records to update simply makes no changes. No exception should be produced. The "lives_ok" worked.

It's safer to wrap almost statements, such as your PREPARE, so that you're not surprised when something you depend on later didn't work.

Alejandro Imass

unread,
Nov 27, 2022, 8:10:45 AM11/27/22
to pgTAP Users
Yeah! I later realized this LOL

Some head banging but eventually came up with:

SELECT ok(foo is not null, 'foo is not null') from bar where bar = 1;

I guess the syntax just takes a little getting used to... 

Thanks!!

-- 
Alex

David E. Wheeler

unread,
Nov 27, 2022, 1:49:03 PM11/27/22
to Alejandro Imass, pgTAP Users
On Nov 27, 2022, at 08:10, Alejandro Imass <aim...@yabarana.com> wrote:

> SELECT ok(foo is not null, 'foo is not null') from bar where bar = 1;
>
> I guess the syntax just takes a little getting used to…

Perfect use case for EXISTS:

SELECT ok(
EXISTS(SELECT 1 FROM foo WHERE bar = 1),
'Foo 1 exists',
);

Best,

David

signature.asc

Alejandro Imass

unread,
Nov 28, 2022, 4:36:15 AM11/28/22
to David E. Wheeler, pgTAP Users
Thank you ! 

NVL what I am testing is that a trigger updated a field. 

Best,

-- 
Alex

--
You received this message because you are subscribed to the Google Groups "pgTAP Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pgtap-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pgtap-users/67D0EFBE-E83F-400F-B1BA-969F13425D2E%40justatheory.com.
Reply all
Reply to author
Forward
0 new messages