-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeozip_test.go
136 lines (128 loc) · 4.71 KB
/
geozip_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
package geozip_test
import (
"github.com/ngrash/geozip"
"net/http"
"os"
"testing"
)
type RoundTripperFunc func(*http.Request) (*http.Response, error)
func (fn RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return fn(req)
}
func TestFetchCountry_NotModified(t *testing.T) {
const requestEtag = "current_etag"
geozip.HTTPClient.Transport = RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
if got, want := r.URL.String(), "https://download.geonames.org/export/zip/DE.zip"; got != want {
t.Errorf("client requested %q, want %q", got, want)
}
if got, want := r.Method, http.MethodGet; got != want {
t.Errorf("client made %s request, want %s", got, want)
}
if got, want := r.Header.Get("If-None-Match"), requestEtag; got != want {
t.Errorf("client sent If-None-Match = %s, want %s", got, want)
}
return &http.Response{
StatusCode: http.StatusNotModified,
}, nil
})
entries, modified, newEtag, err := geozip.FetchCountry("de", requestEtag)
if entries != nil {
t.Errorf("entries = %v, want nil", entries)
}
if modified {
t.Error("modified = true, want false")
}
if got, want := newEtag, requestEtag; got != want {
t.Errorf("newEtag = %v, want %v", got, want)
}
if err != nil {
t.Error("err != nil")
}
}
func TestFetchCountry_Modified(t *testing.T) {
const (
requestEtag = "old_etag"
responseEtag = "new_etag"
)
geozip.HTTPClient.Transport = RoundTripperFunc(func(r *http.Request) (*http.Response, error) {
if got, want := r.URL.String(), "https://download.geonames.org/export/zip/DE.zip"; got != want {
t.Errorf("client requested %q, want %q", got, want)
}
if got, want := r.Method, http.MethodGet; got != want {
t.Errorf("client made %s request, want %s", got, want)
}
if got, want := r.Header.Get("If-None-Match"), requestEtag; got != want {
t.Errorf("client sent If-None-Match = %s, want %s", got, want)
}
file, err := os.Open("test_data/DE.zip")
if err != nil {
t.Fatal("open test data", err)
}
return &http.Response{
StatusCode: http.StatusOK,
Body: file,
Header: http.Header{
"Etag": []string{responseEtag},
},
}, nil
})
entries, modified, newEtag, err := geozip.FetchCountry("de", requestEtag)
if got, want := len(entries), 16477; got != want {
t.Errorf("len(entries) = %v, want %v", got, want)
}
if !modified {
t.Error("modified = false, want true")
}
if got, want := newEtag, responseEtag; got != want {
t.Errorf("newEtag = %v, want %v", got, want)
}
if err != nil {
t.Error("err != nil")
}
// Samples derived from GeoNames postal database. Licensed under Creative Commons Attribution 4.0 License,
// see https://creativecommons.org/licenses/by/4.0/.
samples := map[int][]string{
10351: {"DE", "54668", "Ferschweiler", "Rheinland-Pfalz", "RP", "", "00", "Eifelkreis Bitburg-Prüm", "07232", "49.8667", "6.4", "4"},
11193: {"DE", "56479", "Neustadt (Westerwald)", "Rheinland-Pfalz", "RP", "", "00", "Westerwaldkreis", "07143", "50.6333", "8.0333", ""},
}
for line, fields := range samples {
idx := line - 1
entry := entries[idx]
if got, want := entry[geozip.CountryCode], fields[0]; got != want {
t.Errorf("entries[%d]: CountryCode = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.PostalCode], fields[1]; got != want {
t.Errorf("entries[%d]: PostalCode = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.PlaceName], fields[2]; got != want {
t.Errorf("entries[%d]: PlaceName = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminName1], fields[3]; got != want {
t.Errorf("entries[%d]: AdminName1 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminCode1], fields[4]; got != want {
t.Errorf("entries[%d]: AdminCode1 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminName2], fields[5]; got != want {
t.Errorf("entries[%d]: AdminName2 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminCode2], fields[6]; got != want {
t.Errorf("entries[%d]: AdminCode2 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminName3], fields[7]; got != want {
t.Errorf("entries[%d]: AdminName3 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.AdminCode3], fields[8]; got != want {
t.Errorf("entries[%d]: AdminCode3 = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.Latitude], fields[9]; got != want {
t.Errorf("entries[%d]: Latitude = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.Longitude], fields[10]; got != want {
t.Errorf("entries[%d]: Longitude = %v, want %v", idx, got, want)
}
if got, want := entry[geozip.Accuracy], fields[11]; got != want {
t.Errorf("entries[%d]: Accuracy = %v, want %v", idx, got, want)
}
}
}