Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
corinapurcarea committed Aug 12, 2020
1 parent 1db2250 commit c36606f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
68 changes: 68 additions & 0 deletions codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,74 @@ func TestGetWhenHit(t *testing.T) {
assert.Equal(t, 0, codec.GetStats().ClearError)
}

func TestGetWithTTLWhenHit(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cacheValue := &struct {
Hello string
}{
Hello: "world",
}

store := mocksStore.NewMockStoreInterface(ctrl)
store.EXPECT().GetWithTTL("my-key").Return(cacheValue, 1*time.Second, nil)

codec := New(store)

// When
value, ttl, err := codec.GetWithTTL("my-key")

// Then
assert.Nil(t, err)
assert.Equal(t, cacheValue, value)
assert.Equal(t, 1*time.Second, ttl)

assert.Equal(t, 1, codec.GetStats().Hits)
assert.Equal(t, 0, codec.GetStats().Miss)
assert.Equal(t, 0, codec.GetStats().SetSuccess)
assert.Equal(t, 0, codec.GetStats().SetError)
assert.Equal(t, 0, codec.GetStats().DeleteSuccess)
assert.Equal(t, 0, codec.GetStats().DeleteError)
assert.Equal(t, 0, codec.GetStats().InvalidateSuccess)
assert.Equal(t, 0, codec.GetStats().InvalidateError)
assert.Equal(t, 0, codec.GetStats().ClearSuccess)
assert.Equal(t, 0, codec.GetStats().ClearError)
}

func TestGetWithTTLWhenMiss(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

expectedErr := errors.New("Unable to find in store")

store := mocksStore.NewMockStoreInterface(ctrl)
store.EXPECT().GetWithTTL("my-key").Return(nil, 0*time.Second, expectedErr)

codec := New(store)

// When
value, ttl, err := codec.GetWithTTL("my-key")

// Then
assert.Equal(t, expectedErr, err)
assert.Nil(t, value)
assert.Equal(t, 0*time.Second, ttl)

assert.Equal(t, 0, codec.GetStats().Hits)
assert.Equal(t, 1, codec.GetStats().Miss)
assert.Equal(t, 0, codec.GetStats().SetSuccess)
assert.Equal(t, 0, codec.GetStats().SetError)
assert.Equal(t, 0, codec.GetStats().DeleteSuccess)
assert.Equal(t, 0, codec.GetStats().DeleteError)
assert.Equal(t, 0, codec.GetStats().InvalidateSuccess)
assert.Equal(t, 0, codec.GetStats().InvalidateError)
assert.Equal(t, 0, codec.GetStats().ClearSuccess)
assert.Equal(t, 0, codec.GetStats().ClearError)
}

func TestGetWhenMiss(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
Expand Down
22 changes: 22 additions & 0 deletions store/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ func TestMemcacheGetWithTTL(t *testing.T) {
assert.Equal(t, 5*time.Second, ttl)
}

func TestMemcacheGetWithTTLWhenMissingItem(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
defer ctrl.Finish()

cacheKey := "my-key"

client := mocksStore.NewMockMemcacheClientInterface(ctrl)
client.EXPECT().Get(cacheKey).Return(nil, nil)

options := &Options{Expiration: 3 * time.Second}
store := NewMemcache(client, options)

// When
value, ttl, err := store.GetWithTTL(cacheKey)

// Then
assert.NotNil(t, err)
assert.Nil(t, value)
assert.Equal(t, 0*time.Second, ttl)
}

func TestMemcacheGetWithTTLWhenError(t *testing.T) {
// Given
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit c36606f

Please sign in to comment.