-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion_test.go
97 lines (77 loc) · 2.44 KB
/
region_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
package ebird
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegionInfo(t *testing.T) {
input := `{
"bounds": {"minX": -125.0, "maxX": -66.934570, "minY": 24.396308, "maxY": 49.384358},
"result": "Success",
"code": "US",
"type": "country",
"longitude": -95.712891,
"latitude": 37.09024
}`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, "/ref/region/info/US", r.URL.Path)
w.WriteHeader(http.StatusOK)
w.Write([]byte(input))
}))
defer server.Close()
client, err := NewClient("test-api-key", WithBaseURL(server.URL+"/"))
require.NoError(t, err)
ctx := context.Background()
got, err := client.RegionInfo(ctx, "US")
require.NoError(t, err)
var want RegionInfo
err = json.Unmarshal([]byte(input), &want)
require.NoError(t, err)
assert.Equal(t, &want, got)
}
func TestSubRegionList(t *testing.T) {
input := `[
{"code": "US-TX", "name": "Texas"},
{"code": "US-CA", "name": "California"}
]`
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
assert.Equal(t, "/ref/region/list/subnational1/US", r.URL.Path)
w.WriteHeader(http.StatusOK)
w.Write([]byte(input))
}))
defer server.Close()
client, err := NewClient("test-api-key", WithBaseURL(server.URL+"/"))
require.NoError(t, err)
ctx := context.Background()
got, err := client.SubRegionList(ctx, "subnational1", "US")
require.NoError(t, err)
var want []SubRegion
err = json.Unmarshal([]byte(input), &want)
require.NoError(t, err)
assert.Equal(t, want, got)
}
func TestRegionInfoWithEmptyRegionCode(t *testing.T) {
client, err := NewClient("test-api-key")
require.NoError(t, err)
ctx := context.Background()
_, err = client.RegionInfo(ctx, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "regionCode cannot be empty")
}
func TestSubRegionListWithEmptyParameters(t *testing.T) {
client, err := NewClient("test-api-key")
require.NoError(t, err)
ctx := context.Background()
_, err = client.SubRegionList(ctx, "", "US")
assert.Error(t, err)
assert.Contains(t, err.Error(), "regionType cannot be empty")
_, err = client.SubRegionList(ctx, "subnational1", "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "parentRegionCode cannot be empty")
}