forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_test.go
183 lines (148 loc) · 3.76 KB
/
node_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
package osm
import (
"bytes"
"encoding/json"
"encoding/xml"
"reflect"
"testing"
"time"
)
func TestNode(t *testing.T) {
data := []byte(`<node id="123" changeset="456" timestamp="2014-04-10T00:43:05Z" version="1" visible="true" user="user" uid="1357" lat="50.7107023" lon="6.0043943"/>`)
n := Node{}
err := xml.Unmarshal(data, &n)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if v := n.ID; v != 123 {
t.Errorf("incorrect id, got %v", v)
}
if v := n.ChangesetID; v != 456 {
t.Errorf("incorrect changeset, got %v", v)
}
if v := n.Timestamp; v != time.Date(2014, 4, 10, 0, 43, 05, 0, time.UTC) {
t.Errorf("incorrect timestamp, got %v", v)
}
if v := n.Version; v != 1 {
t.Errorf("incorrect version, got %v", v)
}
if v := n.Visible; !v {
t.Errorf("incorrect visible, got %v", v)
}
if v := n.User; v != "user" {
t.Errorf("incorrect user, got %v", v)
}
if v := n.UserID; v != 1357 {
t.Errorf("incorrect user id, got %v", v)
}
if v := n.Lat; v != 50.7107023 {
t.Errorf("incorrect lat, got %v", v)
}
if v := n.Lon; v != 6.0043943 {
t.Errorf("incorrect lon, got %v", v)
}
}
func TestNode_MarshalJSON(t *testing.T) {
n := Node{
ID: 123,
}
data, err := json.Marshal(n)
if err != nil {
t.Fatalf("marshal error: %v", err)
}
if !bytes.Equal(data, []byte(`{"type":"node","id":123,"lat":0,"lon":0,"visible":false,"timestamp":"0001-01-01T00:00:00Z"}`)) {
t.Errorf("incorrect json: %v", string(data))
}
}
func TestNode_MarshalXML(t *testing.T) {
n := Node{
ID: 123,
}
data, err := xml.Marshal(n)
if err != nil {
t.Fatalf("xml marshal error: %v", err)
}
expected := `<node id="123" lat="0" lon="0" user="" uid="0" visible="false" version="0" changeset="0" timestamp="0001-01-01T00:00:00Z"></node>`
if !bytes.Equal(data, []byte(expected)) {
t.Errorf("incorrect marshal, got: %s", string(data))
}
}
func TestUnmarshalNodes(t *testing.T) {
ns := Nodes{
{ID: 123},
{ID: 321},
}
data, err := ns.Marshal()
if err != nil {
t.Fatalf("nodes marshal error: %v", err)
}
ns2, err := UnmarshalNodes(data)
if err != nil {
t.Fatalf("nodes unmarshal error: %v", err)
}
if !reflect.DeepEqual(ns, ns2) {
t.Errorf("nodes not equal")
t.Logf("%+v", ns)
t.Logf("%+v", ns2)
}
// empty nodes
ns = Nodes{}
data, err = ns.Marshal()
if err != nil {
t.Fatalf("nodes marshal error: %v", err)
}
if l := len(data); l != 0 {
t.Errorf("length of node data should be 0, got %v", l)
}
ns2, err = UnmarshalNodes(data)
if err != nil {
t.Fatalf("nodes unmarshal error: %v", err)
}
if ns2 != nil {
t.Errorf("should return nil Nodes for empty data, got %v", ns2)
}
}
func TestNodes_ids(t *testing.T) {
ns := Nodes{
{ID: 1, Version: 3},
{ID: 2, Version: 4},
}
eids := ElementIDs{NodeID(1).ElementID(3), NodeID(2).ElementID(4)}
if ids := ns.ElementIDs(); !reflect.DeepEqual(ids, eids) {
t.Errorf("incorrect element ids: %v", ids)
}
fids := FeatureIDs{NodeID(1).FeatureID(), NodeID(2).FeatureID()}
if ids := ns.FeatureIDs(); !reflect.DeepEqual(ids, fids) {
t.Errorf("incorrect feature ids: %v", ids)
}
nids := []NodeID{1, 2}
if ids := ns.IDs(); !reflect.DeepEqual(ids, nids) {
t.Errorf("incorrect node ids: %v", nids)
}
}
func TestNodes_SortByIDVersion(t *testing.T) {
ns := Nodes{
{ID: 7, Version: 3},
{ID: 2, Version: 4},
{ID: 5, Version: 2},
{ID: 5, Version: 3},
{ID: 5, Version: 4},
{ID: 3, Version: 4},
{ID: 4, Version: 4},
{ID: 9, Version: 4},
}
ns.SortByIDVersion()
eids := ElementIDs{
NodeID(2).ElementID(4),
NodeID(3).ElementID(4),
NodeID(4).ElementID(4),
NodeID(5).ElementID(2),
NodeID(5).ElementID(3),
NodeID(5).ElementID(4),
NodeID(7).ElementID(3),
NodeID(9).ElementID(4),
}
if ids := ns.ElementIDs(); !reflect.DeepEqual(ids, eids) {
t.Errorf("incorrect sort: %v", eids)
}
}