-- Test unmarshal_booking set client_min_messages to warning; create extension if not exists pgtap; reset client_min_messages; begin; select plan(13); set search_path to camper, public; select has_function('camper', 'unmarshal_booking', array['jsonb']); select function_lang_is('camper', 'unmarshal_booking', array['jsonb'], 'plpgsql'); select function_returns('camper', 'unmarshal_booking', array['jsonb'], 'integer'); select isnt_definer('camper', 'unmarshal_booking', array['jsonb']); select volatility_is('camper', 'unmarshal_booking', array['jsonb'], 'volatile'); select function_privs_are('camper', 'unmarshal_booking', array['jsonb'], 'guest', array['EXECUTE']); select function_privs_are('camper', 'unmarshal_booking', array['jsonb'], 'employee', array['EXECUTE']); select function_privs_are('camper', 'unmarshal_booking', array['jsonb'], 'admin', array['EXECUTE']); select function_privs_are('camper', 'unmarshal_booking', array['jsonb'], 'authenticator', array[]::text[]); set client_min_messages to warning; truncate booking_option cascade; truncate booking cascade; truncate payment_customer cascade; truncate payment_option cascade; truncate payment cascade; truncate campsite_type_option cascade; truncate campsite_type cascade; truncate media cascade; truncate media_content cascade; truncate company cascade; reset client_min_messages; insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag) values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 350, 7, 'ES', 'EUR', 'ca') ; insert into media_content (media_type, bytes) values ('image/x-xpixmap', 'static char *s[]={"1 1 1 1","a c #ffffff","a"};') ; insert into media (media_id, company_id, original_filename, content_hash) values (10, 2, 'cover2.xpm', sha256('static char *s[]={"1 1 1 1","a c #ffffff","a"};')) ; insert into campsite_type (campsite_type_id, company_id, name, media_id, max_campers, bookable_nights, overflow_allowed) values (12, 2, 'Plots', 10, 6, '[1, 7]', true) , (14, 2, 'Bungalows', 10, 6, '[1, 7]', true) ; insert into campsite_type_option (campsite_type_option_id, campsite_type_id, name, range, per_night) values (16, 12, 'Big tent', '[0, 4)', true) , (18, 12, 'Small tent', '[0, 4)', true) ; insert into payment (payment_id, company_id, campsite_type_id, arrival_date, departure_date, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total, currency_code, zone_preferences, acsi_card, payment_status, created_at, updated_at) values (22, 2, 12, '2024-08-29', '2024-09-03', 71000, 2, 200, 3, 400, 4, 600, 5, 800, 1000, 1200, 'EUR', 'a b c', false, 'completed', '2024-01-01 01:01:01', '2024-02-02 02:02:02') , (24, 2, 14, '2024-08-30', '2024-09-02', 61000, 1, 100, 2, 200, 3, 400, 4, 600, 800, 600, 'USD', 'c b a', true, 'preauth', '2024-02-02 02:02:02', '2024-03-03 03:03:03') ; insert into payment_customer (payment_id, full_name, address, postal_code, city, country_code, email, phone, lang_tag) values (22, 'First Name', 'Fake St., 123', '17500', 'None', 'ES', 'one@example.cat', '+34 977 977 977', 'ca') , (24, 'Second Guy', 'Bullshit Av., 1', 'ABC123', 'Some', 'FR', 'two@example.fr', '+33 123 123 123', 'fr') ; insert into payment_option (payment_id, campsite_type_option_id, units, subtotal) values (22, 16, 1, 12) , (22, 18, 2, 24) ; select lives_ok( $$ select unmarshal_booking(marshal_payment(22)) $$, 'Can unmarshal the first payment marshalled' ); select lives_ok( $$ select unmarshal_booking(marshal_payment(24)) $$, 'Can unmarshal the second payment marshalled' ); select bag_eq( $$ select company_id, campsite_type_id, stay, holder_name, address, postal_code, city, country_code::text, email::text, phone::text, lang_tag, zone_preferences, subtotal_nights::integer, number_adults::integer, subtotal_adults::integer, number_teenagers::integer, subtotal_teenagers::integer, number_children::integer, subtotal_children::integer, number_dogs::integer, subtotal_dogs::integer, subtotal_tourist_tax::integer, total::integer, acsi_card, currency_code::text, booking_status from booking $$, $$ values (2, 12, daterange('2024-08-29', '2024-09-03'), 'First Name', 'Fake St., 123', '17500', 'None', 'ES', 'one@example.cat', '+34 977 97 79 77', 'ca', 'a b c', 71000, 2, 200, 3, 400, 4, 600, 5, 800, 1000, 1200, false, 'EUR', 'created') , (2, 14, daterange('2024-08-30', '2024-09-02'), 'Second Guy', 'Bullshit Av., 1', 'ABC123', 'Some', 'FR', 'two@example.fr', '+33 1 23 12 31 23', 'fr', 'c b a', 61000, 1, 100, 2, 200, 3, 400, 4, 600, 800, 600, true, 'USD', 'created') $$, 'Should have created the new bookings' ); select bag_eq ( $$ select campsite_type_id, campsite_type_option_id, units, subtotal from booking_option join booking using (booking_id) $$, $$ values (12, 16, 1, 12) , (12, 18, 2, 24) $$ , 'Should have added the booking options too' ); select * from finish(); rollback;