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

begin;

select plan(14);

set search_path to auth, numerus, public;

select has_function('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]']);
select function_lang_is('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'plpgsql');
select function_returns('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'uuid');
select isnt_definer('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]']);
select volatility_is('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'volatile');
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate product_tax cascade;
truncate product cascade;
truncate tax 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)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR')
     , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD')
;

insert into tax (tax_id, company_id, name, rate)
values (3, 1, 'IRPF -15 %', -0.15)
     , (4, 1, 'IVA 21 %', 0.21)
     , (5, 2, 'IRPF -7 %', -0.07)
     , (6, 2, 'IVA 10 %', 0.10)
;


select lives_ok(
	$$ select add_product(1, 'Product 1', 'Description 1', '12.12', array[3, 4]) $$,
	'Should be able to add product to first company'
);

select lives_ok(
	$$ select add_product(2, 'Product 2', 'Description 2', '24.24', array[6]) $$,
	'Should be able to add product to second company'
);

select lives_ok(
	$$ select add_product(2, 'Product 3', 'Description 3', '36.36', array[]::integer[]) $$,
	'Should be able to add product without taxes'
);

select bag_eq(
	$$ select company_id, name, description, price, created_at from product $$,
	$$ values (1, 'Product 1', 'Description 1', 1212, current_timestamp)
	        , (2, 'Product 2', 'Description 2', 2424, current_timestamp)
		, (2, 'Product 3', 'Description 3', 3636, current_timestamp)
	$$,
	'Should have added all three products'
);

select bag_eq(
	$$ select tax_id, name from product_tax join product using (product_id) $$,
	$$ values (3, 'Product 1')
	        , (4, 'Product 1')
		, (6, 'Product 2')
	$$,
	'Should have added the taxes for the products we told to'
);

select *
from finish();

rollback;
