Skip to content

Commit

Permalink
more test coverage for core package
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Mar 8, 2018
1 parent fd9e4a0 commit a7dae28
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 12 deletions.
8 changes: 8 additions & 0 deletions bounds_test.go
Expand Up @@ -8,6 +8,14 @@ import (
)

func TestNewBoundFromTile(t *testing.T) {
if _, err := NewBoundsFromTile(maptile.New(1000, 1, 3)); err == nil {
t.Errorf("should return error for x out of bound")
}

if _, err := NewBoundsFromTile(maptile.New(1, 1000, 3)); err == nil {
t.Errorf("should return error for y out of bound")
}

bounds, _ := NewBoundsFromTile(maptile.New(7, 8, 9))

// check 9 tiles around bounds
Expand Down
19 changes: 19 additions & 0 deletions change_test.go
Expand Up @@ -8,6 +8,25 @@ import (
"testing"
)

func TestChange_append(t *testing.T) {
c := &Change{}

c.AppendCreate(&Node{ID: 1})
if c.Create.Nodes[0].ID != 1 {
t.Errorf("append create not working")
}

c.AppendModify(&Node{ID: 2})
if c.Modify.Nodes[0].ID != 2 {
t.Errorf("append modify not working")
}

c.AppendDelete(&Node{ID: 3})
if c.Delete.Nodes[0].ID != 3 {
t.Errorf("append delete not working")
}
}

func TestChange(t *testing.T) {
data := []byte(`
<osmChange version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
Expand Down
9 changes: 9 additions & 0 deletions changeset_test.go
Expand Up @@ -310,3 +310,12 @@ func TestChangeset_MarshalXML(t *testing.T) {
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)
}
}
8 changes: 4 additions & 4 deletions datasource.go
Expand Up @@ -70,7 +70,7 @@ func (ds *HistoryDatasource) add(o *OSM, visible ...bool) {
}

// NodeHistory returns the history for the given id from the map.
func (ds HistoryDatasource) NodeHistory(ctx context.Context, id NodeID) (Nodes, error) {
func (ds *HistoryDatasource) NodeHistory(ctx context.Context, id NodeID) (Nodes, error) {
if ds.Nodes == nil {
return nil, errNotFound
}
Expand All @@ -84,7 +84,7 @@ func (ds HistoryDatasource) NodeHistory(ctx context.Context, id NodeID) (Nodes,
}

// WayHistory returns the history for the given id from the map.
func (ds HistoryDatasource) WayHistory(ctx context.Context, id WayID) (Ways, error) {
func (ds *HistoryDatasource) WayHistory(ctx context.Context, id WayID) (Ways, error) {
if ds.Ways == nil {
return nil, errNotFound
}
Expand All @@ -98,7 +98,7 @@ func (ds HistoryDatasource) WayHistory(ctx context.Context, id WayID) (Ways, err
}

// RelationHistory returns the history for the given id from the map.
func (ds HistoryDatasource) RelationHistory(ctx context.Context, id RelationID) (Relations, error) {
func (ds *HistoryDatasource) RelationHistory(ctx context.Context, id RelationID) (Relations, error) {
if ds.Relations == nil {
return nil, errNotFound
}
Expand All @@ -112,6 +112,6 @@ func (ds HistoryDatasource) RelationHistory(ctx context.Context, id RelationID)
}

// NotFound returns true if the error returned is a not found error.
func (ds HistoryDatasource) NotFound(err error) bool {
func (ds *HistoryDatasource) NotFound(err error) bool {
return err == errNotFound
}
92 changes: 92 additions & 0 deletions datasource_test.go
@@ -0,0 +1,92 @@
package osm

import (
"context"
"testing"
)

func TestHistoryDatasource(t *testing.T) {
ctx := context.Background()

t.Run("empty datasource", func(t *testing.T) {
ds := &HistoryDatasource{}

if _, err := ds.NodeHistory(ctx, 1); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}

if _, err := ds.WayHistory(ctx, 1); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}

if _, err := ds.RelationHistory(ctx, 1); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}
})

o := &OSM{
Nodes: Nodes{
{ID: 1, Version: 1},
{ID: 1, Version: 2},
},
Ways: Ways{
{ID: 1, Version: 1},
{ID: 1, Version: 2},
{ID: 1, Version: 3},
},
Relations: Relations{
{ID: 1, Version: 1},
{ID: 1, Version: 2},
{ID: 1, Version: 3},
{ID: 1, Version: 4},
},
}

t.Run("non-empty datasource", func(t *testing.T) {
ds := o.ToHistoryDatasource()

ns, err := ds.NodeHistory(ctx, 1)
if err != nil {
t.Errorf("should not return error: %v", err)
}

if len(ns) != 2 {
t.Errorf("incorrect nodes: %v", ns)
}

ws, err := ds.WayHistory(ctx, 1)
if err != nil {
t.Errorf("should not return error: %v", err)
}

if len(ws) != 3 {
t.Errorf("incorrect ways: %v", ns)
}

rs, err := ds.RelationHistory(ctx, 1)
if err != nil {
t.Errorf("should not return error: %v", err)
}

if len(rs) != 4 {
t.Errorf("incorrect relations: %v", ns)
}
})

t.Run("not found non-empty datasource", func(t *testing.T) {
ds := o.ToHistoryDatasource()

if _, err := ds.NodeHistory(ctx, 2); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}

if _, err := ds.WayHistory(ctx, 2); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}

if _, err := ds.RelationHistory(ctx, 2); !ds.NotFound(err) {
t.Errorf("should be not found error: %v", err)
}
})

}
52 changes: 52 additions & 0 deletions node_test.go
Expand Up @@ -129,3 +129,55 @@ func TestUnmarshalNodes(t *testing.T) {
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)
}
}
129 changes: 129 additions & 0 deletions relation_test.go
Expand Up @@ -11,6 +11,18 @@ import (
"github.com/paulmach/orb"
)

func TestRelation_ids(t *testing.T) {
r := Relation{ID: 12, Version: 2}

if id := r.FeatureID(); id != RelationID(12).FeatureID() {
t.Errorf("incorrect feature id: %v", id)
}

if id := r.ElementID(); id != RelationID(12).ElementID(2) {
t.Errorf("incorrect element id: %v", id)
}
}

func TestRelation_MarshalJSON(t *testing.T) {
r := Relation{
ID: 123,
Expand Down Expand Up @@ -242,3 +254,120 @@ func TestRelations_Marshal(t *testing.T) {
t.Errorf("should return nil Relations for empty data, got %v", rs2)
}
}

func TestMember_ids(t *testing.T) {
cases := []struct {
name string
m Member
fid FeatureID
eid ElementID
}{
{
name: "node",
m: Member{Type: TypeNode, Ref: 12, Version: 2},
fid: NodeID(12).FeatureID(),
eid: NodeID(12).ElementID(2),
},
{
name: "way",
m: Member{Type: TypeWay, Ref: 12, Version: 2},
fid: WayID(12).FeatureID(),
eid: WayID(12).ElementID(2),
},
{
name: "relation",
m: Member{Type: TypeRelation, Ref: 12, Version: 2},
fid: RelationID(12).FeatureID(),
eid: RelationID(12).ElementID(2),
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
if id := tc.m.FeatureID(); id != tc.fid {
t.Errorf("incorrect feature id: %v", id)
}

if id := tc.m.ElementID(); id != tc.eid {
t.Errorf("incorrect element id: %v", id)
}
})
}
}
func TestMembers_ids(t *testing.T) {
ms := Members{
{Type: TypeNode, Ref: 1, Version: 3},
{Type: TypeWay, Ref: 2, Version: 4},
{Type: TypeRelation, Ref: 3, Version: 5},
}

eids := ElementIDs{
NodeID(1).ElementID(3),
WayID(2).ElementID(4),
RelationID(3).ElementID(5),
}
if ids := ms.ElementIDs(); !reflect.DeepEqual(ids, eids) {
t.Errorf("incorrect element ids: %v", ids)
}

fids := FeatureIDs{
NodeID(1).FeatureID(),
WayID(2).FeatureID(),
RelationID(3).FeatureID(),
}
if ids := ms.FeatureIDs(); !reflect.DeepEqual(ids, fids) {
t.Errorf("incorrect feature ids: %v", ids)
}
}

func TestRelations_ids(t *testing.T) {
rs := Relations{
{ID: 1, Version: 3},
{ID: 2, Version: 4},
}

eids := ElementIDs{RelationID(1).ElementID(3), RelationID(2).ElementID(4)}
if ids := rs.ElementIDs(); !reflect.DeepEqual(ids, eids) {
t.Errorf("incorrect element ids: %v", ids)
}

fids := FeatureIDs{RelationID(1).FeatureID(), RelationID(2).FeatureID()}
if ids := rs.FeatureIDs(); !reflect.DeepEqual(ids, fids) {
t.Errorf("incorrect feature ids: %v", ids)
}

rids := []RelationID{1, 2}
if ids := rs.IDs(); !reflect.DeepEqual(ids, rids) {
t.Errorf("incorrect node id: %v", rids)
}
}

func TestRelations_SortByIDVersion(t *testing.T) {
rs := Relations{
{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},
}

rs.SortByIDVersion()

eids := ElementIDs{
RelationID(2).ElementID(4),
RelationID(3).ElementID(4),
RelationID(4).ElementID(4),
RelationID(5).ElementID(2),
RelationID(5).ElementID(3),
RelationID(5).ElementID(4),
RelationID(7).ElementID(3),
RelationID(9).ElementID(4),
}

if ids := rs.ElementIDs(); !reflect.DeepEqual(ids, eids) {
t.Errorf("incorrect sort: %v", eids)
}
}

0 comments on commit a7dae28

Please sign in to comment.