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

begin;

select plan(15);

set search_path to camper, public;

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


set client_min_messages to warning;
truncate home 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')
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 'FR', 'USD', 'ca')
;

prepare home_data as
select company_id, slogan
from home
;

select lives_ok(
	$$ select setup_home(1, 'Enjoy!') $$,
	'Should be able to setup home for the first company'
);

select lives_ok(
	$$ select setup_home(2, 'Go Away!') $$,
	'Should be able to setup home for the second company'
);

select bag_eq(
	'home_data',
	$$ values (1, 'Enjoy!')
	        , (2, 'Go Away!')
	$$,
	'Should have inserted all home texts'
);

select lives_ok(
	$$ select setup_home(1, 'Come Back!') $$,
	'Should be able to update home for the first company'
);

select lives_ok(
	$$ select setup_home(2, 'Quoi?') $$,
	'Should be able to update home for the second company'
);

select bag_eq(
	'home_data',
	$$ values (1, 'Come Back!')
	        , (2, 'Quoi?')
	$$,
	'Should have updated all home texts'
);


select *
from finish();

rollback;