github.com/omniscale/go-osm@v0.3.1/state/state_test.go (about)

     1  package state
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestParse(t *testing.T) {
    12  	s, err := ParseFile("964.state.txt")
    13  	if err != nil {
    14  		t.Fatal("parsing state", err)
    15  	}
    16  
    17  	if s.Sequence != 964 {
    18  		t.Error("unexpected sequence", s)
    19  	}
    20  	if !s.Time.Equal(time.Date(2015, time.May, 4, 0, 0, 0, 0, time.UTC)) {
    21  		t.Error("unexpected time", s)
    22  	}
    23  	if s.URL != "" {
    24  		t.Error("unexpected URL", s)
    25  	}
    26  }
    27  
    28  func TestWriteFile(t *testing.T) {
    29  	f, err := ioutil.TempFile("", "")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	f.Close()
    34  	defer os.Remove(f.Name())
    35  
    36  	want := &DiffState{
    37  		Time:     time.Date(2018, time.November, 22, 10, 42, 0, 0, time.UTC),
    38  		URL:      "https://planet.openstreetmap.org/replication/minute/",
    39  		Sequence: 123456,
    40  	}
    41  	err = WriteFile(f.Name(), want)
    42  	if err != nil {
    43  		t.Fatal("writing state", err)
    44  	}
    45  
    46  	content, err := ioutil.ReadFile(f.Name())
    47  	if err != nil {
    48  		t.Fatal("reading state file", err)
    49  	}
    50  
    51  	if string(content) != `timestamp=2018-11-22T10\:42\:00Z
    52  sequenceNumber=123456
    53  replicationUrl=https://planet.openstreetmap.org/replication/minute/
    54  ` {
    55  		t.Error("unexpected content", string(content))
    56  	}
    57  
    58  	got, err := ParseFile(f.Name())
    59  	if err != nil {
    60  		t.Fatal("reading state file", err)
    61  	}
    62  
    63  	if !reflect.DeepEqual(got, want) {
    64  		t.Error("parsed state differs", got, want)
    65  	}
    66  }