campingmontagut/test/edit_booking.sql
jordi fita mas fdf9502c8b “Finish” the new booking form
Had to bring the same fields that i have for a payment to booking,
except that some of those should be nullable, because it is unreasonable
to ask front desk to gather all customer data when they have a booking
via phone, for instance.

Therefore, i can not take advantage of the validation for customer data
that i use in the public-facing form, but, fortunately, most of the
validations where in separated functions, thus only had to rewrite that
one for this case.

I already have to create a booking from a payment, when receiving a
payment from the public instance, thus i made that function and reused
it here.  Then i “overwrite” the newly created pre-booking with the
customer data from the form, and set is as confirmed, as we do not see
any point of allowing pre-bookings from employees.
2024-04-24 20:19:13 +02:00

96 lines
5.3 KiB
PL/PgSQL

-- Test edit_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', 'edit_booking', array['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]']);
select function_lang_is('camper', 'edit_booking', array['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'plpgsql');
select function_returns('camper', 'edit_booking', array['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'void');
select isnt_definer('camper', 'edit_booking', array['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]']);
select volatility_is('camper', 'edit_booking', array['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'volatile');
select function_privs_are('camper', 'edit_booking', array ['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'guest', array[]::text[]);
select function_privs_are('camper', 'edit_booking', array ['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'edit_booking', array ['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'edit_booking', array ['integer', 'text', 'text', 'text', 'text', 'text', 'email', 'text', 'text', 'text', 'integer[]'], 'authenticator', array[]::text[]);
set client_min_messages to warning;
truncate booking_campsite cascade;
truncate booking 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', '', '', '', '', '', '', 60, 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 (6, 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)
values (10, 2, 'Wooden lodge', 6, 7, '[1, 7]')
;
insert into campsite (campsite_id, company_id, label, campsite_type_id)
values (12, 2, 'A', 10)
, (14, 2, 'B', 10)
, (16, 2, 'C', 10)
;
insert into booking (booking_id, company_id, campsite_type_id, holder_name, stay, acsi_card, currency_code, zone_preferences, subtotal_nights, number_adults, subtotal_adults, number_teenagers, subtotal_teenagers, number_children, subtotal_children, number_dogs, subtotal_dogs, subtotal_tourist_tax, total)
values (18, 2, 10, 'Holder 2', daterange('2024-01-18', '2024-01-29'), false, 'EUR', '', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
, (20, 2, 10, 'Holder 4', daterange('2024-01-28', '2024-01-29'), true, 'USD', 'None', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
;
insert into booking_campsite (booking_id, campsite_id, stay)
values (18, 12, daterange('2024-01-18', '2024-01-29'))
;
select lives_ok(
$$ select edit_booking(18, 'First', 'Fake St., 123', '17800', 'Girona', 'ES', 'customer@example.com', '+34 977 97 79 77', 'ca', 'confirmed', array[14]) $$,
'Should be able to edit the first booking'
);
select lives_ok(
$$ select edit_booking(20, 'Second', 'None', '555', 'Paris', 'FR', 'customer@example.fr', '+33 1 23 12 31 23', 'fr', 'invoiced', array[12, 16]) $$,
'Should be able to edit the second booking'
);
select bag_eq(
$$ select booking_id, 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 (18, 2, 10, daterange('2024-01-18', '2024-01-29'), 'First', 'Fake St., 123', '17800', 'Girona', 'ES', 'customer@example.com', '+34 977 97 79 77', 'ca', '', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, false, 'EUR', 'confirmed')
, (20, 2, 10, daterange('2024-01-28', '2024-01-29'), 'Second', 'None', '555', 'Paris', 'FR', 'customer@example.fr', '+33 1 23 12 31 23', 'fr', 'None', 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, true, 'USD', 'invoiced')
$$,
'Should have updated the bookings'
);
select bag_eq (
$$ select booking_id, campsite_id, stay from booking_campsite $$,
$$ values (18, 14, daterange('2024-01-18', '2024-01-29'))
, (20, 12, daterange('2024-01-28', '2024-01-29'))
, (20, 16, daterange('2024-01-28', '2024-01-29'))
$$ ,
'Should have updated the booking campsites too'
);
select *
from finish();
rollback;