numerus/test/remove_collection.sql

147 lines
5.2 KiB
MySQL
Raw Permalink Normal View History

-- Test remove_collection
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;
begin;
select plan(16);
set search_path to numerus, public;
select has_function('numerus', 'remove_collection', array['uuid']);
select function_lang_is('numerus', 'remove_collection', array['uuid'], 'plpgsql');
select function_returns('numerus', 'remove_collection', array['uuid'], 'void');
select isnt_definer('numerus', 'remove_collection', array['uuid']);
select volatility_is('numerus', 'remove_collection', array['uuid'], 'volatile');
select function_privs_are('numerus', 'remove_collection', array ['uuid'], 'guest', array []::text[]);
select function_privs_are('numerus', 'remove_collection', array ['uuid'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'remove_collection', array ['uuid'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'remove_collection', array ['uuid'], 'authenticator', array []::text[]);
set client_min_messages to warning;
truncate invoice_collection;
truncate collection_attachment;
truncate collection cascade;
truncate invoice_product cascade;
truncate invoice cascade;
truncate contact_tax_details cascade;
truncate contact cascade;
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 contact (contact_id, company_id, name)
values ( 9, 1, 'Customer 1')
;
insert into contact_tax_details (contact_id, business_name, vatin, address, city, province, postal_code, country_code)
values (9, 'Customer 1', 'XX555', '', '', '', '', 'ES')
;
insert into invoice (invoice_id, company_id, invoice_number, contact_id, invoice_date, payment_method_id, currency_code, invoice_status)
values (13, 1, 'INV001', 9, '2011-01-11', 111, 'EUR', 'paid')
, (14, 1, 'INV002', 9, '2022-02-22', 111, 'EUR', 'paid')
, (15, 1, 'INV003', 9, '2022-02-22', 111, 'EUR', 'partial')
;
insert into invoice_product (invoice_product_id, invoice_id, name, price)
values (16, 13, 'P1', 111)
, (17, 14, 'P1', 111)
, (18, 14, 'P2', 111)
, (19, 15, 'P1', 111)
, (20, 15, 'P2', 111)
, (21, 15, 'P3', 111)
;
insert into payment_account (payment_account_id, company_id, payment_account_type, name)
values (11, 1, 'cash', 'Cash 1')
, (12, 1, 'cash', 'Cash 2')
, (13, 1, 'other', 'Other')
;
insert into collection (collection_id, company_id, slug, description, collection_date, payment_account_id, amount, currency_code, payment_status, tags)
values (16, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'Collection INV001', '2023-05-04', 12, 111, 'EUR', 'complete', '{tag1}')
, (17, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'First INV002', '2023-05-05', 13, 100, 'EUR', 'partial', '{tag2}')
, (18, 1, '3bdad7a8-4a1e-4ae0-b5c6-015e51ee0502', 'Second INV002', '2023-05-06', 13, 122, 'EUR', 'partial', '{tag1,tag3}')
, (19, 1, '5a524bee-8311-4d13-9adf-ef6310b26990', 'Partial INV003', '2023-05-07', 11, 123, 'EUR', 'partial', '{}')
;
insert into invoice_collection (invoice_id, collection_id)
values (13, 16)
, (14, 17)
, (14, 18)
, (15, 19)
;
insert into collection_attachment (collection_id, original_fileName, mime_type, content)
values (16, 'collection.txt', 'text/plain', convert_to('Pay 42', 'UTF-8'))
, (18, 'empty.html', 'text/html', convert_to('empty', 'UTF-8'))
, (19, 'collection.html', 'text/html', convert_to('<html> PAY <em>42</em></html>', 'UTF-8'))
;
select lives_ok(
$$ select remove_collection('7ac3ae0e-b0c1-4206-a19b-0be20835edd4') $$,
'Should be able to remove a complete collection'
);
select lives_ok(
$$ select remove_collection('5a524bee-8311-4d13-9adf-ef6310b26990') $$,
'Should be able to remove a partial collection, '
);
select lives_ok(
$$ select remove_collection('b57b980b-247b-4be4-a0b7-03a7819c53ae') $$,
'Should be able to remove a partial collection, leaving the invoices other partial collection'
);
select bag_eq(
$$ select description, collection_date::text, payment_account_id, amount, payment_status, tags::text from collection $$,
$$ values ('Second INV002', '2023-05-06', 13, 122, 'partial', '{tag1,tag3}')
$$,
'Should have deleted all given collections'
);
select bag_eq(
$$ select invoice_id, collection_id from invoice_collection$$,
$$ values (14, 18)
$$,
'Should have deleted all related invoices collections'
);
select bag_eq(
$$ select collection_id, original_filename from collection_attachment $$,
$$ values (18, 'empty.html') $$,
'Should have deleted all related attachments'
);
select bag_eq(
$$ select invoice_id, invoice_status from invoice $$,
$$ values (13, 'created')
, (14, 'partial')
, (15, 'created')
$$,
'Should have updated invoices too'
);
select *
from finish();
rollback;