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

begin;

select plan(13);

set search_path to numerus, public;

select has_function('numerus', 'edit_payment_account_cash', array['uuid', 'text']);
select function_lang_is('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'sql');
select function_returns('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'uuid');
select isnt_definer('numerus', 'edit_payment_account_cash', array['uuid', 'text']);
select volatility_is('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'volatile');

select function_privs_are('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'guest', array []::text[]);
select function_privs_are('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_payment_account_cash', array['uuid', 'text'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate payment_account cascade;
truncate payment_method cascade;
truncate company cascade;
reset client_min_messages;

set constraints "company_default_payment_method_id_fkey" deferred;

insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_payment_method_id)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 111)
;

insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
;

set constraints "company_default_payment_method_id_fkey" immediate;

insert into payment_account (payment_account_id, company_id, slug, payment_account_type, name)
values (11, 1, '81c478ab-395c-44aa-a5b4-3489e5349a0b', 'cash', 'Cash 1')
     , (12, 1, '84e84788-54f1-4796-b93d-dfa2c9adc072', 'cash', 'Cash 2')
     , (13, 1, 'afb774a5-0c38-480c-bef2-fe03465071cd', 'other', 'Other')
;


select lives_ok(
	$$ select edit_payment_account_cash('81c478ab-395c-44aa-a5b4-3489e5349a0b', 'My stash') $$,
	'Should be able to edit the first cash payment account'
);

select lives_ok (
	$$ select edit_payment_account_cash('84e84788-54f1-4796-b93d-dfa2c9adc072', 'Under mattress') $$,
	'Should be able to edit the second cash payment account'
);

select results_eq (
	$$ select edit_payment_account_cash('afb774a5-0c38-480c-bef2-fe03465071cd', 'Huh?')::text $$,
	$$ values (null::text) $$,
	'Should do nothing to an account that is not of type cash'
);

select bag_eq(
	$$ select payment_account_id, payment_account_type::text, name from payment_account $$,
	$$ values (11, 'cash', 'My stash')
	        , (12, 'cash', 'Under mattress')
	        , (13, 'other', 'Other')
	$$,
	'Should have update all cash payment accounts'
);

select *
from finish();

rollback;
