-
Notifications
You must be signed in to change notification settings - Fork 150
/
db_test.go
155 lines (148 loc) · 2.79 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
147
148
149
150
151
152
153
154
155
package money
import (
"database/sql/driver"
"fmt"
"reflect"
"testing"
)
func TestMoney_Value(t *testing.T) {
tests := []struct {
have *Money
separator string
want string
wantErr bool
}{
{
have: New(10, CAD),
separator: "|",
want: "10|CAD",
},
{
have: New(-10, USD),
separator: "+-+",
want: "-10+-+USD",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%#v", tt.have), func(t *testing.T) {
want := driver.Value(tt.want)
DBMoneyValueSeparator = tt.separator
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", got, want)
}
})
}
}
func TestMoney_Scan(t *testing.T) {
tests := []struct {
src interface{}
separator string
want *Money
wantErr bool
}{
{
src: "10|CAD",
want: New(10, CAD),
},
{
src: "20|USD",
want: New(20, USD),
},
{
src: "30000,IDR",
separator: ",",
want: New(30000, IDR),
},
{
src: "10|",
wantErr: true,
},
{
src: "|SAR",
wantErr: true,
},
{
src: "10",
wantErr: true,
},
{
src: "USD",
wantErr: true,
},
{
src: "USD|10",
wantErr: true,
},
{
src: "",
wantErr: true,
},
{
src: "a|b|c",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%#v", tt.src), func(t *testing.T) {
if tt.separator != "" {
DBMoneyValueSeparator = tt.separator
} else {
DBMoneyValueSeparator = DefaultDBMoneyValueSeparator
}
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>")
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)
}
})
}
}