numerus/test/tag_invoice.sql

131 lines
4.1 KiB
PL/PgSQL

-- Test tag_invoice
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(17);
set search_path to numerus, auth, public;
select has_function('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]']);
select function_lang_is('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'sql');
select function_returns('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'void');
select isnt_definer('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]']);
select volatility_is('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'volatile');
select function_privs_are('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'tag_invoice', array ['integer', 'integer', 'tag_name[]'], 'authenticator', array []::text[]);
set client_min_messages to warning;
truncate invoice_tag cascade;
truncate tag cascade;
truncate invoice cascade;
truncate contact 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')
, (112, 1, 'bank', 'send money to my bank account')
;
set constraints "company_default_payment_method_id_fkey" immediate;
insert into tag (tag_id, company_id, name)
values (10, 1, 'tag1')
, (11, 1, 'tag2')
;
-- tag_invoice uses the sequence and sometimes it would confict
alter sequence tag_tag_id_seq restart with 15;
insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)
values (12, 1, 'Contact 2.1', 'XX555', '', '777-777-777', 'c@c', '', '', '', '', '', 'ES')
, (13, 1, 'Contact 2.2', 'XX666', '', '888-888-888', 'd@d', '', '', '', '', '', 'ES')
;
insert into invoice (invoice_id, company_id, slug, invoice_number, invoice_date, contact_id, payment_method_id, currency_code)
values (15, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'INV1', '2023-03-10', 12, 111, 'EUR')
, (16, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'INV2', '2023-03-09', 13, 111, 'EUR')
;
insert into invoice_tag (invoice_id, tag_id)
values (15, 10)
, (16, 11)
;
prepare current_tags as
select invoice_id, tag.name
from invoice
join invoice_tag using (invoice_id)
join tag using (tag_id);
select lives_ok(
$$ select tag_invoice(1, 15, array['tag1']) $$,
'Should be able to keep the same tags to the invoice'
);
select bag_eq(
'current_tags',
$$ values (15, 'tag1')
, (16, 'tag2')
$$,
'Should not have changed any invoice tag'
);
select lives_ok(
$$ select tag_invoice(1, 15, array['tag1', 'tag2']) $$,
'Should be able to add tag2 invoice'
);
select bag_eq(
'current_tags',
$$ values (15, 'tag1')
, (15, 'tag2')
, (16, 'tag2')
$$,
'Should have added tag2 to invoice'
);
select lives_ok(
$$ select tag_invoice(1, 16, array['tag3']) $$,
'Should be able to replace all tags of an invoice with a new one'
);
select bag_eq(
'current_tags',
$$ values (15, 'tag1')
, (15, 'tag2')
, (16, 'tag3')
$$,
'Should have set tag3 to invoice'
);
select lives_ok(
$$ select tag_invoice(1, 15, array[]::tag_name[]) $$,
'Should be able to remove all tags from an invoice'
);
select bag_eq(
'current_tags',
$$ values (16, 'tag3')
$$,
'Should have remove all tags from invoice'
);
select *
from finish();
rollback;