-- Test edit_service
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_service', array['integer', 'text', 'text', 'text']);
select function_lang_is('camper', 'edit_service', array['integer', 'text', 'text', 'text'], 'sql');
select function_returns('camper', 'edit_service', array['integer', 'text', 'text', 'text'], 'void');
select isnt_definer('camper', 'edit_service', array['integer', 'text', 'text', 'text']);
select volatility_is('camper', 'edit_service', array['integer', 'text', 'text', 'text'], 'volatile');
select function_privs_are('camper', 'edit_service', array ['integer', 'text', 'text', 'text'], 'guest', array[]::text[]);
select function_privs_are('camper', 'edit_service', array ['integer', 'text', 'text', 'text'], 'employee', array[]::text[]);
select function_privs_are('camper', 'edit_service', array ['integer', 'text', 'text', 'text'], 'admin', array['EXECUTE']);
select function_privs_are('camper', 'edit_service', array ['integer', 'text', 'text', 'text'], 'authenticator', array[]::text[]);

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

insert into service (service_id, company_id, icon_name, name, description)
values (5, 1, 'information', 'Service A', '<p>A</p>')
     , (6, 1, 'toilet', 'Service B', '<p>B</p>')
;

select lives_ok(
	$$ select edit_service(5, 'wifi', 'Service 1', '<p>1</p>') $$,
	'Should be able to edit the first service'
);

select lives_ok(
	$$ select edit_service(6, 'baby', 'Service 2', '<p>2</p>') $$,
	'Should be able to edit the second service'
);

select bag_eq(
	$$ select service_id, icon_name, name, description::text from service $$,
	$$ values (5, 'wifi', 'Service 1', '<p>1</p>')
	        , (6, 'baby', 'Service 2', '<p>2</p>')
	$$,
	'Should have updated all services.'
);

select *
from finish();

rollback;