Files
threshold/internal/oplog/store/scanner_test.go
T
34f530e0df Basic oplog capability (#6)
The ability to write oplog entries using a ticket based system to ensure serialization.   Uses a go-kms-wrapper for encrypting/decrypting entries.

Co-authored-by: Jim Lambert <jlm.lambert@gmail.com>
Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com>
Co-authored-by: Todd Knight <T.Alan.Knight@gmail.com>
Co-authored-by: Jeff Mitchell <jeffrey.mitchell@gmail.com>
2020-04-08 14:55:40 -04:00

74 lines
1.9 KiB
Go

package store
import (
"reflect"
"testing"
"time"
"github.com/golang/protobuf/ptypes/timestamp"
"gotest.tools/assert"
)
func Test_TimestampValue(t *testing.T) {
t.Parallel()
t.Run("valid", func(t *testing.T) {
ts := Timestamp{Timestamp: &timestamp.Timestamp{Seconds: 0, Nanos: 0}}
v, err := ts.Value()
assert.NilError(t, err)
assert.Equal(t, v, utcDate(1970, 1, 1))
})
t.Run("valid nil ts", func(t *testing.T) {
var ts *Timestamp
v, err := ts.Value()
assert.NilError(t, err)
assert.Equal(t, v, nil)
})
t.Run("invalid ts", func(t *testing.T) {
ts := Timestamp{Timestamp: &timestamp.Timestamp{Seconds: maxValidSeconds, Nanos: 0}}
v, err := ts.Value()
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "timestamp: seconds:253402300800 after 10000-01-01")
assert.Equal(t, v, utcDate(10000, 1, 1))
})
}
func Test_TimestampScan(t *testing.T) {
t.Parallel()
t.Run("valid", func(t *testing.T) {
v := time.Unix(0, 0)
ts := Timestamp{}
err := ts.Scan(v)
assert.NilError(t, err)
assert.Check(t, reflect.DeepEqual(ts.Timestamp, &timestamp.Timestamp{Seconds: 0, Nanos: 0}))
})
t.Run("valid default time", func(t *testing.T) {
var v time.Time
ts := Timestamp{}
err := ts.Scan(v)
assert.NilError(t, err)
assert.Check(t, reflect.DeepEqual(ts.Timestamp, &timestamp.Timestamp{Seconds: -62135596800, Nanos: 0}))
})
t.Run("invalid type", func(t *testing.T) {
v := 1
ts := Timestamp{}
err := ts.Scan(v)
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "Not a protobuf Timestamp")
})
t.Run("invalid time", func(t *testing.T) {
v := time.Unix(maxValidSeconds, 0)
ts := Timestamp{}
err := ts.Scan(v)
assert.Check(t, err != nil)
assert.Equal(t, err.Error(), "error converting the timestamp: timestamp: seconds:253402300800 after 10000-01-01")
})
}
const maxValidSeconds = 253402300800
func utcDate(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}