-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_test.go
101 lines (81 loc) · 2.5 KB
/
account_test.go
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
package db
import (
"context"
"database/sql"
"testing"
"github.com/hhow09/simple_bank/util"
"github.com/stretchr/testify/require"
)
func createRandomAccount(t *testing.T) Account {
user := createRandomUser(t)
args := CreateAccountParams{
Owner: user.Username,
Balance: util.RandomMoney(),
Currency: util.RandomCurrency(),
}
account, err := testQueries.CreateAccount(context.Background(), args)
require.NoError(t, err)
require.NotEmpty(t, account)
require.Equal(t, args.Owner, account.Owner)
require.Equal(t, args.Balance, account.Balance)
require.Equal(t, args.Currency, account.Currency)
require.NotZero(t, account.ID)
require.NotZero(t, account.CreatedAt)
return account
}
func TestCreateAccount(t *testing.T) {
createRandomAccount(t)
}
func TestGetAccount(t *testing.T) {
acc := createRandomAccount(t)
accGet, err := testQueries.GetAccount(context.Background(), acc.ID)
require.NoError(t, err)
require.NotEmpty(t, accGet)
require.Equal(t, accGet.ID, acc.ID)
require.Equal(t, accGet.Owner, acc.Owner)
require.Equal(t, accGet.Balance, acc.Balance)
require.Equal(t, accGet.Currency, acc.Currency)
require.Equal(t, accGet.CreatedAt, acc.CreatedAt)
}
func TestUpdateAccount(t *testing.T) {
acc := createRandomAccount(t)
args := UpdateAccountParams{
ID: acc.ID,
Balance: util.RandomMoney(),
}
accUpdated, err := testQueries.UpdateAccount(context.Background(), args)
require.NoError(t, err)
require.NotEmpty(t, accUpdated)
require.Equal(t, accUpdated.ID, acc.ID)
require.Equal(t, accUpdated.Owner, acc.Owner)
require.Equal(t, accUpdated.Balance, args.Balance)
require.Equal(t, accUpdated.Currency, acc.Currency)
require.Equal(t, accUpdated.CreatedAt, acc.CreatedAt)
}
func TestDeleteAccount(t *testing.T) {
account1 := createRandomAccount(t)
err := testQueries.DeleteAccount(context.Background(), account1.ID)
require.NoError(t, err)
account2, err := testQueries.GetAccount(context.Background(), account1.ID)
require.Error(t, err)
require.EqualError(t, err, sql.ErrNoRows.Error())
require.Empty(t, account2)
}
func TestListAccounts(t *testing.T) {
var lastAccount Account
for i := 0; i < 10; i++ {
lastAccount = createRandomAccount(t)
}
arg := ListAccountsParams{
Owner: lastAccount.Owner,
Limit: 5,
Offset: 0,
}
accounts, err := testQueries.ListAccounts(context.Background(), arg)
require.NoError(t, err)
require.NotEmpty(t, accounts)
for _, account := range accounts {
require.NotEmpty(t, account)
require.Equal(t, lastAccount.Owner, account.Owner)
}
}