mirror of
https://github.com/elyby/chrly.git
synced 2024-12-11 15:59:18 +05:30
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
|
package queue
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
func (m *uuidsStorageMock) StoreUuid(username string, uuid string) {
|
||
|
m.Called(username, uuid)
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
func (m *texturesStorageMock) StoreTextures(textures *mojang.SignedTexturesResponse) {
|
||
|
m.Called(textures)
|
||
|
}
|
||
|
|
||
|
func TestSplittedStorage(t *testing.T) {
|
||
|
createMockedStorage := func() (*SplittedStorage, *uuidsStorageMock, *texturesStorageMock) {
|
||
|
uuidsStorage := &uuidsStorageMock{}
|
||
|
texturesStorage := &texturesStorageMock{}
|
||
|
|
||
|
return &SplittedStorage{uuidsStorage, texturesStorage}, uuidsStorage, texturesStorage
|
||
|
}
|
||
|
|
||
|
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()
|
||
|
storage.StoreUuid("username", "result")
|
||
|
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) {
|
||
|
toStore := &mojang.SignedTexturesResponse{Id: "mock id"}
|
||
|
storage, _, texturesMock := createMockedStorage()
|
||
|
texturesMock.On("StoreTextures", toStore).Once()
|
||
|
storage.StoreTextures(toStore)
|
||
|
texturesMock.AssertExpectations(t)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func TestValueNotFound_Error(t *testing.T) {
|
||
|
err := &ValueNotFound{}
|
||
|
assert.Equal(t, "value not found in the storage", err.Error())
|
||
|
}
|