camper/test/draft_payment.sql

162 lines
8.4 KiB
PL/PgSQL
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Test draft_payment
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(16);
set search_path to camper, public;
select has_function('camper', 'draft_payment', array['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]']);
select function_lang_is('camper', 'draft_payment', array['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'plpgsql');
select function_returns('camper', 'draft_payment', array['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'payment');
select isnt_definer('camper', 'draft_payment', array['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]']);
select volatility_is('camper', 'draft_payment', array['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'volatile');
select function_privs_are('camper', 'draft_payment', array ['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'guest', array['EXECUTE']);
select function_privs_are('camper', 'draft_payment', array ['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'employee', array['EXECUTE']);
select function_privs_are('camper', 'draft_payment', array ['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'draft_payment', array ['uuid', 'date', 'date', 'uuid', 'integer', 'integer', 'integer', 'integer', 'text', 'option_units[]'], 'authenticator', array[]::text[]);
set client_min_messages to warning;
truncate payment_option cascade;
truncate payment cascade;
truncate campsite_type_option_cost cascade;
truncate campsite_type_option cascade;
truncate campsite_type_pet_cost cascade;
truncate campsite_type_cost cascade;
truncate campsite_type cascade;
truncate media cascade;
truncate media_content cascade;
truncate season_calendar cascade;
truncate season 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 season (season_id, company_id, name)
values (4, 2, 'High')
, (6, 2, 'Shoulder')
, (8, 2, 'Offseason')
;
insert into season_calendar (season_id, season_range)
values (4, daterange(current_date, current_date + 60))
, (6, daterange(current_date + 60, current_date + 64))
, (8, daterange(current_date + 64, current_date + 69))
;
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, slug, company_id, name, media_id, max_campers, bookable_nights, overflow_allowed)
values (12, 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 'Plots', 10, 6, '[1, 7]', true)
, (14, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 2, 'Bungalow', 10, 6, '[1, 7]', false)
;
insert into campsite_type_cost (campsite_type_id, season_id, cost_per_night, cost_per_adult, cost_per_teenager, cost_per_child)
values (12, 4, 400, 795, 795, 640)
, (12, 6, 200, 740, 740, 590)
, (12, 8, 0, 660, 660, 540)
, (14, 4, 17000, 0, 0, 0)
, (14, 6, 13500, 0, 0, 0)
, (14, 8, 10500, 0, 0, 0)
;
insert into campsite_type_pet_cost (campsite_type_id, cost_per_night)
values (12, 350)
;
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, 'Car', '[0, 4)', true)
, (20, 12, 'Electricity', '[0, 5)', false)
;
insert into campsite_type_option_cost (campsite_type_option_id, season_id, cost)
values (16, 4, 800)
, (16, 6, 720)
, (16, 8, 620)
, (18, 4, 700)
, (18, 6, 630)
, (18, 8, 530)
, (20, 4, 690)
, (20, 6, 610)
, (20, 8, 590)
;
insert into payment (payment_id, slug, 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, payment_status, created_at, updated_at)
values (22, '7cccfe16-695e-486d-a6a5-1162fb85cafb', 2, 12, current_date + 60, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', '', 'draft', '2024-01-01 01:01:01', '2024-01-01 01:01:01')
, (24, '6eeae04c-2fea-4d67-97dc-a4b8a83df99f', 2, 12, current_date + 61, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', '', 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02')
;
insert into payment_option (payment_id, campsite_type_option_id, units, subtotal)
values (22, 16, 1, 0)
, (22, 18, 1, 0)
, (24, 16, 2, 0)
;
select lives_ok(
$$ select draft_payment(null, current_date + 59, current_date + 64, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 1, 2, 3, 0, null, null) $$,
'Should be able to create a new draft for Bungalows'
);
select lives_ok(
$$ select draft_payment(null, current_date + 6, current_date + 11, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 1, 2, 3, 0, null, null) $$,
'A payment for a reservation in less than a week has a 100 % of downpayment'
);
select lives_ok(
$$ select draft_payment(null, current_date + 10, current_date + 58, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 2, 3, 4, 0, null, null) $$,
'All reservations incur a tourist tax for the first tourist_tax_max_days days'
);
select lives_ok(
$$ select draft_payment('7cccfe16-695e-486d-a6a5-1162fb85cafb', current_date + 58, current_date + 65, 'c1b6f4fc-32c1-4cd5-b796-0c5059152a52', 2, 4, 6, 3, 'pref I before E', array[(16, 2), (20, 3)]::option_units[]) $$,
'Should be able to update the draft for Plots'
);
select results_ne(
$$ select slug::text from draft_payment('6eeae04c-2fea-4d67-97dc-a4b8a83df99f', current_date + 31, current_date + 36, 'b065f4e3-2cc8-491d-a413-d015d7d00183', 2, 1, 1, 1, 'under a tree', array[(16, 1), (18, 1)]::option_units[]) $$,
$$ values ('6eeae04c-2fea-4d67-97dc-a4b8a83df99f') $$,
'When trying to draft a payment already pending, completed, failed, or refunded, create a new instead'
);
select bag_eq(
$$ select 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, down_payment_percent, zone_preferences, payment_status, created_at, updated_at from payment $$,
$$ values (2, 12, current_date + 58, current_date + 65, 3200, 2, 10420, 4, 20840, 6, 25080, 3, 2450, 4900, 79160, 'EUR', 0.3, 'pref I before E', 'draft', '2024-01-01 01:01:01', current_timestamp)
, (2, 14, current_date + 59, current_date + 64, 71000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 72750, 'EUR', 0.3, '', 'draft', current_timestamp, current_timestamp)
, (2, 14, current_date + 10, current_date + 58, 1632000, 2, 0, 3, 0, 4, 0, 0, 0, 4900, 1636900, 'EUR', 0.3, '', 'draft', current_timestamp, current_timestamp)
, (2, 14, current_date + 6, current_date + 11, 85000, 1, 0, 2, 0, 3, 0, 0, 0, 1750, 86750, 'EUR', 1.0, '', 'draft', current_timestamp, current_timestamp)
, (2, 12, current_date + 61, current_date + 62, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'USD', 1.0, '', 'pending', '2024-01-01 02:02:02', '2024-01-01 02:02:02')
, (2, 14, current_date + 31, current_date + 36, 85000, 2, 0, 1, 0, 1, 0, 1, 0, 3500, 96000, 'EUR', 0.3, 'under a tree', 'draft', current_timestamp, current_timestamp)
$$,
'Should have added and updated payments'
);
select bag_eq(
$$ select case payment_id when 22 then 'a' when 24 then 'b' else 'c' end, campsite_type_option_id, units, subtotal from payment_option $$,
$$ values ('a', 16, 2, 10200)
, ('a', 20, 3, 2070)
, ('b', 16, 2, 0)
, ('c', 16, 1, 4000)
, ('c', 18, 1, 3500)
$$,
'Should have added, updated, and removed payment options'
);
select *
from finish();
rollback;