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


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

insert into media_content (media_type, bytes)
values ('text/plain', 'content2')
     , ('text/plain', 'content3')
     , ('text/plain', 'content4')
     , ('text/plain', 'content5')
;

insert into media (media_id, company_id, original_filename, content_hash)
values ( 7, 1, 'text2.txt', sha256('content2'))
     , ( 8, 1, 'text3.txt', sha256('content3'))
     , ( 9, 2, 'text4.txt', sha256('content4'))
     , (10, 2, 'text5.txt', sha256('content5'))
;


prepare surroundings_ad_data as
select company_id, media_id, title, anchor, href::text
from surroundings_ad
;

select lives_ok(
	$$ select setup_surroundings_ad(1, 7, 'Enjoy!', 'Good Link', 'https://ddg.gg/') $$,
	'Should be able to setup surroundings_ad for the first company'
);

select lives_ok(
	$$ select setup_surroundings_ad(2, 9, 'Go Away!', 'Bad Link', 'https://google.es/') $$,
	'Should be able to setup surroundings_ad for the second company'
);

select bag_eq(
	'surroundings_ad_data',
	$$ values (1, 7, 'Enjoy!', 'Good Link', 'https://ddg.gg/')
	        , (2, 9, 'Go Away!', 'Bad Link', 'https://google.es/')
	$$,
	'Should have inserted all surrounding ad'
);

select lives_ok(
	$$ select setup_surroundings_ad(1, 8, 'Come Back!', 'Good Memories', 'https://astalavista.box.sk/') $$,
	'Should be able to update surroundings_ad for the first company'
);

select lives_ok(
	$$ select setup_surroundings_ad(2, 10, 'Quoi?', 'Ecs', 'https://yahoo.fr/') $$,
	'Should be able to update surroundings_ad for the second company'
);

select bag_eq(
	'surroundings_ad_data',
	$$ values (1,  8, 'Come Back!', 'Good Memories', 'https://astalavista.box.sk/')
	        , (2, 10, 'Quoi?', 'Ecs', 'https://yahoo.fr/')
	$$,
	'Should have updated all surrounding ad'
);


select *
from finish();

rollback;
