Skip to content

Commit

Permalink
fix dialog example, improve mock session storage methods, add nop and…
Browse files Browse the repository at this point in the history
… mock storages tests
  • Loading branch information
bbrodriges committed Jan 11, 2019
1 parent 05737a2 commit dff068c
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 10 deletions.
5 changes: 3 additions & 2 deletions dialog/examples/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import (
"encoding/json"
"github.com/bbrodriges/mielofon"
"net/http"

"github.com/bbrodriges/mielofon/dialog/dialogutil"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
input, output, err := mielofon.GetDialogPair(r.Body)
input, output, err := dialogutil.GetDialogPair(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
Expand Down
16 changes: 8 additions & 8 deletions session/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ var _ StoreSeeker = MockStore{}

type MockStore struct {
Store map[string]interface{}
MockGet func(id string) (interface{}, error)
MockSet func(id string, sess interface{}) error
MockDelete func(id string) error
MockCount func() int
MockGet func(st MockStore, id string) (interface{}, error)
MockSet func(st MockStore, id string, sess interface{}) error
MockDelete func(st MockStore, id string) error
MockCount func(st MockStore) int
}

func (s MockStore) Get(id string) (interface{}, error) {
return s.MockGet(id)
return s.MockGet(s, id)
}

func (s MockStore) Set(id string, sess interface{}) error {
return s.MockSet(id, sess)
return s.MockSet(s, id, sess)
}

func (s MockStore) Delete(id string) error {
return s.MockDelete(id)
return s.MockDelete(s, id)
}

func (s MockStore) Count() int {
return s.MockCount()
return s.MockCount(s)
}

func (s MockStore) VisitAll(f VisitFunc) {
Expand Down
178 changes: 178 additions & 0 deletions session/mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package session

import (
"errors"
"github.com/stretchr/testify/assert"
"math/rand"
"sort"
"testing"
)

func TestMockStoreGet(t *testing.T) {
called := false
ms := MockStore{
MockGet: func(_ MockStore, _ string) (interface{}, error) {
called = true
return NopSession{}, nil
},
}
sess, err := ms.Get("ololo")
assert.Nil(t, err)
assert.Equal(t, NopSession{}, sess)
assert.True(t, called)

called = false
ms = MockStore{
MockGet: func(_ MockStore, _ string) (interface{}, error) {
called = true
return nil, errors.New("unknown error")
},
}
sess, err = ms.Get("ololo")
assert.Nil(t, sess)
assert.Error(t, err)
assert.True(t, called)

called = false
ms = MockStore{
Store: map[string]interface{}{"ololo": NopSession{}},
MockGet: func(s MockStore, id string) (interface{}, error) {
called = true
if sess, ok := s.Store[id]; ok {
return sess, nil
}
return nil, errors.New("not found")
},
}
sess, err = ms.Get("ololo")
assert.Equal(t, NopSession{}, sess)
assert.NoError(t, err)
assert.True(t, called)
}

func TestMockStoreSet(t *testing.T) {
called := false
ms := MockStore{
MockSet: func(_ MockStore, _ string, _ interface{}) error {
called = true
return nil
},
}
err := ms.Set("ololo", NopSession{})
assert.Nil(t, err)
assert.True(t, called)

called = false
ms = MockStore{
MockSet: func(_ MockStore, _ string, _ interface{}) error {
called = true
return errors.New("unknown error")
},
}
err = ms.Set("ololo", NopSession{})
assert.Error(t, err)
assert.True(t, called)

called = false
ms = MockStore{
Store: make(map[string]interface{}),
MockSet: func(s MockStore, id string, sess interface{}) error {
called = true
s.Store[id] = sess
return nil
},
}
err = ms.Set("ololo", NopSession{})
assert.NoError(t, err)
assert.Equal(t, map[string]interface{}{"ololo": NopSession{}}, ms.Store)
assert.True(t, called)
}

func TestMockStoreDelete(t *testing.T) {
called := false
ms := MockStore{
MockDelete: func(_ MockStore, _ string) error {
called = true
return nil
},
}
err := ms.Delete("ololo")
assert.Nil(t, err)
assert.True(t, called)

called = false
ms = MockStore{
MockDelete: func(_ MockStore, _ string) error {
called = true
return errors.New("unknown error")
},
}
err = ms.Delete("ololo")
assert.Error(t, err)
assert.True(t, called)

called = false
ms = MockStore{
Store: map[string]interface{}{"ololo": NopSession{}},
MockDelete: func(s MockStore, id string) error {
called = true
delete(s.Store, id)
return nil
},
}
err = ms.Delete("ololo")
assert.Nil(t, err)
assert.True(t, called)
assert.Len(t, ms.Store, 0)
}

func TestMockStoreCount(t *testing.T) {
expectCount := rand.Intn(1000000)

called := false
ms := MockStore{
MockCount: func(_ MockStore) int {
called = true
return expectCount
},
}
count := ms.Count()
assert.Equal(t, expectCount, count)
assert.True(t, called)

called = false
ms = MockStore{
Store: map[string]interface{}{"ololo": NopSession{}},
MockCount: func(s MockStore) int {
called = true
return len(s.Store)
},
}
count = ms.Count()
assert.Equal(t, 1, count)
assert.True(t, called)
}

func TestMockStoreVisitAll(t *testing.T) {
ms := MockStore{
Store: map[string]interface{}{
"ololo": NopSession{},
"trololo": NopSession{},
},
}

var expectedIds []string
for id := range ms.Store {
expectedIds = append(expectedIds, id)
}

var visitedIds []string
ms.VisitAll(func(id string, _ interface{}) bool {
visitedIds = append(visitedIds, id)
return true
})

sort.StringSlice(expectedIds).Sort()
sort.StringSlice(visitedIds).Sort()
assert.Equal(t, expectedIds, visitedIds)
}
47 changes: 47 additions & 0 deletions session/nop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package session

import (
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)

func TestNopStoreGet(t *testing.T) {
ns := NopStore{}
val, err := ns.Get("ololo")
assert.Equal(t, NopSession{}, val)
assert.Nil(t, err)
}

func TestNopStoreSet(t *testing.T) {
ns := NopStore{}
assert.Nil(t, ns.Set("ololo", NopSession{}))
}

func TestNopStoreDelete(t *testing.T) {
ns := NopStore{}
assert.Nil(t, ns.Delete("ololo"))
}

func TestNopStoreCount(t *testing.T) {
ns := NopStore{}
assert.Equal(t, 0, ns.Count())
}

func TestNopStoreVisitAll(t *testing.T) {
ns := NopStore{}

for i := 0; i < 5; i++ {
id := strconv.FormatInt(int64(i), 10)
err := ns.Set(id, NopSession{})
assert.NoError(t, err)
}

var visitedIds []string
ns.VisitAll(func(id string, _ interface{}) bool {
visitedIds = append(visitedIds, id)
return true
})

assert.Len(t, visitedIds, 0)
}

0 comments on commit dff068c

Please sign in to comment.