|
| 1 | +begin; |
| 2 | +select plan(9); |
| 3 | +select pgflow_tests.reset_db(); |
| 4 | + |
| 5 | +-- TEST: Inserts new worker function when it does not exist |
| 6 | +select pgflow.track_worker_function('my-edge-function'); |
| 7 | +select is( |
| 8 | + (select count(*) from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 9 | + 1::bigint, |
| 10 | + 'track_worker_function() inserts new worker function' |
| 11 | +); |
| 12 | + |
| 13 | +-- TEST: New worker function has enabled=true by default |
| 14 | +select is( |
| 15 | + (select enabled from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 16 | + true, |
| 17 | + 'New worker function has enabled=true by default' |
| 18 | +); |
| 19 | + |
| 20 | +-- TEST: New worker function has default heartbeat_timeout_seconds |
| 21 | +select is( |
| 22 | + (select heartbeat_timeout_seconds from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 23 | + 6, |
| 24 | + 'New worker function has heartbeat_timeout_seconds=6 by default' |
| 25 | +); |
| 26 | + |
| 27 | +-- TEST: New worker function has last_invoked_at set (debounce protection) |
| 28 | +select isnt( |
| 29 | + (select last_invoked_at from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 30 | + null::timestamptz, |
| 31 | + 'New worker function has last_invoked_at set on insert (debounce protection)' |
| 32 | +); |
| 33 | + |
| 34 | +-- TEST: last_invoked_at is set to approximately now |
| 35 | +select ok( |
| 36 | + (select last_invoked_at >= now() - interval '1 second' |
| 37 | + from pgflow.worker_functions |
| 38 | + where function_name = 'my-edge-function'), |
| 39 | + 'last_invoked_at is set to approximately now on insert' |
| 40 | +); |
| 41 | + |
| 42 | +-- TEST: Upsert updates updated_at on conflict |
| 43 | +-- First, get the original updated_at timestamp |
| 44 | +select pg_sleep(0.01); -- Small delay to ensure timestamp difference |
| 45 | +select pgflow.track_worker_function('my-edge-function'); |
| 46 | +select ok( |
| 47 | + (select updated_at > created_at from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 48 | + 'Upsert updates updated_at timestamp on conflict' |
| 49 | +); |
| 50 | + |
| 51 | +-- TEST: Upsert updates last_invoked_at on conflict |
| 52 | +select ok( |
| 53 | + (select last_invoked_at >= now() - interval '1 second' |
| 54 | + from pgflow.worker_functions |
| 55 | + where function_name = 'my-edge-function'), |
| 56 | + 'Upsert updates last_invoked_at on conflict (refreshes debounce)' |
| 57 | +); |
| 58 | + |
| 59 | +-- TEST: Upsert does not duplicate rows |
| 60 | +select is( |
| 61 | + (select count(*) from pgflow.worker_functions where function_name = 'my-edge-function'), |
| 62 | + 1::bigint, |
| 63 | + 'Upsert does not create duplicate rows' |
| 64 | +); |
| 65 | + |
| 66 | +-- TEST: Can track multiple different functions |
| 67 | +select pgflow.track_worker_function('another-function'); |
| 68 | +select is( |
| 69 | + (select count(*) from pgflow.worker_functions), |
| 70 | + 2::bigint, |
| 71 | + 'Can track multiple different worker functions' |
| 72 | +); |
| 73 | + |
| 74 | +select finish(); |
| 75 | +rollback; |
0 commit comments