-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeocoder_test.go
99 lines (89 loc) · 2.32 KB
/
geocoder_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/pmezard/apec/jstruct"
"github.com/pquerna/ffjson/ffjson"
)
func addCacheEntry(t *testing.T, cache *Cache, key, path string) {
path = filepath.Join("testdata", path)
data, err := ioutil.ReadFile(path)
if err != nil {
t.Fatalf("could not read test file %s: %s", path, err)
}
loc := &jstruct.Location{}
err = ffjson.Unmarshal(data, loc)
if err != nil {
t.Fatalf("could not parse json data from %s: %s", path, err)
}
p := buildLocation(loc)
err = cache.Put(key, data, p)
if err != nil {
t.Fatalf("could not cache from %s: %s", path, err)
}
}
func checkCacheLocation(t *testing.T, cache *Cache, key string,
there bool, expected *Location) {
loc, ok, err := cache.GetLocation(key)
if err != nil {
t.Fatalf("cannot retrieve location for %s: %s", key, err)
}
if !there {
if ok {
t.Fatalf("%s is not expected in the cache", key)
}
return
}
if expected == nil {
if loc != nil {
t.Fatalf("cached locations points differ: %+v\n!=\n%+v", expected, loc)
}
return
}
if !reflect.DeepEqual(*expected, *loc) {
t.Fatalf("cached locations differ: %+v\n!=\n%+v", *expected, *loc)
}
}
func TestGeocoderCacheLocation(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "apec-")
if err != nil {
t.Fatalf("could not create geocoder cache directory: %s", err)
}
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "geocoder")
cache, err := OpenCache(path)
if err != nil {
t.Fatalf("could not create cache: %s", err)
}
defer cache.Close()
// Add entry with results
addCacheEntry(t, cache, "results", "geo_results.json")
addCacheEntry(t, cache, "noresult", "geo_noresult.json")
// Check what was stored
checkCacheLocation(t, cache, "results", true, &Location{
City: "Paris",
County: "Paris",
State: "Ile-de-France",
Country: "France",
Lat: 48.8565056,
Lon: 2.3521334,
})
checkCacheLocation(t, cache, "noresult", true, nil)
checkCacheLocation(t, cache, "missing", false, nil)
}
func TestGeocoderNew(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "apec-")
if err != nil {
t.Fatalf("could not create geocoder cache directory: %s", err)
}
defer os.RemoveAll(tmpDir)
path := filepath.Join(tmpDir, "geocoder")
g, err := NewGeocoder("some_key", path)
if err != nil {
t.Fatal(err)
}
g.Close()
}