-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transferMoney.sql
More file actions
125 lines (94 loc) · 3.11 KB
/
test_transferMoney.sql
File metadata and controls
125 lines (94 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Unit tests for transferMoney() function
* by Victor Correa */
-- Load pgTAP if not already loaded on our database
CREATE EXTENSION IF NOT EXISTS pgtap;
-- Start the test set
BEGIN;
SELECT plan(10);
-- Create test schema test envirnoment
CREATE SCHEMA IF NOT EXISTS test_env;
SET search_path TO test_env, public;
-- Testdatabase table definitions
CREATE TABLE accounts (
account_id SERIAL PRIMARY KEY,
balance NUMERIC(12,2) NOT NULL,
currency TEXT NOT NULL
);
CREATE TABLE transactions (
id SERIAL PRIMARY KEY,
from_account INT,
to_account INT,
amount NUMERIC(12,2),
description TEXT,
created_at TIMESTAMP DEFAULT NOW()
);
-- Load the function transferMoney under test
\i database/procedures/transferMoney.sql
-- Seed some sample data to be tested
INSERT INTO accounts (balance, currency) VALUES
/* Account ID 1, 2 and 3 and 3 set with a different currency */
(100.00, 'USD'),
(50.00, 'USD'),
(10.00, 'EUR');
/* Test definitions */
-- Test 1: Test a Successful transfer
SELECT lives_ok(
$$ SELECT transferMoney(1, 2, 30.00, 'Test transfer'); $$,
'Transfer between accounts 1 and 2 should succeed'
);
-- Tests 2 and 3: Assert correct balances
SELECT is(
(SELECT balance FROM accounts WHERE account_id = 1),
70.00::numeric,
'Account 1 should have 70.00'
);
SELECT is(
(SELECT balance FROM accounts WHERE account_id = 2),
80.00::numeric,
'Account 2 should have 80.00'
);
-- Test 4: Check if Insufficient funds error is working properly
SELECT throws_ok(
$$ SELECT transferMoney(2, 1, 999.00, 'Too big'); $$,
'Insufficient funds in account 2, balance: 80.00, attempted: 999.00',
'Should throw on insufficient balance'
);
-- Test 5: Check if balance remains unchanged after a fail transaction
SELECT is(
(SELECT balance FROM accounts WHERE account_id = 2),
80.00::numeric,
'Account 2 balance unchanged after failed transfer'
);
-- Test 6: Try to make a negative transfer and check the error
SELECT throws_ok(
$$ SELECT transferMoney(1, 2, -5.00, 'Bad'); $$,
'Transfer amount must be positive',
'Should throw on negative transfer amount'
);
-- Test 7: Different currency, just to assure it (but won't be used)
SELECT throws_ok(
$$ SELECT transferMoney(1, 3, 10.00, 'Wrong currency'); $$,
'Currency mismatch: USD vs EUR',
'Should throw when accounts have different currencies'
);
-- Test 8: If the transaction already exists
SELECT isnt_empty(
$$ SELECT * FROM transactions WHERE amount = 30 AND description = 'Test transfer' $$,
'Transaction should be recorded'
);
-- Test 9: Check if the source account exists
SELECT throws_ok(
$$ SELECT transferMoney(999, 1, 10, 'Invalid'); $$,
'Source account 999 does not exist',
'Should throw on non-existing source account'
);
-- Test 10: Checks if the destination account exists
SELECT throws_ok(
$$ SELECT transferMoney(1, 999, 10, 'Invalid'); $$,
'Destination account 999 does not exist',
'Should throw on non-existing destination account'
);
/* Finish test */
SELECT * FROM finish();
/* Rolls back all changes made during test */
ROLLBACK;