-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
geo_test.go
70 lines (63 loc) · 2.03 KB
/
geo_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
/*
* Copyright 2016-2023 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package types
import (
"reflect"
"testing"
)
func TestParse(t *testing.T) {
array := []string{
`{'type':'Point','coordinates':[1,2]}`,
`{'type':'MultiLineString','coordinates':[[[1,2,3],[4,5,6],[7,8,9],[1,2,3]]]}`,
}
for _, v := range array {
src := Val{StringID, []byte(v)}
if g, err := Convert(src, GeoID); err != nil {
t.Errorf("Error parsing %s: %v", v, err)
} else {
// Marshal it back to text
got := ValueForType(StringID)
if err := Marshal(g, &got); err != nil || got.Value.(string) != v {
t.Errorf("Marshal error expected %s, got %s. error %v", v, got.Value.(string), err)
}
wkb := ValueForType(BinaryID)
// Marshal and unmarshal to WKB
if err := Marshal(g, &wkb); err != nil {
t.Errorf("Error marshaling to WKB: %v", err)
}
src := Val{GeoID, wkb.Value.([]byte)}
if bg, err := Convert(src, GeoID); err != nil {
t.Errorf("Error unmarshaling WKB: %v", err)
} else if !reflect.DeepEqual(g, bg) {
t.Errorf("Expected %#v, got %#v", g, bg)
}
}
}
}
func TestParseGeoJsonErrors(t *testing.T) {
array := []string{
`{"type":"Curve","coordinates":[1,2]}`,
`{"type":"Feature","geometry":{"type":"Point","coordinates":[125.6,10.1]},"properties":{"name":"Dinagat Islands"}}`,
`{}`,
`thisisntjson`,
}
for _, v := range array {
src := Val{StringID, []byte(v)}
if _, err := Convert(src, GeoID); err == nil {
t.Errorf("Expected error parsing %s: %v", v, err)
}
}
}