-- Test edit_amenity
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(12);

set search_path to camper, public;

select has_function('camper', 'edit_amenity', array['integer', 'text', 'text', 'text', 'text', 'boolean']);
select function_lang_is('camper', 'edit_amenity', array['integer', 'text', 'text', 'text', 'text', 'boolean'], 'sql');
select function_returns('camper', 'edit_amenity', array['integer', 'text', 'text', 'text', 'text', 'boolean'], 'integer');
select isnt_definer('camper', 'edit_amenity', array['integer', 'text', 'text', 'text', 'text', 'boolean']);
select volatility_is('camper', 'edit_amenity', array['integer', 'text', 'text', 'text', 'text', 'boolean'], 'volatile');
select function_privs_are('camper', 'edit_amenity', array ['integer', 'text', 'text', 'text', 'text', 'boolean'], 'guest', array[]::text[]);
select function_privs_are('camper', 'edit_amenity', array ['integer', 'text', 'text', 'text', 'text', 'boolean'], 'employee', array[]::text[]);
select function_privs_are('camper', 'edit_amenity', array ['integer', 'text', 'text', 'text', 'text', 'boolean'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'edit_amenity', array ['integer', 'text', 'text', 'text', 'text', 'boolean'], 'authenticator', array[]::text[]);

set client_min_messages to warning;
truncate amenity 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 (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
;

insert into amenity (amenity_id, company_id, label, name, info1, info2, active)
values (21, 1, 'A1', 'Anemity A1', '<p>A1.1</p>', '<p>A1.2</p>', true)
     , (22, 1, 'B1', 'Anemity B1', '<p>B1.1</p>', '<p>B1.2</p>', false)
;

select lives_ok(
	$$ select edit_amenity(21, 'C1', 'Anemity C1', '<p>C1.1</p>', '<p>C1.2</p>', false) $$,
	'Should be able to edit the first amenity.'
);

select lives_ok(
	$$ select edit_amenity(22, 'B2', 'Anemity B2', '<p>B2.1</p>', '<p>B2.2</p>', true) $$,
	'Should be able to edit the second amenity.'
);

select bag_eq(
	$$ select amenity_id, label, name, info1::text, info2::text, active from amenity $$,
	$$ values (21, 'C1', 'Anemity C1', '<p>C1.1</p>', '<p>C1.2</p>', false)
	        , (22, 'B2', 'Anemity B2', '<p>B2.1</p>', '<p>B2.2</p>', true)
	$$,
	'Should have updated all amenities.'
);


select *
from finish();

rollback;
