-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_test.go
159 lines (106 loc) · 3.02 KB
/
database_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
package sqlite
import (
"context"
"fmt"
"io"
"os"
"strconv"
"testing"
_ "github.com/mattn/go-sqlite3"
"github.com/whosonfirst/go-whosonfirst-spatial/database"
"github.com/whosonfirst/go-whosonfirst-spatial/filter"
"github.com/whosonfirst/go-whosonfirst-spatial/geo"
)
func TestSpatialDatabaseQuery(t *testing.T) {
ctx := context.Background()
database_uri := "sqlite://sqlite3?dsn=fixtures/sfomuseum-architecture.db"
expected := int64(1745882085) // This test may fail if sfomuseum-data/sfomuseum-data-architecture is updated and there is a "newer" T2
lat := 37.616951
lon := -122.383747
db, err := database.NewSpatialDatabase(ctx, database_uri)
if err != nil {
t.Fatalf("Failed to create new spatial database, %v", err)
}
defer db.Close(ctx)
c, err := geo.NewCoordinate(lon, lat)
if err != nil {
t.Fatalf("Failed to create new coordinate, %v", err)
}
i, err := filter.NewSPRInputs()
if err != nil {
t.Fatalf("Failed to create SPR inputs, %v", err)
}
i.IsCurrent = []int64{1}
i.Placetypes = []string{"wing"}
f, err := filter.NewSPRFilterFromInputs(i)
if err != nil {
t.Fatalf("Failed to create SPR filter from inputs, %v", err)
}
for i := 0; i < 50; i++ {
spr, err := db.PointInPolygon(ctx, c, f)
if err != nil {
t.Fatalf("Failed to perform point in polygon query, %v", err)
}
results := spr.Results()
count := len(results)
if count != 1 {
t.Fatalf("Expected 1 result but got %d", count)
}
first := results[0]
if first.Id() != strconv.FormatInt(expected, 10) {
t.Fatalf("Expected %d but got %s", expected, first.Id())
}
}
}
func TestSpatialDatabaseRemoveFeature(t *testing.T) {
ctx := context.Background()
database_uri := "sqlite://sqlite3?dsn=:memory:"
db, err := database.NewSpatialDatabase(ctx, database_uri)
if err != nil {
t.Fatalf("Failed to create new spatial database, %v", err)
}
defer db.Close(ctx)
id := 101737491
lat := 46.852675
lon := -71.330873
test_data := fmt.Sprintf("fixtures/%d.geojson", id)
fh, err := os.Open(test_data)
if err != nil {
t.Fatalf("Failed to open %s, %v", test_data, err)
}
defer fh.Close()
body, err := io.ReadAll(fh)
if err != nil {
t.Fatalf("Failed to read %s, %v", test_data, err)
}
err = db.IndexFeature(ctx, body)
if err != nil {
t.Fatalf("Failed to index %s, %v", test_data, err)
}
c, err := geo.NewCoordinate(lon, lat)
if err != nil {
t.Fatalf("Failed to create new coordinate, %v", err)
}
spr, err := db.PointInPolygon(ctx, c)
if err != nil {
t.Fatalf("Failed to perform point in polygon query, %v", err)
}
results := spr.Results()
count := len(results)
if count != 1 {
t.Fatalf("Expected 1 result but got %d", count)
}
err = db.RemoveFeature(ctx, "101737491")
if err != nil {
t.Fatalf("Failed to remove %s, %v", test_data, err)
}
spr, err = db.PointInPolygon(ctx, c)
if err != nil {
t.Fatalf("Failed to perform point in polygon query, %v", err)
}
results = spr.Results()
count = len(results)
if count != 0 {
t.Fatalf("Expected 0 results but got %d", count)
}
}