-- Test add_season
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(13);

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


set client_min_messages to warning;
truncate season 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')
;

select lives_ok(
	$$ select add_season(1, 'Low', '#232323') $$,
	'Should be able to add a season to the first company'
);

select lives_ok(
	$$ select add_season(1, 'Mid', '#555555') $$,
	'Should be able to add another season to the same company'
);

select lives_ok(
	$$ select add_season(2, 'High', '#a0a0a0') $$,
	'Should be able to add a season to the second company'
);

select bag_eq(
	$$ select company_id, name, to_color(color)::text, active from season $$,
	$$ values (1, 'Low', '#232323', true)
	        , (1, 'Mid', '#555555', true)
	        , (2, 'High', '#a0a0a0', true)
	$$,
	'Should have added all seasons'
);

select *
from finish();

rollback;
