-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_live_test.go
209 lines (187 loc) · 4.77 KB
/
client_live_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
package cvr_test
import (
"os"
"testing"
"github.com/cvr-dev/cvr.dev-go"
"github.com/stretchr/testify/require"
)
func getAPIKey(t *testing.T) string {
apiKey, ok := os.LookupEnv("CVR_DEV_TEST_API_KEY")
require.True(t, ok, "failed to find valid API key in ENV")
return apiKey
}
// TestTestAPIKey verifies that the client sends requests to the backend and
// correctly interprets the responses sent.
func TestTestAPIKey(t *testing.T) {
if testing.Short() {
t.Skip("Running short tests")
}
tests := map[string]struct {
apiKey string
expected error
}{
"valid api key": {
apiKey: getAPIKey(t),
expected: nil,
},
"invalid api key": {
apiKey: "invalid api key",
expected: cvr.ErrUnauthorized,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
c := cvr.NewClient(test.apiKey)
err := c.TestAPIKey()
require.Equal(t, test.expected, err)
})
}
}
// TestCVRVirksomhedByNavn verifies that the client correctly sends requests to
// the backend and correctly interprets the responses sent.
func TestCVRVirksomhedByNavn(t *testing.T) {
if testing.Short() {
t.Skip("Running short tests")
}
tests := map[string]struct {
navn string
assert func([]cvr.Virksomhed) bool
expected error
}{
"multiple virksomheder": {
navn: "statsministeriet",
assert: func(vs []cvr.Virksomhed) bool {
return len(vs) > 0
},
expected: nil,
},
"not found": {
navn: "kfgnkjdfgkjdgkdfhgkdjhgkdjgkjdfhgkjdfkdfngkdfjn",
assert: func(vs []cvr.Virksomhed) bool {
return len(vs) == 0
},
expected: cvr.ErrNotFound,
},
}
apiKey := getAPIKey(t)
for name, test := range tests {
t.Run(name, func(t *testing.T) {
c := cvr.NewClient(apiKey)
vs, err := c.CVRVirksomhederByNavn(test.navn)
require.True(t, test.assert(vs))
require.IsType(t, test.expected, err)
})
}
}
// TestCVRVirksomhedByCVRNumre verifies that the client correctly sends requests
// to the backend and correctly interprets the responses sent.
func TestCVRVirksomhedByCVRNumre(t *testing.T) {
if testing.Short() {
t.Skip("Running short tests")
}
tests := map[string]struct {
cvrNumre []int
expected error
}{
"one virksomhed": {
cvrNumre: []int{10103940},
expected: nil,
},
"multiple virksomheder": {
cvrNumre: []int{10103940, 10150817, 10213231},
expected: nil,
},
"not found": {
cvrNumre: []int{1337},
expected: cvr.ErrNotFound,
},
}
apiKey := getAPIKey(t)
for name, test := range tests {
t.Run(name, func(t *testing.T) {
c := cvr.NewClient(apiKey)
vs, err := c.CVRVirksomhederByCVRNumre(test.cvrNumre...)
require.Equal(t, test.expected, err)
if test.expected == nil {
require.Equal(t, len(test.cvrNumre), len(vs))
for i, v := range vs {
require.Equal(t, test.cvrNumre[i], v.CVRNummer)
}
}
})
}
}
// TestProduktionsenhederByPNumre verifies that the client correctly sends
// requests to the backend and correctly interprets the responses sent.
func TestProduktionsenhederByPNumre(t *testing.T) {
if testing.Short() {
t.Skip("Running short tests")
}
tests := map[string]struct {
pNumre []int
expected error
}{
"one produktionsenhed": {
pNumre: []int{1004862579},
expected: nil,
},
"multiple produktionsenheder": {
pNumre: []int{1004862579, 1003388394, 1020852379},
expected: nil,
},
"not found": {
pNumre: []int{1337},
expected: cvr.ErrNotFound,
},
}
apiKey := getAPIKey(t)
for name, test := range tests {
t.Run(name, func(t *testing.T) {
c := cvr.NewClient(apiKey)
vs, err := c.CVRProduktionsenhederByPNumre(test.pNumre...)
require.Equal(t, test.expected, err)
if test.expected == nil {
require.Equal(t, len(test.pNumre), len(vs))
for i, v := range vs {
require.Equal(t, test.pNumre[i], v.PNummer)
}
}
})
}
}
// TestCVRProduktionsenhedByAdresse verifies that the client correctly sends
// requests to the backend and correctly interprets the responses sent.
func TestCVRProduktionsenhedByAdresse(t *testing.T) {
if testing.Short() {
t.Skip("Running short tests")
}
tests := map[string]struct {
adresse string
assert func([]cvr.Produktionsenhed) bool
expected error
}{
"multiple produktionsenheder": {
adresse: "Prins Jørgens Gård 11",
assert: func(vs []cvr.Produktionsenhed) bool {
return len(vs) > 0
},
expected: nil,
},
"not found": {
adresse: "kfgnkjdfgkjdgkdfhgkdjhgkdjgkjdfhgkjdfkdfngkdfjn",
assert: func(vs []cvr.Produktionsenhed) bool {
return len(vs) == 0
},
expected: cvr.ErrNotFound,
},
}
apiKey := getAPIKey(t)
for name, test := range tests {
t.Run(name, func(t *testing.T) {
c := cvr.NewClient(apiKey)
ps, err := c.CVRProduktionsenhederByAdresse(test.adresse)
require.IsType(t, test.expected, err)
require.True(t, test.assert(ps))
})
}
}