forked from junghao/geonet-rest
/
quakeV1_test.go
300 lines (253 loc) · 7.72 KB
/
quakeV1_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//# Quake Information
//
//##/quake
//
// Look up quake information. All calls return [GeoJSON](http://geojson.org/) with Point features.
//
//### Quake Properties
//
// Each quake in the returned geojson has properties associated with it.
// Please follow this link for information about how the [quake properties](http://info.geonet.org.nz/x/J4IW) are derived.
//
// * `publicID` - the unique public identifier for this quake.
// * `time` - the origin time of the quake.
// * `depth` - the depth of the quake in km.
// * `magnitude` - the summary magnitude for the quake. This is *not* Richter magnitude.
// * `type` - the event type; earthquake, landslide etc.
// * `agency` - the agency that located this quake. The official GNS/GeoNet agency name for this field is WEL(*).
// * `locality` - distance and direction to the nearest locality.
// * `intensity` - the calculated [intensity](http://info.geonet.org.nz/x/b4Ih) at the surface above the quake (epicenter) e.g., `strong`.
// * `regionIntensity` - the calculated intensity at the closest locality in the region for the request. If no region is specified for the query then this is the intensity in the `newzealand` region.
// * `quality` - the quality of this information; `best`, `good`, `caution`, `unknown`, `deleted`.
// * `modificationTime` - the modification time of this information.
package main
import (
"encoding/json"
"github.com/GeoNet/web"
"github.com/GeoNet/web/webtest"
"log"
"testing"
)
type QuakeFeatures struct {
Features []QuakeFeature
}
type QuakeFeature struct {
Properties QuakeProperties
Geometry QuakeGeometry
}
type QuakeGeometry struct {
Type string
Coordinates []float64
}
type QuakeProperties struct {
Publicid, Time, Modificationtime, Type, Agency string
Locality, Intensity, Regionintensity, Quality string
Depth, Magnitude float64
}
//## Single Quake
//
// **GET /quake/(publicID)**
//
// Get information for a single quake.
//
//### Parameters
//
// * `publicID` - a valid quake ID e.g., `2014p715167`.
//
//### Example request:
//
// `/quake/2013p407387`
//
func TestQuakeV1(t *testing.T) {
setup()
defer teardown()
c := webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake/2013p407387",
}
b, err := c.Get(ts)
if err != nil {
t.Fatal(err)
}
var f QuakeFeatures
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
if f.Features[0].Geometry.Type != "Point" {
t.Error("wrong type")
}
if f.Features[0].Geometry.Coordinates[0] != 172.28223 {
t.Error("wrong longitude")
}
if f.Features[0].Geometry.Coordinates[1] != -43.397461 {
t.Error("wrong latitude")
}
if f.Features[0].Properties.Publicid != "2013p407387" {
t.Error("incorrect publicid")
}
if f.Features[0].Properties.Time != "2013-05-30T15:15:37.812Z" {
t.Error("incorrect time")
}
if f.Features[0].Properties.Modificationtime != "2013-06-13T23:47:04.344Z" {
t.Error("incorrect updatetime")
}
if f.Features[0].Properties.Type != "earthquake" {
t.Error("incorrect type")
}
if f.Features[0].Properties.Quality != "good" {
t.Error("incorrect quality")
}
if f.Features[0].Properties.Intensity != "moderate" {
t.Error("incorrect intensity")
}
if f.Features[0].Properties.Regionintensity != "moderate" {
t.Error("incorrect region intensity")
}
if f.Features[0].Properties.Agency != "WEL(Avalon)" {
t.Error("incorrect agency")
}
if f.Features[0].Properties.Locality != "15 km south-east of Oxford" {
t.Error("incorrect locality")
}
if f.Features[0].Properties.Depth != 20.141276 {
t.Error("incorrect depth")
}
if f.Features[0].Properties.Magnitude != 4.0252561 {
t.Error("incorrect magnitude")
}
}
//## Quakes Possibly Felt in a Region
//
// **GET /quake?regionID=(region)®ionIntensity=(intensity)&number=(n)&quality=(quality)**
//
// Get quake information from the last 365 days.
// If no quakes are found for the query parameters then a null features array is returned.
//
//### Parameters
//
// * `regionID` - a valid quake region identifier e.g., `newzealand`.
// * `regionIntensity` - the minimum intensity in the region e.g., `weak`. Must be one of `unnoticeable`, `weak`, `light`, `moderate`, `strong`, `severe`.
// * `number` - the maximum number of quakes to return. Must be one of `3`, `30`, `100`, `500`, `1000`, `1500`.
// * `quality` - a comma separated list of quality values to be included in the response; `best`, `caution`, `deleted`, `good`.
//
// *The `number` of quakes that can be returned is restricted to a range of options to improve caching.*
//
//### Example request:
//
// `/quake?regionID=newzealand®ionIntensity=weak&number=30`
//
func TestQuakesRegionV1(t *testing.T) {
setup()
defer teardown()
c := webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake?regionID=newzealand®ionIntensity=severe&number=30&quality=best,caution,good",
}
b, err := c.Get(ts)
if err != nil {
t.Fatal(err)
}
var f QuakeFeatures
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
if len(f.Features) != 2 {
t.Errorf("Found wrong number of features: %d", len(f.Features))
}
// Check that deleted quakes are included in the response.
// This is a change from the existing GeoNet services.
c = webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake?regionID=newzealand®ionIntensity=unnoticeable&number=1000&quality=best,caution,good,deleted",
}
b, err = c.Get(ts)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
var count = 0
for _, q := range f.Features {
if q.Properties.Quality == "deleted" {
count++
}
}
if count == 0 {
t.Error("found no deleted quakes in the JSON.")
}
}
//## Quakes in a Region
//
// **GET /quake?regionID=(region)&intensity=(intensity)&number=(n)&quality=(quality)**
//
// Get quake information from the last 365 days.
// If no quakes are found for the query parameters then a null features array is returned.
//
//### Parameters
//
// * `regionID` - a valid quake region identifier e.g., `newzealand`.
// * `intensity` - the minimum intensity at the epicenter e.g., `weak`. Must be one of `unnoticeable`, `weak`, `light`, `moderate`, `strong`, `severe`.
// * `number` - the maximum number of quakes to return. Must be one of `30`, `100`, `500`, `1000`, `1500`.
// * `quality` - a comma separated list of quality values to be included in the response; `best`, `caution`, `deleted`, `good`.
//
// *The `number` of quakes that can be returned is restricted to a range of options to improve caching.*
//
//### Example request:
//
// `/quake?regionID=newzealand&intensity=weak&number=30`
//
func TestQuakesV1(t *testing.T) {
setup()
defer teardown()
c := webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake?regionID=wellington&intensity=weak&number=30&quality=best,caution,good",
}
b, err := c.Get(ts)
if err != nil {
t.Fatal(err)
}
var f QuakeFeatures
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
if len(f.Features) != 0 {
t.Errorf("Found wrong number of features: %d", len(f.Features))
}
c = webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake?regionID=wellington®ionIntensity=weak&number=30&quality=best,caution,good",
}
b, err = c.Get(ts)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
if len(f.Features) != 2 {
t.Errorf("Found wrong number of features: %d", len(f.Features))
}
// There should be 7 quakes weak and above in the Canterbury region.
c = webtest.Content{
Accept: web.V1GeoJSON,
URI: "/quake?regionID=canterbury&intensity=weak&number=30&quality=best,caution,good",
}
b, err = c.Get(ts)
if err != nil {
t.Fatal(err)
}
err = json.Unmarshal(b, &f)
if err != nil {
log.Fatal(err)
}
if len(f.Features) != 7 {
t.Errorf("Found wrong number of features: %d", len(f.Features))
}
}