Add shared/utils_test.go
This commit is contained in:
parent
6e08d543fc
commit
4ba9275e7d
1 changed files with 50 additions and 0 deletions
50
shared/utils_test.go
Normal file
50
shared/utils_test.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package shared
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
"github.com/yosssi/boltstore/shared/protobuf"
|
||||||
|
|
||||||
|
"code.google.com/p/gogoprotobuf/proto"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSession(t *testing.T) {
|
||||||
|
expiresAt := time.Now().Unix()
|
||||||
|
sessionOrig := &protobuf.Session{
|
||||||
|
Values: []byte("test"),
|
||||||
|
ExpiresAt: &expiresAt,
|
||||||
|
}
|
||||||
|
data, err := proto.Marshal(sessionOrig)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
}
|
||||||
|
session, err := Session(data)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
}
|
||||||
|
if string(session.Values) != "test" || *session.ExpiresAt != expiresAt {
|
||||||
|
t.Errorf("Session() should return %+v (actual: %+v)", sessionOrig, session)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestExpired(t *testing.T) {
|
||||||
|
// When Expired() should return true.
|
||||||
|
expiresAt := time.Now().Unix()
|
||||||
|
session := protobuf.Session{
|
||||||
|
Values: []byte("test"),
|
||||||
|
ExpiresAt: &expiresAt,
|
||||||
|
}
|
||||||
|
if Expired(session) != true {
|
||||||
|
t.Error("Expired() should return true (actual: false)")
|
||||||
|
}
|
||||||
|
|
||||||
|
// When Expired() should return false.
|
||||||
|
expiresAt = time.Now().Add(time.Hour).Unix()
|
||||||
|
session = protobuf.Session{
|
||||||
|
Values: []byte("test"),
|
||||||
|
ExpiresAt: &expiresAt,
|
||||||
|
}
|
||||||
|
if Expired(session) != false {
|
||||||
|
t.Error("Expired() should return false (actual: true)")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue