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

set client_min_messages to warning;
truncate service_i18n cascade;
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, country_code, currency_code, default_lang_tag)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '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>')
     , (7, 1, 'wheelchair', 'Service C', '<p>C</p>')
;

insert into service_i18n (service_id, lang_tag, name, description)
values (5, 'ca', 'Servei A', '<p>A</p>')
     , (5, 'es', 'Servicio A', '<p>A</p>')
     , (6, 'ca', 'Servei B', '<p>B</p>')
     , (6, 'es', 'Servicio B', '<p>B</p>')
     , (7, 'ca', 'Servei C', '<p>C</p>')
     , (7, 'es', 'Servicio C', '<p>C</p>')
;

select lives_ok(
	$$ select remove_service(6) $$,
	'Should be able to delete a service'
);

select bag_eq(
	$$ select service_id, name from service $$,
	$$ values (5, 'Service A')
	        , (7, 'Service C')
	$$,
	'Should have removed the serivec'
);

select bag_eq(
	$$ select service_id, lang_tag, name from service_i18n $$,
	$$ values (5, 'ca', 'Servei A')
	        , (5, 'es', 'Servicio A')
	        , (7, 'ca', 'Servei C')
	        , (7, 'es', 'Servicio C')
	$$,
	'Should have removed the service’s translations'
);

select *
from finish();

rollback;
