BEGIN;
    -- Load the TAP functions.
    CREATE EXTENSION pgtap;
    SET client_min_messages TO warning;

    -- Plan the tests.

    SELECT plan(20);

    -- Add data

    \i xt/data/42-pg/Base.sql

    -- Validate required tables

    SELECT has_table('acc_trans');
    SELECT has_table('ac_tax_form');
    SELECT has_table('ap');
    SELECT has_table('ar');
    SELECT has_table('entity_credit_account');
    SELECT has_table('entity');
    SELECT has_table('gl');

    -- Validate required functions

    SELECT has_function('draft__search',ARRAY['text','text','date','date','numeric','numeric']);
    SELECT has_function('draft_approve',ARRAY['integer']);
    SELECT has_function('draft_delete',ARRAY['integer']);

    -- Set specific data

    INSERT INTO entity (id, name, country_id)
    VALUES (-1000, '__TEST', 243);

    INSERT INTO entity_credit_account (id, meta_number, entity_class, entity_id, ar_ap_account_id, curr)
    VALUES (-1000, '_testv', 1, -1000, -1000, 'XTS');
    INSERT INTO entity_credit_account (id, meta_number, entity_class, entity_id, ar_ap_account_id, curr)
    VALUES (-1001, '_testc', 2, -1000, -1000, 'XTS');
    SELECT account__save
           (NULL, '00001', 'test only', 'A', NULL, (select id from account_heading limit 1), null, FALSE, FALSE,'{}', false,
           false);
    SELECT account__save
           (NULL, '00002', 'test only', 'A', NULL, (select id from account_heading limit 1), null, FALSE, FALSE,'{}', false,
           false);
    INSERT INTO ap (invnumber, entity_credit_account, amount_bc, netamount_bc,
            amount_tc, netamount_tc, approved, curr)
    select '_TEST AP', -1000, '100', '100', '100', '100', FALSE, 'XTS';

    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '100', 'XTS', 100, TRUE FROM account WHERE accno = '00001';
    INSERT INTO ac_tax_form (entry_id, reportable)
    VALUES (currval('acc_trans_entry_id_seq')::int, true);
    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '-100', 'XTS', -100, TRUE FROM account WHERE accno = '00002';
    INSERT INTO ac_tax_form (entry_id, reportable)
    VALUES (currval('acc_trans_entry_id_seq')::int, false);

    INSERT INTO ar (invnumber, entity_credit_account, amount_bc, netamount_bc,
           amount_tc, netamount_tc, approved, curr)
    select '_TEST AR', -1001, '100', '100', 100, 100, FALSE, 'XTS';

    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '-100', 'XTS', -100, TRUE FROM account WHERE accno = '00001';
    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '100', 'XTS', 100, TRUE FROM account WHERE accno = '00002';

    INSERT INTO gl (reference, description, approved)
    VALUES ('_TEST GL', 'Testing GL Drafts', false);

    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '-100', 'XTS', -100, TRUE FROM account WHERE accno = '00001';
    INSERT INTO acc_trans (chart_id, trans_id, amount_bc, curr, amount_tc, approved)
    SELECT id, currval('id'), '100', 'XTS', 100, TRUE FROM account WHERE accno = '00002';

    -- Run tests

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('ap',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST AP';
    SELECT results_eq('test',ARRAY[true],'"ap" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('AP',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST AP';
    SELECT results_eq('test',ARRAY[true],'"AP" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT draft_delete(id)
                    FROM draft__search('AP',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST AP';
    SELECT results_eq('test',ARRAY[true],'"AP" delete successful (w/1099)');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('ar',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST AR';
    SELECT results_eq('test',ARRAY[true],'"ar" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('AR',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST AR';
    SELECT results_eq('test',ARRAY[true],'"AR" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('gl',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST GL';
    SELECT results_eq('test',ARRAY[true],'"gl" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
                    FROM draft__search('GL',  NULL, NULL, NULL, NULL, NULL)
                    WHERE reference = '_TEST GL';
    SELECT results_eq('test',ARRAY[true],'"GL" search successful');
    DEALLOCATE test;

    PREPARE test AS SELECT draft_delete(currval('id')::int);
    SELECT results_eq('test',ARRAY[true],'gl draft deletion');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 0 from gl where id = currval('id');
    SELECT results_eq('test',ARRAY[true],'gl table cleanup');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 0 from acc_trans where trans_id = currval('id');
    SELECT results_eq('test',ARRAY[true],'acc_trans table cleanup');
    DEALLOCATE test;

    -- Finish the tests and clean up.
    SELECT * FROM finish();

ROLLBACK;
