forked from Rhymond/go-money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
146 lines (139 loc) · 2.94 KB
/
db_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package money
import (
"database/sql/driver"
"fmt"
"reflect"
"testing"
)
func TestMoney_Value(t *testing.T) {
tests := []struct {
have *Money
want []byte
wantErr bool
}{
{
have: New(10, CAD),
want: []byte(`{"amount":10,"currency":"CAD"}`),
},
{
have: New(-10, USD),
want: []byte(`{"amount":-10,"currency":"USD"}`),
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%#v", tt.have), func(t *testing.T) {
want := driver.Value(tt.want)
got, err := tt.have.Value()
if err != nil {
t.Errorf("Value() error = %v", err)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Value() got = %v, want %v", string((got).([]byte)), string((want).([]byte)))
}
})
}
}
func TestMoney_Scan(t *testing.T) {
tests := []struct {
name string
src interface{}
want *Money
wantErr bool
}{
{
name: "10 cad",
src: []byte(`{"amount":10,"currency":"CAD"}`),
want: New(10, CAD),
},
{
name: "20 usd",
src: []byte(`{"amount":20,"currency":"USD"}`),
want: New(20, USD),
},
{
name: "30.00 IDR",
src: []byte(`{"amount":30000,"currency":"IDR"}`),
want: New(30000, IDR),
},
{
name: "empty currency",
src: []byte(`{"amount":10,"currency":""}`),
wantErr: true,
},
{
name: "missing amount, parse error",
src: []byte(`{"amount":,"currency":"SAR"}`),
wantErr: true,
},
{
name: "missing currency",
src: []byte(`{"amount":10}`),
wantErr: true,
},
{
name: "missing amount",
src: []byte(`{"currency":"USD"}`),
// unfortunately, this is not an error
want: New(0, USD),
},
{
name: "empty",
src: "{}",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := &Money{}
if err := got.Scan(tt.src); (err != nil) != tt.wantErr {
t.Errorf("Scan() error = %v, wantErr %v", err, tt.wantErr)
return
}
if tt.wantErr {
return
}
if got == nil {
t.Errorf("money.Scan() result was <nil> %v", tt.want)
return
}
eq, err := tt.want.Equals(got)
if err != nil {
t.Errorf(err.Error())
}
if !eq {
t.Errorf("Value() got = %s %s, want %s %s", got.Display(), got.Currency().Code, tt.want.Display(), tt.want.Currency().Code)
}
})
}
}
func TestCurrency_Value(t *testing.T) {
for code, cc := range currencies {
t.Run(code, func(t *testing.T) {
want := driver.Value(code)
got, err := cc.Value()
if err != nil {
t.Errorf("Value() error = %v", err)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Value() got = %v, want %v", got, want)
}
})
}
}
func TestCurrency_Scan(t *testing.T) {
for code, want := range currencies {
t.Run(code, func(t *testing.T) {
src := interface{}(code)
got := &Currency{}
err := got.Scan(src)
if err != nil {
t.Errorf("Scan() error = %v", err)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Scan() got %#v, want %#v", got, want)
}
})
}
}