chrly/mojangtextures/storage_test.go

90 lines
2.5 KiB
Go
Raw Normal View History

package mojangtextures
2019-04-27 04:16:15 +05:30
import (
"github.com/elyby/chrly/api/mojang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
)
type uuidsStorageMock struct {
mock.Mock
}
func (m *uuidsStorageMock) GetUuid(username string) (string, error) {
args := m.Called(username)
return args.String(0), args.Error(1)
}
2019-05-06 19:42:37 +05:30
func (m *uuidsStorageMock) StoreUuid(username string, uuid string) error {
2019-04-27 04:16:15 +05:30
m.Called(username, uuid)
2019-05-06 19:42:37 +05:30
return nil
2019-04-27 04:16:15 +05:30
}
type texturesStorageMock struct {
mock.Mock
}
func (m *texturesStorageMock) GetTextures(uuid string) (*mojang.SignedTexturesResponse, error) {
args := m.Called(uuid)
var result *mojang.SignedTexturesResponse
if casted, ok := args.Get(0).(*mojang.SignedTexturesResponse); ok {
result = casted
}
return result, args.Error(1)
}
2019-05-06 01:36:29 +05:30
func (m *texturesStorageMock) StoreTextures(uuid string, textures *mojang.SignedTexturesResponse) {
m.Called(uuid, textures)
2019-04-27 04:16:15 +05:30
}
func TestSplittedStorage(t *testing.T) {
createMockedStorage := func() (*SeparatedStorage, *uuidsStorageMock, *texturesStorageMock) {
2019-04-27 04:16:15 +05:30
uuidsStorage := &uuidsStorageMock{}
texturesStorage := &texturesStorageMock{}
return &SeparatedStorage{uuidsStorage, texturesStorage}, uuidsStorage, texturesStorage
2019-04-27 04:16:15 +05:30
}
t.Run("GetUuid", func(t *testing.T) {
storage, uuidsMock, _ := createMockedStorage()
uuidsMock.On("GetUuid", "username").Once().Return("find me", nil)
result, err := storage.GetUuid("username")
assert.Nil(t, err)
assert.Equal(t, "find me", result)
uuidsMock.AssertExpectations(t)
})
t.Run("StoreUuid", func(t *testing.T) {
storage, uuidsMock, _ := createMockedStorage()
uuidsMock.On("StoreUuid", "username", "result").Once()
2019-05-06 19:42:37 +05:30
_ = storage.StoreUuid("username", "result")
2019-04-27 04:16:15 +05:30
uuidsMock.AssertExpectations(t)
})
t.Run("GetTextures", func(t *testing.T) {
result := &mojang.SignedTexturesResponse{Id: "mock id"}
storage, _, texturesMock := createMockedStorage()
texturesMock.On("GetTextures", "uuid").Once().Return(result, nil)
returned, err := storage.GetTextures("uuid")
assert.Nil(t, err)
assert.Equal(t, result, returned)
texturesMock.AssertExpectations(t)
})
t.Run("StoreTextures", func(t *testing.T) {
2019-05-06 01:36:29 +05:30
toStore := &mojang.SignedTexturesResponse{}
2019-04-27 04:16:15 +05:30
storage, _, texturesMock := createMockedStorage()
2019-05-06 01:36:29 +05:30
texturesMock.On("StoreTextures", "mock id", toStore).Once()
storage.StoreTextures("mock id", toStore)
2019-04-27 04:16:15 +05:30
texturesMock.AssertExpectations(t)
})
}
func TestValueNotFound_Error(t *testing.T) {
err := &ValueNotFound{}
assert.Equal(t, "value not found in the storage", err.Error())
}