Skip to content

Commit

Permalink
refactor: change session API, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lonng committed Jan 13, 2017
1 parent 10ce85a commit b4db1a0
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 59 deletions.
118 changes: 59 additions & 59 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,180 +116,180 @@ func (s *Session) Set(key string, value interface{}) {
s.data[key] = value
}

func (s *Session) Int(key string) (int, error) {
func (s *Session) HasKey(key string) bool {
_, has := s.data[key]
return has
}

func (s *Session) Int(key string) int {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(int)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Int8(key string) (int8, error) {
func (s *Session) Int8(key string) int8 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(int8)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Int16(key string) (int16, error) {
func (s *Session) Int16(key string) int16 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(int16)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Int32(key string) (int32, error) {
func (s *Session) Int32(key string) int32 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(int32)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Int64(key string) (int64, error) {
func (s *Session) Int64(key string) int64 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(int64)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Uint(key string) (uint, error) {
func (s *Session) Uint(key string) uint {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(uint)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Uint8(key string) (uint8, error) {
func (s *Session) Uint8(key string) uint8 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(uint8)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Uint16(key string) (uint16, error) {
func (s *Session) Uint16(key string) uint16 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(uint16)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Uint32(key string) (uint32, error) {
func (s *Session) Uint32(key string) uint32 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(uint32)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Uint64(key string) (uint64, error) {
func (s *Session) Uint64(key string) uint64 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(uint64)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Float32(key string) (float32, error) {
func (s *Session) Float32(key string) float32 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(float32)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) Float64(key string) (float64, error) {
func (s *Session) Float64(key string) float64 {
v, ok := s.data[key]
if !ok {
return 0, ErrKeyNotFound
return 0
}

value, ok := v.(float64)
if !ok {
return 0, ErrWrongValueType
return 0
}
return value, nil
return value
}

func (s *Session) String(key string) (string, error) {
func (s *Session) String(key string) string {
v, ok := s.data[key]
if !ok {
return "", ErrKeyNotFound
return ""
}

value, ok := v.(string)
if !ok {
return "", ErrWrongValueType
return ""
}
return value, nil
return value
}

func (s *Session) Value(key string) (interface{}, error) {
v, ok := s.data[key]
if !ok {
return nil, ErrKeyNotFound
}

return v, nil
func (s *Session) Value(key string) interface{} {
return s.data[key]
}
149 changes: 149 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,151 @@
package session

import "testing"

func TestNewSession(t *testing.T) {
s := NewSession(nil)
if s.ID < 1 {
t.Fail()
}
}

func TestSession_Bind(t *testing.T) {
s := NewSession(nil)
uids := []int64{100,1000,10000000}
for i, uid := range uids {
s.Bind(uid)
if s.Uid != uids[i] {
t.Fail()
}
}
}

func TestSession_HasKey(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := "world"
s.Set(key,value)
if !s.HasKey(key) {
t.Fail()
}
}

func TestSession_Float32(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := float32(1.2000)
s.Set(key,value)
if value != s.Float32(key) {
t.Fail()
}
}

func TestSession_Float64(t *testing.T) {
s := NewSession(nil)
key := "hello"
value := 1.2000
s.Set(key,value)
if value != s.Float64(key) {
t.Fail()
}
}

func TestSession_Int(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := 234
s.Set(key,value)
if value != s.Int(key) {
t.Fail()
}
}

func TestSession_Int8(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := int8(123)
s.Set(key,value)
if value != s.Int8(key) {
t.Fail()
}
}

func TestSession_Int16(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := int16(3245)
s.Set(key,value)
if value != s.Int16(key) {
t.Fail()
}
}

func TestSession_Int32(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := int32(5454)
s.Set(key,value)
if value != s.Int32(key) {
t.Fail()
}
}

func TestSession_Int64(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := int64(444454)
s.Set(key,value)
if value != s.Int64(key) {
t.Fail()
}
}

func TestSession_Uint(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint(24254)
s.Set(key,value)
if value != s.Uint(key) {
t.Fail()
}
}

func TestSession_Uint8(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint8(34)
s.Set(key,value)
if value != s.Uint8(key) {
t.Fail()
}
}

func TestSession_Uint16(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint16(4645)
s.Set(key,value)
if value != s.Uint16(key) {
t.Fail()
}
}

func TestSession_Uint32(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint32(12365)
s.Set(key,value)
if value != s.Uint32(key) {
t.Fail()
}
}

func TestSession_Uint64(t *testing.T) {
s := NewSession(nil)
key := "testkey"
value := uint64(1000)
s.Set(key,value)
if value != s.Uint64(key) {
t.Fail()
}
}

0 comments on commit b4db1a0

Please sign in to comment.