numerus/test/add_quote.sql

162 lines
6.8 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Test add_quote
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(18);
set search_path to auth, numerus, public;
select has_function('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]']);
select function_lang_is('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'plpgsql');
select function_returns('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'uuid');
select isnt_definer('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]']);
select volatility_is('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'volatile');
select function_privs_are('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'add_quote', array ['integer', 'date', 'integer', 'text', 'text', 'integer', 'tag_name[]', 'new_quote_product[]'], 'authenticator', array []::text[]);
set client_min_messages to warning;
truncate quote_number_counter cascade;
truncate quote_product_tax cascade;
truncate quote_product cascade;
truncate quote_contact cascade;
truncate quote_payment_method cascade;
truncate quote cascade;
truncate contact cascade;
truncate product cascade;
truncate tax cascade;
truncate tax_class 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, quote_number_format, default_payment_method_id)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', '"Q"YYYY0000', 111)
, (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', '"QUO"000-YY', 222)
;
insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
, (222, 2, 'cash', 'cash')
;
set constraints "company_default_payment_method_id_fkey" immediate;
insert into quote_number_counter (company_id, year, currval)
values (1, 2023, '5')
, (2, 2023, '55')
;
insert into tax_class (tax_class_id, company_id, name)
values (11, 1, 'tax')
, (22, 2, 'tax')
;
insert into tax (tax_id, company_id, tax_class_id, name, rate)
values (3, 1, 11, 'IRPF -15 %', -0.15)
, (4, 1, 11, 'IVA 21 %', 0.21)
, (5, 2, 22, 'IRPF -7 %', -0.07)
, (6, 2, 22, 'IVA 10 %', 0.10)
;
insert into product (product_id, company_id, name, price)
values ( 7, 1, 'Product 2.1', 1212)
, ( 8, 1, 'Product 2.2', 2424)
, ( 9, 2, 'Product 4.1', 4848)
, (10, 2, 'Product 4.2', 9696)
, (11, 2, 'Product 4.3', 1010)
;
insert into contact (contact_id, company_id, name)
values (12, 1, 'Contact 2.1')
, (13, 1, 'Contact 2.2')
, (14, 2, 'Contact 4.1')
, (15, 2, 'Contact 4.2')
;
select lives_ok(
$$ select add_quote(1, '2023-02-15', 12, 'No need for advance payment', 'Notes 1', null, '{tag1,tag2}','{"(7,Product 1,Description 1,12.24,2,0.0,{4})"}') $$,
'Should be able to insert an quote for the first company with a product'
);
select lives_ok(
$$ select add_quote(1, '2023-02-16', null, 'Pay 10% in advance', 'Notes 2', 111, '{}', '{"(7,Product 1 bis,Description 1 bis,33.33,1,0.50,\"{4,3}\")","(8,Product 2,Description 2,24.00,3,0.75,{})"}') $$,
'Should be able to insert a second quote for the first company with two product'
);
select lives_ok(
$$ select add_quote(2, '2023-02-14', 15, 'Pay half in advance', 'Notes 3', 222, '{tag3}','{"(11,Product 4.3,,11.11,1,0.0,{6})","(,Product 4.4,Description 4.4,22.22,3,0.05,{})"}') $$,
'Should be able to insert an quote for the second company with a product'
);
select bag_eq(
$$ select company_id, quote_number, quote_date, quote_status, terms_and_conditions, notes, currency_code, tags, created_at from quote $$,
$$ values (1, 'Q20230006', '2023-02-15'::date, 'created', 'No need for advance payment', 'Notes 1', 'EUR', '{tag1,tag2}'::tag_name[], current_timestamp)
, (1, 'Q20230007', '2023-02-16'::date, 'created', 'Pay 10% in advance', 'Notes 2', 'EUR', '{}'::tag_name[], current_timestamp)
, (2, 'QUO056-23', '2023-02-14'::date, 'created', 'Pay half in advance', 'Notes 3', 'USD', '{tag3}'::tag_name[], current_timestamp)
$$,
'Should have created all quotes'
);
select bag_eq(
$$ select quote_number, payment_method_id from quote_payment_method join quote using (quote_id) $$,
$$ values ('Q20230007', 111)
, ('QUO056-23', 222)
$$,
'Should have created all payment methods'
);
select bag_eq(
$$ select quote_number, contact_id from quote_contact join quote using (quote_id) $$,
$$ values ('Q20230006', 12)
, ('QUO056-23', 15)
$$,
'Should have created all contacts'
);
select bag_eq(
$$ select quote_number, name, description, price, quantity, discount_rate from quote_product join quote using (quote_id) $$,
$$ values ('Q20230006', 'Product 1', 'Description 1', 1224, 2, 0.00)
, ('Q20230007', 'Product 1 bis', 'Description 1 bis', 3333, 1, 0.50)
, ('Q20230007', 'Product 2', 'Description 2', 2400, 3, 0.75)
, ('QUO056-23', 'Product 4.3', '', 1111, 1, 0.0)
, ('QUO056-23', 'Product 4.4', 'Description 4.4', 2222, 3, 0.05)
$$,
'Should have created all quote products'
);
select bag_eq(
$$ select quote_number, product_id, name from quote_product left join quote_product_product using (quote_product_id) join quote using (quote_id) $$,
$$ values ('Q20230006', 7, 'Product 1')
, ('Q20230007', 7, 'Product 1 bis')
, ('Q20230007', 8, 'Product 2')
, ('QUO056-23', 11, 'Product 4.3')
, ('QUO056-23', NULL, 'Product 4.4')
$$,
'Should have linked all quote products'
);
select bag_eq(
$$ select quote_number, name, tax_id, tax_rate from quote_product_tax join quote_product using (quote_product_id) join quote using (quote_id) $$,
$$ values ('Q20230006', 'Product 1', 4, 0.21)
, ('Q20230007', 'Product 1 bis', 4, 0.21)
, ('Q20230007', 'Product 1 bis', 3, -0.15)
, ('QUO056-23', 'Product 4.3', 6, 0.10)
$$,
'Should have created all quote product taxes'
);
select *
from finish();
rollback;