-
Notifications
You must be signed in to change notification settings - Fork 48
/
changeset_test.go
271 lines (223 loc) · 7.76 KB
/
changeset_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
package osm
import (
"bytes"
"encoding/xml"
"reflect"
"testing"
"time"
)
func TestChangesets(t *testing.T) {
data := []byte(`
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="replicate_changesets.rb" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<changeset id="36947117" created_at="2016-02-01T21:57:17Z" closed_at="2016-02-01T23:05:55Z" open="true" num_changes="86" user="padvinder" uid="978786" min_lat="52.7016394" max_lat="52.7236643" min_lon="5.1545597" max_lon="5.2532961" comments_count="5">
<tag k="build" v="2.4-16-g0c126d0"/>
<tag k="created_by" v="Potlatch 2"/>
<tag k="version" v="2.4"/>
</changeset>
<changeset id="36947173" created_at="2016-02-01T22:00:56Z" closed_at="2016-02-01T23:05:06Z" open="false" num_changes="9" user="florijn11" uid="1319603" min_lat="51.5871887" max_lat="51.6032569" min_lon="5.3214071" max_lon="5.33106" comments_count="0">
<tag k="version" v="2.4"/>
<tag k="build" v="2.4-16-g0c126d0"/>
<tag k="comment" v="Fietsdoorsteek aangepast"/>
<tag k="created_by" v="Potlatch 2"/>
</changeset>
</osm>`)
cs := &OSM{}
err := xml.Unmarshal(data, &cs)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if l := len(cs.Changesets); l != 2 {
t.Fatalf("incorrect number of changesets, got %v", l)
}
c := cs.Changesets[0]
if v := c.ID; v != 36947117 {
t.Errorf("incorrect id, got %v", v)
}
if v := c.CreatedAt; v != time.Date(2016, time.February, 1, 21, 57, 17, 0, time.UTC) {
t.Errorf("incorrect created at, got %v", v)
}
if v := c.ClosedAt; v != time.Date(2016, time.February, 1, 23, 05, 55, 0, time.UTC) {
t.Errorf("incorrect closed at, got %v", v)
}
if v := c.ChangesCount; v != 86 {
t.Errorf("incorrect changes count, got %v", v)
}
if v := c.User; v != "padvinder" {
t.Errorf("incorrect user, got %v", v)
}
if v := c.UserID; v != 978786 {
t.Errorf("incorrect user id, got %v", v)
}
if v := c.MinLat; v != 52.7016394 {
t.Errorf("incorrect min lat, got %v", v)
}
if v := c.MaxLat; v != 52.7236643 {
t.Errorf("incorrect max lat, got %v", v)
}
if v := c.MinLon; v != 5.1545597 {
t.Errorf("incorrect min lon, got %v", v)
}
if v := c.MaxLon; v != 5.2532961 {
t.Errorf("incorrect max on, got %v", v)
}
if v := c.CommentsCount; v != 5 {
t.Errorf("incorrect comment count, got %v", v)
}
}
func TestChangeset_open(t *testing.T) {
data := []byte(`
<changeset id="40309372" user="Bahntech" uid="3619264" created_at="2016-06-26T21:26:41Z" open="true" min_lat="51.484563" min_lon="12.0995042" max_lat="51.484563" max_lon="12.0995042" comments_count="0">
<tag k="comment" v="updated fire hydrant details with OsmHydrant"/>
<tag k="created_by" v="OsmHydrant / http://yapafo.net v0.3"/>
</changeset>`)
var c Changeset
err := xml.Unmarshal(data, &c)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if !c.ClosedAt.IsZero() {
t.Errorf("closed at should be zero for open changesets")
}
}
func TestChangeset_tags(t *testing.T) {
data := []byte(`
<changeset id="123123">
<tag k="comment" v="changeset comment"/>
<tag k="created_by" v="iD 1.8.3"/>
<tag k="locale" v="en-US"/>
<tag k="host" v="http://id.org"/>
<tag k="imagery_used" v="Bing"/>
<tag k="source" v="some data"/>
<tag k="bot" v="yes"/>
</changeset>`)
var c Changeset
err := xml.Unmarshal(data, &c)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if v := c.Comment(); v != "changeset comment" {
t.Errorf("incorrect comment, got %v", v)
}
if v := c.CreatedBy(); v != "iD 1.8.3" {
t.Errorf("incorrect created by, got %v", v)
}
if v := c.Locale(); v != "en-US" {
t.Errorf("incorrect locale, got %v", v)
}
if v := c.Host(); v != "http://id.org" {
t.Errorf("incorrect host, got %v", v)
}
if v := c.ImageryUsed(); v != "Bing" {
t.Errorf("incorrect imagery used, got %v", v)
}
if v := c.Source(); v != "some data" {
t.Errorf("incorrect source, got %v", v)
}
if v := c.Bot(); !v {
t.Errorf("incorrect bot, got %v", v)
}
}
func TestChangeset_bounds(t *testing.T) {
data := []byte(`
<changeset id="36947173" created_at="2016-02-01T22:00:56Z" closed_at="2016-02-01T23:05:06Z" open="false" num_changes="9" user="florijn11" uid="1319603" min_lat="51.5871887" max_lat="51.6032569" min_lon="5.3214071" max_lon="5.33106" comments_count="0">
<tag k="version" v="2.4"/>
<tag k="build" v="2.4-16-g0c126d0"/>
<tag k="comment" v="Fietsdoorsteek aangepast"/>
<tag k="created_by" v="Potlatch 2"/>
</changeset>`)
var c Changeset
err := xml.Unmarshal(data, &c)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if v := c.MinLat; v != 51.5871887 {
t.Errorf("incorrect value, got %v", v)
}
if v := c.MaxLat; v != 51.6032569 {
t.Errorf("incorrect value, got %v", v)
}
if v := c.MinLon; v != 5.3214071 {
t.Errorf("incorrect value, got %v", v)
}
if v := c.MaxLon; v != 5.33106 {
t.Errorf("incorrect value, got %v", v)
}
}
func TestChangeset_comments(t *testing.T) {
data := []byte(`
<changeset id="40303151" user="Glen Bundrick" uid="4173877" created_at="2016-06-26T15:37:47Z" closed_at="2016-06-26T15:37:48Z" open="false" min_lat="34.6591676" min_lon="-81.8789825" max_lat="34.6594167" max_lon="-81.8788142" comments_count="3">
<tag k="comment" v="Recent Doublewide addition"/>
<tag k="locale" v="en-US"/>
<tag k="host" v="https://www.openstreetmap.org/id"/>
<tag k="imagery_used" v="Bing"/>
<tag k="created_by" v="iD 1.9.6"/>
<discussion>
<comment date="2016-06-26T17:22:27Z" uid="5359" user="user_5359">
<text>Welcome to OSM!</text>
</comment>
<comment date="2016-06-26T20:56:11Z" uid="4173877" user="Glen Bundrick">
<text>OK New to this and learning
</text>
</comment>
<comment date="2016-06-26T21:37:40Z" uid="5359" user="user_5359">
<text>No problem. If you need help, you are welcome!</text>
</comment>
</discussion>
</changeset>`)
var c Changeset
err := xml.Unmarshal(data, &c)
if err != nil {
t.Fatalf("unmarshal error: %v", err)
}
if l := len(c.Discussion.Comments); l != 3 {
t.Errorf("incorrect number of comments, got %v", l)
}
com := c.Discussion.Comments[0]
if v := com.Timestamp; v != time.Date(2016, 6, 26, 17, 22, 27, 0, time.UTC) {
t.Errorf("incorrect created at, got %v", v)
}
if v := com.User; v != "user_5359" {
t.Errorf("incorrect user, got %v", v)
}
if v := com.UserID; v != 5359 {
t.Errorf("incorrect username, got %v", v)
}
if v := com.Text; v != "Welcome to OSM!" {
t.Errorf("incorrect text, got %v", v)
}
}
func TestChangeset_MarshalXML(t *testing.T) {
cs := Changeset{
ID: 123,
}
data, err := xml.Marshal(cs)
if err != nil {
t.Fatalf("xml marshal error: %v", err)
}
expected := `<changeset id="123" user="" uid="0" created_at="0001-01-01T00:00:00Z" closed_at="0001-01-01T00:00:00Z" open="false" min_lat="0" max_lat="0" min_lon="0" max_lon="0"></changeset>`
if !bytes.Equal(data, []byte(expected)) {
t.Errorf("incorrect marshal, got: %s", string(data))
}
// changeset with discussion
cs.Discussion = &ChangesetDiscussion{
Comments: []*ChangesetComment{
{Text: "foo"},
},
}
data, err = xml.Marshal(cs)
if err != nil {
t.Fatalf("xml marshal error: %v", err)
}
expected = `<changeset id="123" user="" uid="0" created_at="0001-01-01T00:00:00Z" closed_at="0001-01-01T00:00:00Z" open="false" min_lat="0" max_lat="0" min_lon="0" max_lon="0"><discussion><comment user="" uid="0" date="0001-01-01T00:00:00Z"><text>foo</text></comment></discussion></changeset>`
if !bytes.Equal(data, []byte(expected)) {
t.Errorf("incorrect marshal, got: %s", string(data))
}
}
func TestChangesets_IDs(t *testing.T) {
cs := Changesets{{ID: 1}, {ID: 2}}
csids := []ChangesetID{1, 2}
if ids := cs.IDs(); !reflect.DeepEqual(ids, csids) {
t.Errorf("incorrect changeset id: %v", csids)
}
}