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

begin;

set search_path to camper, public;

select plan(12);

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


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

select lives_ok(
	$$ select add_campsite_type(1, 'Type A', '<!-- block --><h2>This is what, exactly?</h2><!-- /block --><p>Dunno</p>') $$,
	'Should be able to add a campsite type to the first company'
);

select lives_ok(
	$$ select add_campsite_type(2, 'Type B', '') $$,
	'Should be able to add a campsite type to the second company'
);

select bag_eq(
	$$ select company_id, name, description::text, active from campsite_type $$,
	$$ values (1, 'Type A', '<!-- block --><h2>This is what, exactly?</h2><!-- /block --><p>Dunno</p>', true)
	        , (2, 'Type B', '', true)
	$$,
	'Should have added all two campsite type'
);


select *
from finish();

rollback;
