Skip to content

Commit

Permalink
add tests to subpackages
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Mar 7, 2018
1 parent 1e91448 commit fd9e4a0
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
25 changes: 25 additions & 0 deletions annotate/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package annotate

import (
"errors"
"testing"

"github.com/paulmach/osm/annotate/internal/core"
)

func TestMapErrors(t *testing.T) {
e := mapErrors(&core.NoHistoryError{})
if _, ok := e.(*NoHistoryError); !ok {
t.Errorf("should map NoHistoryError: %+v", e)
}

e = mapErrors(&core.NoVisibleChildError{})
if _, ok := e.(*NoVisibleChildError); !ok {
t.Errorf("should map NoVisibleChildError: %+v", e)
}

err := errors.New("some error")
if e := mapErrors(err); e != err {
t.Errorf("should pass through other errors: %v", e)
}
}
2 changes: 1 addition & 1 deletion annotate/internal/core/compute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestCompute(t *testing.T) {
},
}

updates, err := Compute(ctx, parents, ds, &Options{Threshold: time.Minute})
updates, err := Compute(ctx, parents, ds, nil)
if err != nil {
t.Fatalf("compute error: %v", err)
}
Expand Down
54 changes: 53 additions & 1 deletion osmgeojson/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func TestConvert(t *testing.T) {
t.Run("relation", func(t *testing.T) {
xml := `
<osm>
<relation id='1'>
<relation id='1' version='1' timestamp='2018-01-01T00:00:00Z'
changeset='123' user='user' uid='431'>
<tag k='type' v='multipolygon' />
<tag k='amenity' v='hospital' />
<member type='way' ref='2' role='outer' />
Expand Down Expand Up @@ -132,6 +133,13 @@ func TestConvert(t *testing.T) {
"amenity": "hospital",
"type": "multipolygon",
}
feature.Properties["meta"] = map[string]interface{}{
"changeset": osm.ChangesetID(123),
"timestamp": "2018-01-01T00:00:00Z",
"uid": osm.UserID(431),
"user": "user",
"version": 1,
}

fc := geojson.NewFeatureCollection().Append(feature)
testConvert(t, xml, fc)
Expand Down Expand Up @@ -517,6 +525,50 @@ func TestConvert_useAugmentedNodeValues(t *testing.T) {
testConvert(t, xml, fc)
}

func TestBuildRouteLineString(t *testing.T) {
ctx := &context{
osm: &osm.OSM{},
skippable: map[osm.WayID]struct{}{},
wayMap: map[osm.WayID]*osm.Way{
2: &osm.Way{
ID: 2,
Nodes: osm.WayNodes{
{ID: 1, Lat: 1, Lon: 2},
{ID: 2},
{ID: 3, Lat: 3, Lon: 4},
},
},
},
}

relation := &osm.Relation{
ID: 1,
Members: osm.Members{
{Type: osm.TypeNode, Ref: 1},
{Type: osm.TypeWay, Ref: 2},
{Type: osm.TypeWay, Ref: 3},
},
}

feature := ctx.buildRouteLineString(relation)
if !orb.Equal(feature.Geometry, orb.LineString{{2, 1}, {4, 3}}) {
t.Errorf("incorrect geometry: %v", feature.Geometry)
}

relation = &osm.Relation{
ID: 1,
Members: osm.Members{
{Type: osm.TypeWay, Ref: 20},
{Type: osm.TypeWay, Ref: 30},
},
}

feature = ctx.buildRouteLineString(relation)
if feature != nil {
t.Errorf("should not return feature if no ways present: %v", feature)
}
}

func testConvert(t *testing.T, rawXML string, expected *geojson.FeatureCollection, opts ...Option) {
t.Helper()

Expand Down
64 changes: 64 additions & 0 deletions osmtest/scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package osmtest

import (
"errors"
"reflect"
"testing"

"github.com/paulmach/osm"
)

func TestScanner(t *testing.T) {
objs := osm.Objects{
&osm.Node{ID: 1, Version: 4},
&osm.Way{ID: 2, Version: 5},
&osm.Relation{ID: 3, Version: 6},
}

scanner := NewScanner(objs)
defer scanner.Close()

expected := osm.ObjectIDs{
osm.NodeID(1).ObjectID(4),
osm.WayID(2).ObjectID(5),
osm.RelationID(3).ObjectID(6),
}

ids := osm.ObjectIDs{}
for scanner.Scan() {
ids = append(ids, scanner.Object().ObjectID())
}

if !reflect.DeepEqual(ids, expected) {
t.Errorf("incorrect ids: %v", ids)
}
}

func TestScanner_error(t *testing.T) {
objs := osm.Objects{
&osm.Node{ID: 1, Version: 4},
&osm.Way{ID: 2, Version: 5},
&osm.Relation{ID: 3, Version: 6},
}

scanner := NewScanner(objs)
defer scanner.Close()

if scanner.Err() != nil {
t.Errorf("error should not be set initially")
}

if !scanner.Scan() {
t.Errorf("should be true initially")
}

scanner.ScanError = errors.New("some error")

if scanner.Scan() {
t.Errorf("should be false after error")
}

if scanner.Err() == nil {
t.Errorf("should return error if there is one")
}
}

0 comments on commit fd9e4a0

Please sign in to comment.