\set ON_ERROR_STOP on begin; create function pg_temp.assert_service(condition boolean, label text) returns void language plpgsql as $$ begin if condition is distinct from true then raise exception 'FAIL: %', label; end if; raise notice 'PASS: %', label; end; $$; create temp table fixture as select gen_random_uuid() event_id, gen_random_uuid() other_event, gen_random_uuid() service_id, gen_random_uuid() talk_id, 'service-first-' || gen_random_uuid() || '@example.invalid' first_email, 'service-later-' || gen_random_uuid() || '@example.invalid' later_email, 'service-other-' || gen_random_uuid() || '@example.invalid' other_email, -9000000-floor(random()*1000000)::integer service_slot, -11000000-floor(random()*1000000)::integer talk_slot; insert into public.events(id,name,start_date,end_date) select event_id,'Service fixture',date '2026-10-23',date '2026-10-23' from fixture union all select other_event,'Other service fixture','2026-10-23','2026-10-23' from fixture; insert into public.contacts(email,full_name) select first_email,'First Staff' from fixture union all select later_email,'Later Staff' from fixture union all select other_email,'Other Staff' from fixture; insert into public.registrations(event_id,contact_email,type) select event_id,first_email,'staff'::public.registration_type from fixture union all select event_id,later_email,'attendee' from fixture union all select other_event,other_email,'staff' from fixture; insert into public.slots(id,event_id,type,name,slot_date,start_time,end_time) select service_slot,event_id,'service_session'::public.slot_type,'Coffee',date '2026-10-23',time '08:30',time '09:00' from fixture union all select talk_slot,event_id,'session','Talk','2026-10-23','10:15','11:00' from fixture; insert into public.sessions(id,event_id,slot_id,title,external_id) select service_id,event_id,service_slot,'Coffee',service_id::text from fixture union all select talk_id,event_id,talk_slot,'Talk',talk_id::text from fixture; select pg_temp.assert_service((select count(*)=1 from public.session_speakers where session_id=fixture.service_id),'existing staff assigned on service creation') from fixture; update public.registrations set type='staff' where contact_email=(select later_email from fixture); select pg_temp.assert_service((select count(*)=2 from public.session_speakers where session_id=fixture.service_id),'later staff assigned to service') from fixture; select pg_temp.assert_service(not exists(select 1 from public.session_speakers where session_id=fixture.talk_id),'talk speakers untouched') from fixture; select pg_temp.assert_service(not exists(select 1 from public.session_speakers where session_id=fixture.service_id and contact_email=fixture.other_email),'other event staff excluded') from fixture; select public._assign_service_session_speakers(service_id,event_id) from fixture; select pg_temp.assert_service((select count(*)=2 from public.session_speakers where session_id=fixture.service_id),'repeat assignment is idempotent') from fixture; rollback;