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

set client_min_messages to warning;
truncate location 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 location_data as
select company_id, directions::text, map_embed::text, opening_dates::text
from location
;

select lives_ok(
	$$ select setup_location(1, '<p>Up</p>', '<map/>', '<p>Today</p>') $$,
	'Should be able to setup location parameters for the first company'
);

select lives_ok(
	$$ select setup_location(2, '<p>Left</p>', '<leaflet/>', '<p>Tomorrow</p>') $$,
	'Should be able to setup location parameters for the second company'
);

select bag_eq(
	'location_data',
	$$ values (1, '<p>Up</p>', '<map/>', '<p>Today</p>'),
	          (2, '<p>Left</p>', '<leaflet/>', '<p>Tomorrow</p>')
	$$,
	'Should have inserted all location parameters'
);

select lives_ok(
	$$ select setup_location(1, '<p>Down</p>', '<google/>', '<p>Never</p>') $$,
	'Should be able to update location parameters for the first company'
);

select lives_ok(
	$$ select setup_location(2, '<p>Right</p>', '<map/>', '<p>Yesterday</p>') $$,
	'Should be able to update location parameters for the second company'
);

select bag_eq(
	'location_data',
	$$ values (1, '<p>Down</p>', '<google/>', '<p>Never</p>'),
	          (2, '<p>Right</p>', '<map/>', '<p>Yesterday</p>')
	$$,
	'Should have updated all location parameters'
);

select *
from finish();

rollback;