Skip to content

Commit

Permalink
add test for marshal date (apache#886)
Browse files Browse the repository at this point in the history
* Support marshal date type

* add test for marshal date
  • Loading branch information
evildecay authored and Zariel committed Apr 1, 2017
1 parent f057eea commit f8b6dca
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,52 @@ func TestUnmarshalDate(t *testing.T) {
expectedDate := "2017-02-04"
formattedDate := date.Format("2006-01-02")
if expectedDate != formattedDate {
t.Errorf("marshalTest: expected %x (%v), got %x (%v)", expectedDate, formattedDate)
t.Errorf("marshalTest: expected %v, got %v", expectedDate, formattedDate)
return
}
}

func TestMarshalDate(t *testing.T) {
now := time.Now()
timestamp := now.UnixNano()/int64(time.Millisecond)
expectedData := encInt(int32(timestamp/86400000 + int64(1 << 31)))
var marshalDateTests = []struct {
Info TypeInfo
Data []byte
Value interface{}
}{
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
timestamp,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
now,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
&now,
},
{
NativeType{proto: 4, typ: TypeDate},
expectedData,
now.Format("2006-01-02"),
},
}

for i, test := range marshalDateTests {
t.Log(i, test)
data, err := Marshal(test.Info, test.Value)
if err != nil {
t.Errorf("marshalTest[%d]: %v", i, err)
continue
}
if !bytes.Equal(data, test.Data) {
t.Errorf("marshalTest[%d]: expected %x (%v), got %x (%v) for time %s", i,
test.Data, decInt(test.Data), data, decInt(data), test.Value)
}
}
}

0 comments on commit f8b6dca

Please sign in to comment.