github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/logs/internal/timelog/timelog_test.go (about)

     1  package timelog
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestTimeStackDriverJSONMarshal(t *testing.T) {
    10  	tu := time.Unix(1000000000, 0)
    11  
    12  	loc, err := time.LoadLocation("Asia/Shanghai")
    13  
    14  	if err != nil {
    15  		panic(err)
    16  	}
    17  
    18  	tu = tu.In(loc)
    19  
    20  	var past = TimeStackDriver(tu)
    21  
    22  	var data []byte
    23  	data, err = json.Marshal(past)
    24  
    25  	var want = `"2001-09-09T09:46:40+08:00"`
    26  
    27  	if err != nil {
    28  		t.Errorf("Expected no error, got %v instead", err)
    29  	}
    30  
    31  	if string(data) != want {
    32  		t.Errorf("Expected time to be %v, got %v instead", want, string(data))
    33  	}
    34  }
    35  
    36  func TestTimeStackDriverJSONUnmarshal(t *testing.T) {
    37  	var data = `"2019-06-03T21:31:05.908999919Z"`
    38  	var want = int64(1559597465)
    39  	var r TimeStackDriver
    40  
    41  	if err := json.Unmarshal([]byte(data), &r); err != nil {
    42  		t.Errorf("Expected no error, got %v instead", err)
    43  	}
    44  
    45  	var rt = time.Time(r)
    46  	var got = rt.Unix()
    47  
    48  	if got != want {
    49  		t.Errorf("Expected Unix time %v, got %v instead", want, got)
    50  	}
    51  }
    52  
    53  func TestTimeStackDriverJSONUnmarshalNull(t *testing.T) {
    54  	var data = `null`
    55  	var r TimeStackDriver
    56  
    57  	if err := json.Unmarshal([]byte(data), &r); err != nil {
    58  		t.Errorf("Expected no error, got %v instead", err)
    59  	}
    60  
    61  	var rt = time.Time(r)
    62  
    63  	if (rt != time.Time{}) {
    64  		t.Error("Expected null to be ignored")
    65  	}
    66  }