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

begin;

select plan(13);

set search_path to camper, public;

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


set client_min_messages to warning;
truncate home_i18n cascade;
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, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (2, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
     , (4, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', '', 60, 7, 'FR', 'USD', 'ca')
;

insert into home (company_id, slogan)
values (2, 'Enjoy')
     , (4, 'Away')
;

insert into home_i18n (company_id, lang_tag, slogan)
values (2, 'ca', 'Hola')
     , (4, 'ca', 'Hola')
;

select lives_ok(
	$$ select translate_home(2, 'es', 'Disfruta') $$,
	'Should be able to translate the home of the first company to a new language'
);

select lives_ok(
	$$ select translate_home(2, 'ca', 'Gaudeix') $$,
	'Should be able to overwrite a home’s translation'
);

select lives_ok(
	$$ select translate_home(4, 'ca', null) $$,
	'Should be able to “translate” a home to empty strings'
);


select bag_eq(
	$$ select company_id, lang_tag, slogan from home_i18n $$,
	$$ values (2, 'ca', 'Gaudeix')
	        , (2, 'es', 'Disfruta')
	        , (4, 'ca', '')
	$$,
	'Should have translated all home texts'
);


select *
from finish();

rollback;
