Created
July 2, 2014 18:54
-
-
Save bryanl/975a53365ac6786ff9ad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
type KeyPair struct { | |
Key string | |
Value []byte | |
} | |
type KeyStore interface { | |
Get(k string) (*KeyPair, error) | |
} | |
type MockKeyStore struct { | |
Dict map[string][]byte | |
} | |
func NewRedisKeyStore() { | |
return &RedisKeyStore{ | |
r: redis.Redis{}, | |
} | |
} | |
func (rks *RedisKeyStore) Get(k string) (*KeyPair, error) { | |
rkvp, err := rks.r.Get(k) | |
if err != nil { | |
return nil, err | |
} | |
return &KeyPair{ | |
Key: rkvp.Key, | |
Value: rkvp.Value, | |
}, nil | |
} | |
func NewMockKeyStore() { | |
return &MockKeyStore{ | |
Dict: map[string][]byte{}, | |
} | |
} | |
func (mks *MockKeyStore) Get(k string) (*KeyPair, error) { | |
return &KeyPair{Key: k, Value: mks.Dict[k]}, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a great piece for a beginner in programming and TDD. In all languages I have tried Go is the easiest to unit test and this just made it easier. Now my interfaces know what to do in my code.