-- Test attach_to_expense set client_min_messages to warning; create extension if not exists pgtap; reset client_min_messages; begin; select plan(12); set search_path to auth, numerus, public; select has_function('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea']); select function_lang_is('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'sql'); select function_returns('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'void'); select isnt_definer('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea']); select volatility_is('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'volatile'); select function_privs_are('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'guest', array []::text[]); select function_privs_are('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'invoicer', array ['EXECUTE']); select function_privs_are('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'admin', array ['EXECUTE']); select function_privs_are('numerus', 'attach_to_expense', array ['uuid', 'text', 'text', 'bytea'], 'authenticator', array []::text[]); set client_min_messages to warning; truncate expense_attachment cascade; 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') , (112, 1, 'bank', 'send money to my bank account') ; 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 (12, 1, 'Contact 2.1') , (13, 1, 'Contact 2.2') ; insert into expense (expense_id, company_id, slug, invoice_number, invoice_date, contact_id, amount, currency_code, tags) values (15, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'INV1', '2023-05-04', 12, 111, 'EUR', '{tag1}') , (16, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'INV2', '2023-05-05', 13, 222, 'EUR', '{tag2}') ; insert into expense_attachment (expense_id, original_filename, mime_type, content) values (16, 'something.txt', 'text/plain', convert_to('Once upon a time…', 'UTF-8')) ; select lives_ok( $$ select attach_to_expense('7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'expense.txt', 'text/plain', convert_to('Total 42€', 'UTF-8')) $$, 'Should be able to attach a document to the first expense' ); select lives_ok( $$ select attach_to_expense('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'expense.html', 'text/html', convert_to('

Total 42€

', 'UTF-8')) $$, 'Should be able to replace the second expense’s attachment with a new document' ); select bag_eq( $$ select expense_id, original_filename, mime_type, content from expense_attachment $$, $$ values (15, 'expense.txt', 'text/plain', convert_to('Total 42€', 'UTF-8')) , (16, 'expense.html', 'text/html', convert_to('

Total 42€

', 'UTF-8')) $$, 'Should have attached all documents' ); select * from finish(); rollback;