LP#800478 pgtap test to confirm error condition
authorBill Erickson <berick@esilibrary.com>
Tue, 25 Feb 2014 16:30:18 +0000 (11:30 -0500)
committerBill Erickson <berick@esilibrary.com>
Mon, 12 May 2014 14:12:30 +0000 (10:12 -0400)
This test demonstrates a problem with acq.fund_transfer, where the final
amounts in the allocation and transfer are incorrect.

When the transfer is successful, mutiple rows are added to
acq.fund_transfer and acq.fund_allocation.  Accommodate those in the
test.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Open-ILS/src/sql/Pg/t/acq_fund_transfer.pg [new file with mode: 0644]

diff --git a/Open-ILS/src/sql/Pg/t/acq_fund_transfer.pg b/Open-ILS/src/sql/Pg/t/acq_fund_transfer.pg
new file mode 100644 (file)
index 0000000..a898af8
--- /dev/null
@@ -0,0 +1,78 @@
+BEGIN;
+
+SELECT plan(2);
+
+INSERT INTO acq.funding_source (name, owner, currency_type, code) 
+    VALUES ('FS1', 1, 'USD', 'FS1');
+
+INSERT INTO acq.funding_source_credit (funding_source, amount, deadline_date ) 
+VALUES 
+    (
+        (SELECT id FROM acq.funding_source WHERE name = 'FS1'), 
+        300, 
+        NOW() + '1 DAY'::INTERVAL
+    ), (
+        (SELECT id FROM acq.funding_source WHERE name = 'FS1'), 
+        500, 
+        NULL -- no deadline_date ensures this credit will be used first
+             -- for transfers, giving us a predictable (testable) outcome.
+    ); 
+
+INSERT INTO acq.fund (org, name, year, currency_type, active) VALUES 
+    (1, 'F1', '2014', 'USD', TRUE),
+    (1, 'F2', '2014', 'USD', TRUE);
+
+-- LP#800478
+-- allocation must exceed transfer amount but be less than total credits
+INSERT INTO acq.fund_allocation 
+    (funding_source, fund, amount, allocator, note) 
+VALUES (
+    (SELECT id FROM acq.funding_source WHERE name = 'FS1'),
+    (SELECT id FROM acq.fund WHERE name = 'F1'),
+    700 , 1, 'test'
+); 
+
+-- LP#800478
+-- transfer amount must exceed any one funding source credit but be less
+-- than the allocation amount
+SELECT acq.transfer_fund(
+    (SELECT id FROM acq.fund WHERE name = 'F1'), 600,
+    (SELECT id FROM acq.fund WHERE name = 'F2'), 600, 1, 'test'
+);
+
+-- fund transfer should show 600 moved between funds
+-- 500 from the 500 credit, 100 from the 300 credit
+SELECT is(
+    (
+        SELECT ARRAY_AGG(dest_amount) FROM (
+            SELECT dest_amount FROM acq.fund_transfer 
+            WHERE 
+                src_fund = (SELECT id FROM acq.fund WHERE name = 'F1') AND
+                dest_fund = (SELECT id FROM acq.fund WHERE name = 'F2')
+            ORDER BY dest_amount
+        ) dest_amount
+    ),
+    '{100.00,500.00}',
+    'Transfer amount should be 100 and 500'
+);
+
+-- destination fund should have a 600 allocation
+SELECT is(
+    (
+        SELECT ARRAY_AGG(amount) FROM (
+            SELECT amount FROM acq.fund_allocation
+            WHERE 
+                funding_source = 
+                    (SELECT id FROM acq.funding_source WHERE name = 'FS1') AND
+                fund = (SELECT id FROM acq.fund WHERE name = 'F2')
+            ORDER BY amount
+        ) amount
+    ),
+    '{100.00,500.00}',
+    'Allocation amount should be 100 and 500'
+);
+
+ROLLBACK;
+
+
+-- vim: ft=sql