-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate_test.go
214 lines (202 loc) · 6.54 KB
/
validate_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package vat
import (
"strconv"
"testing"
"github.com/golang/mock/gomock"
"github.com/teamwork/vat/v3/mocks"
)
var tests = []struct {
number string
expectedError error
}{
{"", ErrInvalidVATNumberFormat},
{"A", ErrInvalidVATNumberFormat},
{"AB123A01", ErrInvalidCountryCode},
{"ATU12345678", nil},
{"ATU15673009", nil},
{"ATU1234567", ErrInvalidVATNumberFormat},
{"BE0123456789", nil},
{"BE1234567891", nil},
{"BE0999999999", nil},
{"BE9999999999", nil},
{"BE012345678", ErrInvalidVATNumberFormat},
{"BE123456789", ErrInvalidVATNumberFormat},
{"BG123456789", nil},
{"BG1234567890", nil},
{"BG1234567", ErrInvalidVATNumberFormat},
{"CHE-156.730.098 MWST", nil},
{"CHE-156.730.098", nil},
{"CHE156730098MWST", nil},
{"CHE156730098", nil},
{"CY12345678X", nil},
{"CY15673009L", nil},
{"CY1234567X", ErrInvalidVATNumberFormat},
{"CZ12345678", nil},
{"CZ1234567", ErrInvalidVATNumberFormat},
{"DE123456789", nil},
{"DE12345678", ErrInvalidVATNumberFormat},
{"DK12345678", nil},
{"DK1234567", ErrInvalidVATNumberFormat},
{"EE123456789", nil},
{"EE12345678", ErrInvalidVATNumberFormat},
{"EL123456789", nil},
{"EL12345678", ErrInvalidVATNumberFormat},
{"ESX12345678", nil},
{"ESX1234567", ErrInvalidVATNumberFormat},
{"FI1234567", ErrInvalidVATNumberFormat},
{"FI12345678", nil},
{"FR12345678901", nil},
{"FR1234567890", ErrInvalidVATNumberFormat},
{"GB999999973", nil},
{"GB156730098481", nil},
{"GBGD549", ErrInvalidVATNumberFormat},
{"GBHA549", ErrInvalidVATNumberFormat},
{"GB99999997", ErrInvalidVATNumberFormat},
{"HU12345678", nil},
{"HU1234567", ErrInvalidVATNumberFormat},
{"HR12345678901", nil},
{"HR1234567890", ErrInvalidVATNumberFormat},
{"IE1234567X", nil},
{"IE123456X", ErrInvalidVATNumberFormat},
{"IT12345678901", nil},
{"IT1234567890", ErrInvalidVATNumberFormat},
{"LT123456789", nil},
{"LT12345678", ErrInvalidVATNumberFormat},
{"LU26375245", nil},
{"LU12345678", nil},
{"LU1234567", ErrInvalidVATNumberFormat},
{"LV12345678901", nil},
{"LV1234567890", ErrInvalidVATNumberFormat},
{"MT12345678", nil},
{"MT1234567", ErrInvalidVATNumberFormat},
{"NL123456789B01", nil},
{"NL123456789B12", nil},
{"NL12345678B12", ErrInvalidVATNumberFormat},
{"PL1234567890", nil},
{"PL123456789", ErrInvalidVATNumberFormat},
{"PT123456789", nil},
{"PT12345678", ErrInvalidVATNumberFormat},
{"RO123456789", nil},
{"RO1", ErrInvalidVATNumberFormat}, // Romania has a really weird VAT format...
{"SE123456789012", nil},
{"SE12345678901", ErrInvalidVATNumberFormat},
{"SI12345678", nil},
{"SI1234567", ErrInvalidVATNumberFormat},
{"SK1234567890", nil},
{"SK123456789", ErrInvalidVATNumberFormat},
{"KL123456789B12", ErrInvalidCountryCode},
{"NL12343678B12", ErrInvalidVATNumberFormat},
{"PL1224567890", nil},
{"PL123456989", ErrInvalidVATNumberFormat},
{"PT125456789", nil},
{"PT16345678", ErrInvalidVATNumberFormat},
{"RO123456789", nil},
{"KT123456789", ErrInvalidCountryCode},
{"LT12335678", ErrInvalidVATNumberFormat},
{"LU26275245", nil},
{"LU14345678", nil},
{"LU1234567", ErrInvalidVATNumberFormat},
{"LQ12345678901", ErrInvalidCountryCode},
{"QE123456789", ErrInvalidCountryCode},
{"DE12375670", ErrInvalidVATNumberFormat},
{"DK123365678", ErrInvalidVATNumberFormat},
{"DO1231567", ErrInvalidCountryCode},
{"EE123456789", nil},
{"EL123434678", nil},
{"EL122456789", nil},
{"EL12245678", ErrInvalidVATNumberFormat},
{"CY1134567X", ErrInvalidVATNumberFormat},
{"CZ17345678", nil},
{"CZ1239567", ErrInvalidVATNumberFormat},
{"DE123456789", nil},
{"DE12325678", ErrInvalidVATNumberFormat},
{"DK12385678", nil},
{"DK1234767", ErrInvalidVATNumberFormat},
{"EE123456689", nil},
{"EE12343678", ErrInvalidVATNumberFormat},
{"EL123426789", nil},
{"EL12342678", ErrInvalidVATNumberFormat},
{"ESX12315678", nil},
{"ESX1634567", ErrInvalidVATNumberFormat},
{"FI1274567", ErrInvalidVATNumberFormat},
{"FI12385678", nil},
{"FR12349678901", nil},
{"FR1234597890", ErrInvalidVATNumberFormat},
{"GB999979973", nil},
{"GB156730984812", nil},
{"GBGD529", ErrInvalidVATNumberFormat},
{"GBHA519", ErrInvalidVATNumberFormat},
{"GB99997997", ErrInvalidVATNumberFormat},
{"GB15673098481", ErrInvalidVATNumberFormat},
{"HU12342678", nil},
{"HU1234167", ErrInvalidVATNumberFormat},
{"HR12341678901", nil},
{"HR1234267890", ErrInvalidVATNumberFormat},
{"IE1234167X", nil},
{"IE123356X", ErrInvalidVATNumberFormat},
{"IT12245678901", nil},
{"IT122456789013333", ErrInvalidVATNumberFormat},
{"IT1231567890", ErrInvalidVATNumberFormat},
{"LT123156789", nil},
{"LT12343678", ErrInvalidVATNumberFormat},
{"LU26371245", nil},
{"LU12325678", nil},
{"LU1214567", ErrInvalidVATNumberFormat},
{"LV12345578901", nil},
{"LV1234367890", ErrInvalidVATNumberFormat},
{"MT12325678", nil},
{"MT1234167", ErrInvalidVATNumberFormat},
{"NL123156789B01", nil},
{"NL113456789B12", nil},
{"NL12315678B12", ErrInvalidVATNumberFormat},
{"PO1234567890", ErrInvalidCountryCode},
}
func BenchmarkValidateFormat(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ValidateFormat("NL" + strconv.Itoa(i))
}
}
func TestValidateFormat(t *testing.T) {
for _, test := range tests {
err := ValidateFormat(test.number)
if err != test.expectedError {
t.Errorf("Expected <%v> for %v, got <%v>", test.expectedError, test.number, err)
}
}
}
func TestValidateExists(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockViesService := mocks.NewMockLookupServiceInterface(ctrl)
mockUKVATService := mocks.NewMockLookupServiceInterface(ctrl)
ViesLookupService = mockViesService
UKVATLookupService = mockUKVATService
defer restoreLookupServices()
var lookupTests = []struct {
vatNumber string
service *mocks.MockLookupServiceInterface
expectedError error
}{
{"BE0472429986", mockViesService, ErrVATNumberNotFound},
{"NL123456789B01", mockViesService, nil},
{"Hi", mockViesService, ErrInvalidVATNumberFormat},
{"INVALID INPUT", mockViesService, ErrInvalidVATNumberFormat},
{"BE0472429986", mockViesService, ErrServiceUnavailable{Err: nil}},
{"GB333289454", mockUKVATService, nil},
// XI is Northern Ireland, which while part of the UK is actually still validated by VIES
{"XI0472429986", mockViesService, nil},
}
for _, test := range lookupTests {
if len(test.vatNumber) >= 3 {
test.service.EXPECT().Validate(test.vatNumber).Return(test.expectedError)
}
err := ValidateExists(test.vatNumber)
if err != test.expectedError {
t.Errorf("Expected <%v> for %v, got <%v>", test.expectedError, test.vatNumber, err)
}
}
}
func restoreLookupServices() {
ViesLookupService = &viesService{}
UKVATLookupService = &ukVATService{}
}