github.com/omniscale/go-osm@v0.3.1/parser/diff/parser_test.go (about)

     1  package diff
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"os"
     7  	"reflect"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/omniscale/go-osm"
    13  )
    14  
    15  func TestParse(t *testing.T) {
    16  	conf := Config{
    17  		Diffs:           make(chan osm.Diff),
    18  		IncludeMetadata: true,
    19  	}
    20  	f, err := os.Open("612.osc.gz")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer f.Close()
    25  
    26  	p, err := NewGZIP(f, conf)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  
    31  	diffs := []osm.Diff{}
    32  	wg := sync.WaitGroup{}
    33  	wg.Add(1)
    34  	go func() {
    35  		for d := range conf.Diffs {
    36  			diffs = append(diffs, d)
    37  		}
    38  		wg.Done()
    39  	}()
    40  	err = p.Parse(context.Background())
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	wg.Wait()
    46  
    47  	for _, tc := range []struct {
    48  		Idx  int
    49  		Name string
    50  		Want osm.Diff
    51  	}{
    52  		{
    53  			Name: "modified node",
    54  			Idx:  0,
    55  			Want: osm.Diff{
    56  				Create: false,
    57  				Modify: true,
    58  				Delete: false,
    59  				Node: &osm.Node{
    60  					Element: osm.Element{
    61  						ID:       25594547,
    62  						Tags:     osm.Tags{"source": "SRTM"},
    63  						Metadata: &osm.Metadata{UserID: 462835, UserName: "G-eMapper", Version: 3, Timestamp: time.Date(2016, 12, 2, 14, 15, 11, 0, time.UTC), Changeset: 44115151},
    64  					},
    65  					Lat:  16.187913,
    66  					Long: 122.0913159,
    67  				},
    68  			},
    69  		},
    70  		{
    71  			Name: "deleted node",
    72  			Idx:  47,
    73  			Want: osm.Diff{
    74  				Create: false,
    75  				Modify: false,
    76  				Delete: true,
    77  				Node: &osm.Node{
    78  					Element: osm.Element{
    79  						ID:       1884933281,
    80  						Metadata: &osm.Metadata{UserID: 3315483, UserName: "8dirfriend", Version: 2, Timestamp: time.Date(2016, 12, 2, 14, 15, 10, 0, time.UTC), Changeset: 44115150},
    81  					},
    82  					Lat:  35.0233546,
    83  					Long: 132.879755,
    84  				},
    85  			},
    86  		},
    87  		{
    88  			Idx:  1753,
    89  			Name: "added node",
    90  			Want: osm.Diff{
    91  				Create: true,
    92  				Modify: false,
    93  				Delete: false,
    94  				Node: &osm.Node{
    95  					Element: osm.Element{
    96  						ID:       4533952893,
    97  						Tags:     osm.Tags{"amenity": "hospital", "name": "Кожно-венерологический диспансер", "name:ru": "Кожно-венерологический диспансер"},
    98  						Metadata: &osm.Metadata{UserID: 4112953, UserName: "Sergei97", Version: 1, Timestamp: time.Date(2016, 12, 2, 14, 15, 19, 0, time.UTC), Changeset: 44115157},
    99  					},
   100  					Lat:  52.563681,
   101  					Long: 24.4658314,
   102  				},
   103  			},
   104  		},
   105  		{
   106  			Idx:  2267,
   107  			Name: "modified way",
   108  			Want: osm.Diff{
   109  				Create: false,
   110  				Modify: true,
   111  				Delete: false,
   112  				Way: &osm.Way{
   113  					Element: osm.Element{
   114  						ID:       6863685,
   115  						Tags:     osm.Tags{"highway": "unclassified", "maxspeed": "30", "name": "Oranjestraat", "oneway": "yes", "cycleway": "opposite"},
   116  						Metadata: &osm.Metadata{UserID: 619707, UserName: "openMvD", Version: 6, Timestamp: time.Date(2016, 12, 2, 14, 15, 6, 0, time.UTC), Changeset: 44115110},
   117  					},
   118  					Refs:  []int64{44776397, 44776575, 4534010578, 44776865, 4534010576, 44780387},
   119  					Nodes: nil,
   120  				}},
   121  		},
   122  		{
   123  			Idx:  2563,
   124  			Name: "modified relation",
   125  			Want: osm.Diff{
   126  				Create: false,
   127  				Modify: true,
   128  				Delete: false,
   129  				Rel: &osm.Relation{
   130  					Element: osm.Element{
   131  						ID:       2139646,
   132  						Tags:     osm.Tags{"destination": "Balonne River", "name": "Condamine River", "type": "waterway", "waterway": "river", "wikidata": "Q805500", "wikipedia": "en:Condamine River"},
   133  						Metadata: &osm.Metadata{UserID: 1185091, UserName: "nick0252", Version: 13, Timestamp: time.Date(2016, 12, 2, 14, 15, 32, 0, time.UTC), Changeset: 44115162},
   134  					},
   135  					Members: []osm.Member{
   136  						{ID: 142320051, Type: 1, Role: "main_stream"},
   137  						{ID: 162045587, Type: 1, Role: "main_stream"},
   138  						{ID: 162045590, Type: 1, Role: "main_stream"},
   139  						{ID: 162047493, Type: 1, Role: "main_stream"},
   140  						{ID: 199021540, Type: 1, Role: "main_stream"},
   141  						{ID: 199021536, Type: 1, Role: "main_stream"},
   142  						{ID: 162077162, Type: 1, Role: "main_stream"},
   143  						{ID: 165967517, Type: 1, Role: "main_stream"},
   144  						{ID: 165967518, Type: 1, Role: "main_stream"},
   145  						{ID: 165967519, Type: 1, Role: "main_stream"},
   146  						{ID: 165967516, Type: 1, Role: "main_stream"},
   147  						{ID: 165967520, Type: 1, Role: "main_stream"},
   148  						{ID: 41454859, Type: 1, Role: "main_stream"},
   149  						{ID: 166808071, Type: 1, Role: "main_stream"},
   150  						{ID: 166814228, Type: 1, Role: "main_stream"},
   151  						{ID: 149724655, Type: 1, Role: "main_stream"},
   152  						{ID: 165368857, Type: 1, Role: "main_stream"},
   153  						{ID: 162077161, Type: 1, Role: "side_stream"},
   154  						{ID: 162077160, Type: 1, Role: "side_stream"},
   155  						{ID: 457226217, Type: 1, Role: "outer"},
   156  						{ID: 457226545, Type: 1, Role: "inner"},
   157  					},
   158  				},
   159  			},
   160  		},
   161  	} {
   162  		t.Run(tc.Name, func(t *testing.T) {
   163  			if !reflect.DeepEqual(diffs[tc.Idx], tc.Want) {
   164  				fmt.Printf("%#v\n", diffs[tc.Idx])
   165  				t.Errorf("unexpected diff, got:\n%#v\nwant:\n%#v", diffs[tc.Idx], tc.Want)
   166  			}
   167  		})
   168  	}
   169  }