2023-02-14 11:39:54 +00:00
|
|
|
|
-- Test add_product
|
|
|
|
|
set client_min_messages to warning;
|
|
|
|
|
create extension if not exists pgtap;
|
|
|
|
|
reset client_min_messages;
|
|
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
|
select plan(16);
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
|
|
|
|
set search_path to auth, numerus, public;
|
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
|
select has_function('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]']);
|
|
|
|
|
select function_lang_is('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'plpgsql');
|
|
|
|
|
select function_returns('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'uuid');
|
|
|
|
|
select isnt_definer('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]']);
|
|
|
|
|
select volatility_is('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'volatile');
|
|
|
|
|
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'guest', array []::text[]);
|
|
|
|
|
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
|
|
|
|
|
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'admin', array ['EXECUTE']);
|
|
|
|
|
select function_privs_are('numerus', 'add_product', array ['integer', 'text', 'text', 'text', 'integer[]', 'tag_name[]'], 'authenticator', array []::text[]);
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set client_min_messages to warning;
|
2023-03-26 11:51:57 +00:00
|
|
|
|
truncate product_tag cascade;
|
|
|
|
|
truncate tag cascade;
|
2023-02-14 11:39:54 +00:00
|
|
|
|
truncate product_tax cascade;
|
|
|
|
|
truncate product cascade;
|
|
|
|
|
truncate tax cascade;
|
2023-02-28 11:02:27 +00:00
|
|
|
|
truncate tax_class cascade;
|
2023-03-04 21:15:52 +00:00
|
|
|
|
truncate payment_method cascade;
|
2023-02-14 11:39:54 +00:00
|
|
|
|
truncate company cascade;
|
|
|
|
|
reset client_min_messages;
|
|
|
|
|
|
2023-03-04 21:15:52 +00:00
|
|
|
|
set constraints "company_default_payment_method_id_fkey" deferred;
|
2023-02-14 11:39:54 +00:00
|
|
|
|
|
2023-03-04 21:15:52 +00:00
|
|
|
|
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)
|
|
|
|
|
, (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', 222)
|
2023-02-14 11:39:54 +00:00
|
|
|
|
;
|
|
|
|
|
|
2023-03-04 21:15:52 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2023-02-28 11:02:27 +00:00
|
|
|
|
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)
|
2023-02-14 11:39:54 +00:00
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
2023-03-26 11:51:57 +00:00
|
|
|
|
$$ select add_product(1, 'Product 1', 'Description 1', '12.12', array[3, 4], array['tag1']) $$,
|
2023-02-14 11:39:54 +00:00
|
|
|
|
'Should be able to add product to first company'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
2023-03-26 11:51:57 +00:00
|
|
|
|
$$ select add_product(2, 'Product 2', 'Description 2', '24.24', array[6], array['tag1', 'tag2']) $$,
|
2023-02-14 11:39:54 +00:00
|
|
|
|
'Should be able to add product to second company'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
2023-03-26 11:51:57 +00:00
|
|
|
|
$$ select add_product(2, 'Product 3', 'Description 3', '36.36', array[]::integer[], array[]::tag_name[]) $$,
|
2023-02-14 11:39:54 +00:00
|
|
|
|
'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'
|
|
|
|
|
);
|
|
|
|
|
|
2023-03-26 11:51:57 +00:00
|
|
|
|
select bag_eq(
|
|
|
|
|
$$ select company_id, name from tag $$,
|
|
|
|
|
$$ values (1, 'tag1')
|
|
|
|
|
, (2, 'tag1')
|
|
|
|
|
, (2, 'tag2')
|
|
|
|
|
$$,
|
|
|
|
|
'Should have added all new tags once'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select bag_eq(
|
|
|
|
|
$$ select product.name as product_name, tag.name as tag_name from product_tag join product using (product_id) join tag using (tag_id) $$,
|
|
|
|
|
$$ values ('Product 1', 'tag1')
|
|
|
|
|
, ('Product 2', 'tag1')
|
|
|
|
|
, ('Product 2', 'tag2')
|
|
|
|
|
$$,
|
|
|
|
|
'Should have assigned the tags to products'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
2023-02-14 11:39:54 +00:00
|
|
|
|
select *
|
|
|
|
|
from finish();
|
|
|
|
|
|
|
|
|
|
rollback;
|