-- Test remove_expense set client_min_messages to warning; create extension if not exists pgtap; reset client_min_messages; begin; select plan(14); set search_path to numerus, auth, public; select has_function('numerus', 'remove_expense', array['uuid']); select function_lang_is('numerus', 'remove_expense', array['uuid'], 'plpgsql'); select function_returns('numerus', 'remove_expense', array['uuid'], 'void'); select isnt_definer('numerus', 'remove_expense', array['uuid']); select volatility_is('numerus', 'remove_expense', array['uuid'], 'volatile'); select function_privs_are('numerus', 'remove_expense', array ['uuid'], 'guest', array []::text[]); select function_privs_are('numerus', 'remove_expense', array ['uuid'], 'invoicer', array ['EXECUTE']); select function_privs_are('numerus', 'remove_expense', array ['uuid'], 'admin', array ['EXECUTE']); select function_privs_are('numerus', 'remove_expense', array ['uuid'], 'authenticator', array []::text[]); set client_min_messages to warning; truncate expense_tax; truncate expense_attachment; truncate expense cascade; truncate contact 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, 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 tax_class (tax_class_id, company_id, name) values (11, 1, '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) ; insert into contact (contact_id, company_id, name) values ( 9, 1, 'Customer 1') ; insert into expense (expense_id, company_id, slug, invoice_number, contact_id, invoice_date, amount, currency_code, expense_status) values (13, 1, '3816f5a2-0275-4a5f-9808-a180cf1e169d', 'INV001', 9, '2011-01-11', 111, 'EUR', 'paid') , (14, 1, '0af59ae8-df8f-44e8-ae76-b7af0d58b93d', 'INV002', 9, '2022-02-22', 222, 'EUR', 'paid') , (15, 1, 'cf548a2a-c7e9-4b0c-8fc2-c86e004f7aad', 'INV003', 9, '2022-02-22', 333, 'EUR', 'partial') ; insert into expense_tax (expense_id, tax_id, tax_rate) values (14, 4, 0.21) , (14, 3, -0.07) , (15, 4, 0.20) ; insert into expense_attachment (expense_id, original_filename, mime_type, content) values (14, 'something.txt', 'text/plain', convert_to('Once upon a time…', 'UTF-8')) , (15, 'else.txt', 'text/plain', convert_to('What do i know?', 'UTF-8')) ; select lives_ok( $$ select remove_expense('3816f5a2-0275-4a5f-9808-a180cf1e169d') $$, 'Should be able to remove an expense without taxes nor attachments' ); select lives_ok( $$ select remove_expense('0af59ae8-df8f-44e8-ae76-b7af0d58b93d') $$, 'Should be able to remove an expense with both taxes and attachments' ); select bag_eq( $$ select expense_id, invoice_number from expense $$, $$ values (15, 'INV003') $$, 'Should have deleted all given expenses' ); select bag_eq( $$ select expense_id, tax_id, tax_rate from expense_tax $$, $$ values (15, 4, 0.20) $$, 'Should have deleted all related taxes' ); select bag_eq( $$ select expense_id, original_filename from expense_attachment $$, $$ values (15, 'else.txt') $$, 'Should have deleted all related attachments' ); select * from finish(); rollback;