migrate from mockery to moq

This commit is contained in:
Dmitry Verkhoturov
2022-10-02 21:21:28 -05:00
committed by Umputun
parent 53cc370727
commit 050f1b7941
29 changed files with 903 additions and 5340 deletions
+26 -25
View File
@@ -12,7 +12,6 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/umputun/remark42/backend/app/store/image"
@@ -95,7 +94,8 @@ func TestImage_Replace(t *testing.T) {
}
func TestImage_Routes(t *testing.T) {
imageStore := image.MockStore{}
// no image supposed to be cached
imageStore := image.StoreMock{LoadFunc: func(id string) ([]byte, error) { return nil, nil }}
img := Image{
HTTP2HTTPS: true,
RemarkURL: "https://demo.remark42.com",
@@ -110,9 +110,6 @@ func TestImage_Routes(t *testing.T) {
encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img1.png"))
// no image supposed to be cached
imageStore.On("Load", mock.Anything).Times(2).Return(nil, nil)
resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL)
require.NoError(t, err)
assert.NoError(t, resp.Body.Close())
@@ -131,10 +128,11 @@ func TestImage_Routes(t *testing.T) {
require.NoError(t, err)
assert.NoError(t, resp.Body.Close())
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, 2, len(imageStore.LoadCalls()))
}
func TestImage_DisabledCachingAndHTTP2HTTPS(t *testing.T) {
imageStore := image.MockStore{}
imageStore := image.StoreMock{LoadFunc: func(id string) ([]byte, error) { return nil, nil }}
img := Image{
RemarkURL: "https://demo.remark42.com",
RoutePath: "/api/v1/proxy",
@@ -148,8 +146,6 @@ func TestImage_DisabledCachingAndHTTP2HTTPS(t *testing.T) {
encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img1.png"))
imageStore.On("Load", mock.Anything).Once().Return(nil, nil)
resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL)
require.NoError(t, err)
assert.NoError(t, resp.Body.Close())
@@ -157,11 +153,18 @@ func TestImage_DisabledCachingAndHTTP2HTTPS(t *testing.T) {
assert.Equal(t, "1462", resp.Header["Content-Length"][0])
assert.Equal(t, "image/png", resp.Header["Content-Type"][0])
imageStore.AssertCalled(t, "Load", mock.Anything)
assert.Equal(t, 1, len(imageStore.LoadCalls()))
}
func TestImage_RoutesCachingImage(t *testing.T) {
imageStore := image.MockStore{}
imageStore := image.StoreMock{
LoadFunc: func(id string) ([]byte, error) {
return nil, nil
},
SaveFunc: func(id string, img []byte) error {
return nil
},
}
img := Image{
CacheExternal: true,
RemarkURL: "https://demo.remark42.com",
@@ -177,9 +180,6 @@ func TestImage_RoutesCachingImage(t *testing.T) {
imgURL := httpSrv.URL + "/image/img1.png"
encodedImgURL := base64.URLEncoding.EncodeToString([]byte(imgURL))
imageStore.On("Load", mock.Anything).Once().Return(nil, nil)
imageStore.On("Save", mock.Anything, mock.Anything).Once().Return(nil)
resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL)
require.NoError(t, err)
assert.NoError(t, resp.Body.Close())
@@ -187,12 +187,18 @@ func TestImage_RoutesCachingImage(t *testing.T) {
assert.Equal(t, "1462", resp.Header["Content-Length"][0])
assert.Equal(t, "image/png", resp.Header["Content-Type"][0])
imageStore.AssertCalled(t, "Load", mock.Anything)
imageStore.AssertCalled(t, "Save", "cached_images/4b84b15bff6ee5796152495a230e45e3d7e947d9-"+image.Sha1Str(imgURL), gopherPNGBytes())
assert.Equal(t, 1, len(imageStore.LoadCalls()))
assert.Equal(t, 1, len(imageStore.SaveCalls()))
assert.Equal(t, "cached_images/4b84b15bff6ee5796152495a230e45e3d7e947d9-"+image.Sha1Str(imgURL), imageStore.SaveCalls()[0].ID)
assert.Equal(t, gopherPNGBytes(), imageStore.SaveCalls()[0].Img)
}
func TestImage_RoutesUsingCachedImage(t *testing.T) {
imageStore := image.MockStore{}
// In order to validate that cached data used cache "will return" some other data from what http server would
testImage := []byte(fmt.Sprintf("%256s", "X"))
imageStore := image.StoreMock{LoadFunc: func(id string) ([]byte, error) {
return testImage, nil
}}
img := Image{
CacheExternal: true,
RemarkURL: "https://demo.remark42.com",
@@ -207,10 +213,6 @@ func TestImage_RoutesUsingCachedImage(t *testing.T) {
encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img1.png"))
// In order to validate that cached data used cache "will return" some other data from what http server would
testImage := []byte(fmt.Sprintf("%256s", "X"))
imageStore.On("Load", mock.Anything).Once().Return(testImage, nil)
resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL)
require.NoError(t, err)
assert.NoError(t, resp.Body.Close())
@@ -219,11 +221,12 @@ func TestImage_RoutesUsingCachedImage(t *testing.T) {
assert.Equal(t, "text/plain; charset=utf-8", resp.Header["Content-Type"][0],
"if you save text you receive text/plain in response, that's only fair option you got")
imageStore.AssertCalled(t, "Load", mock.Anything)
assert.Equal(t, 1, len(imageStore.LoadCalls()))
}
func TestImage_RoutesTimedOut(t *testing.T) {
imageStore := image.MockStore{}
// no image supposed to be cached
imageStore := image.StoreMock{LoadFunc: func(id string) ([]byte, error) { return nil, nil }}
img := Image{
HTTP2HTTPS: true,
RemarkURL: "https://demo.remark42.com",
@@ -239,9 +242,6 @@ func TestImage_RoutesTimedOut(t *testing.T) {
encodedImgURL := base64.URLEncoding.EncodeToString([]byte(httpSrv.URL + "/image/img-slow.png"))
// no image supposed to be cached
imageStore.On("Load", mock.Anything).Once().Return(nil, nil)
resp, err := http.Get(ts.URL + "/?src=" + encodedImgURL)
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
@@ -250,6 +250,7 @@ func TestImage_RoutesTimedOut(t *testing.T) {
require.NoError(t, err)
t.Log(string(b))
assert.True(t, strings.Contains(string(b), "deadline exceeded"))
assert.Equal(t, 1, len(imageStore.LoadCalls()))
}
func TestImage_ConvertProxyMode(t *testing.T) {
+2 -2
View File
@@ -11,8 +11,8 @@ import (
"github.com/umputun/remark42/backend/app/store"
)
// NOTE: mockery should be installed globally and works with `go generate ./...`
//go:generate mockery --inpackage --name Interface --filename engine_mock.go
// NOTE: matryer/moq should be installed globally and works with `go generate ./...`
//go:generate moq --out engine_mock.go . Interface
// Interface defines methods provided by low-level storage engine
type Interface interface {
+461 -199
View File
@@ -1,246 +1,508 @@
// Code generated by mockery v2.14.0. DO NOT EDIT.
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq
package engine
import (
mock "github.com/stretchr/testify/mock"
store "github.com/umputun/remark42/backend/app/store"
"sync"
)
// MockInterface is an autogenerated mock type for the Interface type
type MockInterface struct {
mock.Mock
}
// Ensure, that InterfaceMock does implement Interface.
// If this is not the case, regenerate this file with moq.
var _ Interface = &InterfaceMock{}
// Close provides a mock function with given fields:
func (_m *MockInterface) Close() error {
ret := _m.Called()
// InterfaceMock is a mock implementation of Interface.
//
// func TestSomethingThatUsesInterface(t *testing.T) {
//
// // make and configure a mocked Interface
// mockedInterface := &InterfaceMock{
// CloseFunc: func() error {
// panic("mock out the Close method")
// },
// CountFunc: func(req FindRequest) (int, error) {
// panic("mock out the Count method")
// },
// CreateFunc: func(comment store.Comment) (string, error) {
// panic("mock out the Create method")
// },
// DeleteFunc: func(req DeleteRequest) error {
// panic("mock out the Delete method")
// },
// FindFunc: func(req FindRequest) ([]store.Comment, error) {
// panic("mock out the Find method")
// },
// FlagFunc: func(req FlagRequest) (bool, error) {
// panic("mock out the Flag method")
// },
// GetFunc: func(req GetRequest) (store.Comment, error) {
// panic("mock out the Get method")
// },
// InfoFunc: func(req InfoRequest) ([]store.PostInfo, error) {
// panic("mock out the Info method")
// },
// ListFlagsFunc: func(req FlagRequest) ([]interface{}, error) {
// panic("mock out the ListFlags method")
// },
// UpdateFunc: func(comment store.Comment) error {
// panic("mock out the Update method")
// },
// UserDetailFunc: func(req UserDetailRequest) ([]UserDetailEntry, error) {
// panic("mock out the UserDetail method")
// },
// }
//
// // use mockedInterface in code that requires Interface
// // and then make assertions.
//
// }
type InterfaceMock struct {
// CloseFunc mocks the Close method.
CloseFunc func() error
var r0 error
if rf, ok := ret.Get(0).(func() error); ok {
r0 = rf()
} else {
r0 = ret.Error(0)
}
// CountFunc mocks the Count method.
CountFunc func(req FindRequest) (int, error)
return r0
}
// CreateFunc mocks the Create method.
CreateFunc func(comment store.Comment) (string, error)
// Count provides a mock function with given fields: req
func (_m *MockInterface) Count(req FindRequest) (int, error) {
ret := _m.Called(req)
// DeleteFunc mocks the Delete method.
DeleteFunc func(req DeleteRequest) error
var r0 int
if rf, ok := ret.Get(0).(func(FindRequest) int); ok {
r0 = rf(req)
} else {
r0 = ret.Get(0).(int)
}
// FindFunc mocks the Find method.
FindFunc func(req FindRequest) ([]store.Comment, error)
var r1 error
if rf, ok := ret.Get(1).(func(FindRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
}
// FlagFunc mocks the Flag method.
FlagFunc func(req FlagRequest) (bool, error)
return r0, r1
}
// GetFunc mocks the Get method.
GetFunc func(req GetRequest) (store.Comment, error)
// Create provides a mock function with given fields: comment
func (_m *MockInterface) Create(comment store.Comment) (string, error) {
ret := _m.Called(comment)
// InfoFunc mocks the Info method.
InfoFunc func(req InfoRequest) ([]store.PostInfo, error)
var r0 string
if rf, ok := ret.Get(0).(func(store.Comment) string); ok {
r0 = rf(comment)
} else {
r0 = ret.Get(0).(string)
}
// ListFlagsFunc mocks the ListFlags method.
ListFlagsFunc func(req FlagRequest) ([]interface{}, error)
var r1 error
if rf, ok := ret.Get(1).(func(store.Comment) error); ok {
r1 = rf(comment)
} else {
r1 = ret.Error(1)
}
// UpdateFunc mocks the Update method.
UpdateFunc func(comment store.Comment) error
return r0, r1
}
// UserDetailFunc mocks the UserDetail method.
UserDetailFunc func(req UserDetailRequest) ([]UserDetailEntry, error)
// Delete provides a mock function with given fields: req
func (_m *MockInterface) Delete(req DeleteRequest) error {
ret := _m.Called(req)
var r0 error
if rf, ok := ret.Get(0).(func(DeleteRequest) error); ok {
r0 = rf(req)
} else {
r0 = ret.Error(0)
}
return r0
}
// Find provides a mock function with given fields: req
func (_m *MockInterface) Find(req FindRequest) ([]store.Comment, error) {
ret := _m.Called(req)
var r0 []store.Comment
if rf, ok := ret.Get(0).(func(FindRequest) []store.Comment); ok {
r0 = rf(req)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]store.Comment)
// calls tracks calls to the methods.
calls struct {
// Close holds details about calls to the Close method.
Close []struct {
}
// Count holds details about calls to the Count method.
Count []struct {
// Req is the req argument value.
Req FindRequest
}
// Create holds details about calls to the Create method.
Create []struct {
// Comment is the comment argument value.
Comment store.Comment
}
// Delete holds details about calls to the Delete method.
Delete []struct {
// Req is the req argument value.
Req DeleteRequest
}
// Find holds details about calls to the Find method.
Find []struct {
// Req is the req argument value.
Req FindRequest
}
// Flag holds details about calls to the Flag method.
Flag []struct {
// Req is the req argument value.
Req FlagRequest
}
// Get holds details about calls to the Get method.
Get []struct {
// Req is the req argument value.
Req GetRequest
}
// Info holds details about calls to the Info method.
Info []struct {
// Req is the req argument value.
Req InfoRequest
}
// ListFlags holds details about calls to the ListFlags method.
ListFlags []struct {
// Req is the req argument value.
Req FlagRequest
}
// Update holds details about calls to the Update method.
Update []struct {
// Comment is the comment argument value.
Comment store.Comment
}
// UserDetail holds details about calls to the UserDetail method.
UserDetail []struct {
// Req is the req argument value.
Req UserDetailRequest
}
}
var r1 error
if rf, ok := ret.Get(1).(func(FindRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
}
return r0, r1
lockClose sync.RWMutex
lockCount sync.RWMutex
lockCreate sync.RWMutex
lockDelete sync.RWMutex
lockFind sync.RWMutex
lockFlag sync.RWMutex
lockGet sync.RWMutex
lockInfo sync.RWMutex
lockListFlags sync.RWMutex
lockUpdate sync.RWMutex
lockUserDetail sync.RWMutex
}
// Flag provides a mock function with given fields: req
func (_m *MockInterface) Flag(req FlagRequest) (bool, error) {
ret := _m.Called(req)
var r0 bool
if rf, ok := ret.Get(0).(func(FlagRequest) bool); ok {
r0 = rf(req)
} else {
r0 = ret.Get(0).(bool)
// Close calls CloseFunc.
func (mock *InterfaceMock) Close() error {
if mock.CloseFunc == nil {
panic("InterfaceMock.CloseFunc: method is nil but Interface.Close was just called")
}
var r1 error
if rf, ok := ret.Get(1).(func(FlagRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
}
return r0, r1
callInfo := struct {
}{}
mock.lockClose.Lock()
mock.calls.Close = append(mock.calls.Close, callInfo)
mock.lockClose.Unlock()
return mock.CloseFunc()
}
// Get provides a mock function with given fields: req
func (_m *MockInterface) Get(req GetRequest) (store.Comment, error) {
ret := _m.Called(req)
var r0 store.Comment
if rf, ok := ret.Get(0).(func(GetRequest) store.Comment); ok {
r0 = rf(req)
} else {
r0 = ret.Get(0).(store.Comment)
// CloseCalls gets all the calls that were made to Close.
// Check the length with:
//
// len(mockedInterface.CloseCalls())
func (mock *InterfaceMock) CloseCalls() []struct {
} {
var calls []struct {
}
var r1 error
if rf, ok := ret.Get(1).(func(GetRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
}
return r0, r1
mock.lockClose.RLock()
calls = mock.calls.Close
mock.lockClose.RUnlock()
return calls
}
// Info provides a mock function with given fields: req
func (_m *MockInterface) Info(req InfoRequest) ([]store.PostInfo, error) {
ret := _m.Called(req)
var r0 []store.PostInfo
if rf, ok := ret.Get(0).(func(InfoRequest) []store.PostInfo); ok {
r0 = rf(req)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]store.PostInfo)
}
// Count calls CountFunc.
func (mock *InterfaceMock) Count(req FindRequest) (int, error) {
if mock.CountFunc == nil {
panic("InterfaceMock.CountFunc: method is nil but Interface.Count was just called")
}
var r1 error
if rf, ok := ret.Get(1).(func(InfoRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
callInfo := struct {
Req FindRequest
}{
Req: req,
}
return r0, r1
mock.lockCount.Lock()
mock.calls.Count = append(mock.calls.Count, callInfo)
mock.lockCount.Unlock()
return mock.CountFunc(req)
}
// ListFlags provides a mock function with given fields: req
func (_m *MockInterface) ListFlags(req FlagRequest) ([]interface{}, error) {
ret := _m.Called(req)
var r0 []interface{}
if rf, ok := ret.Get(0).(func(FlagRequest) []interface{}); ok {
r0 = rf(req)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]interface{})
}
// CountCalls gets all the calls that were made to Count.
// Check the length with:
//
// len(mockedInterface.CountCalls())
func (mock *InterfaceMock) CountCalls() []struct {
Req FindRequest
} {
var calls []struct {
Req FindRequest
}
var r1 error
if rf, ok := ret.Get(1).(func(FlagRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
}
return r0, r1
mock.lockCount.RLock()
calls = mock.calls.Count
mock.lockCount.RUnlock()
return calls
}
// Update provides a mock function with given fields: comment
func (_m *MockInterface) Update(comment store.Comment) error {
ret := _m.Called(comment)
var r0 error
if rf, ok := ret.Get(0).(func(store.Comment) error); ok {
r0 = rf(comment)
} else {
r0 = ret.Error(0)
// Create calls CreateFunc.
func (mock *InterfaceMock) Create(comment store.Comment) (string, error) {
if mock.CreateFunc == nil {
panic("InterfaceMock.CreateFunc: method is nil but Interface.Create was just called")
}
return r0
}
// UserDetail provides a mock function with given fields: req
func (_m *MockInterface) UserDetail(req UserDetailRequest) ([]UserDetailEntry, error) {
ret := _m.Called(req)
var r0 []UserDetailEntry
if rf, ok := ret.Get(0).(func(UserDetailRequest) []UserDetailEntry); ok {
r0 = rf(req)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]UserDetailEntry)
}
callInfo := struct {
Comment store.Comment
}{
Comment: comment,
}
mock.lockCreate.Lock()
mock.calls.Create = append(mock.calls.Create, callInfo)
mock.lockCreate.Unlock()
return mock.CreateFunc(comment)
}
var r1 error
if rf, ok := ret.Get(1).(func(UserDetailRequest) error); ok {
r1 = rf(req)
} else {
r1 = ret.Error(1)
// CreateCalls gets all the calls that were made to Create.
// Check the length with:
//
// len(mockedInterface.CreateCalls())
func (mock *InterfaceMock) CreateCalls() []struct {
Comment store.Comment
} {
var calls []struct {
Comment store.Comment
}
return r0, r1
mock.lockCreate.RLock()
calls = mock.calls.Create
mock.lockCreate.RUnlock()
return calls
}
type mockConstructorTestingTNewMockInterface interface {
mock.TestingT
Cleanup(func())
// Delete calls DeleteFunc.
func (mock *InterfaceMock) Delete(req DeleteRequest) error {
if mock.DeleteFunc == nil {
panic("InterfaceMock.DeleteFunc: method is nil but Interface.Delete was just called")
}
callInfo := struct {
Req DeleteRequest
}{
Req: req,
}
mock.lockDelete.Lock()
mock.calls.Delete = append(mock.calls.Delete, callInfo)
mock.lockDelete.Unlock()
return mock.DeleteFunc(req)
}
// NewMockInterface creates a new instance of MockInterface. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockInterface(t mockConstructorTestingTNewMockInterface) *MockInterface {
mock := &MockInterface{}
mock.Mock.Test(t)
// DeleteCalls gets all the calls that were made to Delete.
// Check the length with:
//
// len(mockedInterface.DeleteCalls())
func (mock *InterfaceMock) DeleteCalls() []struct {
Req DeleteRequest
} {
var calls []struct {
Req DeleteRequest
}
mock.lockDelete.RLock()
calls = mock.calls.Delete
mock.lockDelete.RUnlock()
return calls
}
t.Cleanup(func() { mock.AssertExpectations(t) })
// Find calls FindFunc.
func (mock *InterfaceMock) Find(req FindRequest) ([]store.Comment, error) {
if mock.FindFunc == nil {
panic("InterfaceMock.FindFunc: method is nil but Interface.Find was just called")
}
callInfo := struct {
Req FindRequest
}{
Req: req,
}
mock.lockFind.Lock()
mock.calls.Find = append(mock.calls.Find, callInfo)
mock.lockFind.Unlock()
return mock.FindFunc(req)
}
return mock
// FindCalls gets all the calls that were made to Find.
// Check the length with:
//
// len(mockedInterface.FindCalls())
func (mock *InterfaceMock) FindCalls() []struct {
Req FindRequest
} {
var calls []struct {
Req FindRequest
}
mock.lockFind.RLock()
calls = mock.calls.Find
mock.lockFind.RUnlock()
return calls
}
// Flag calls FlagFunc.
func (mock *InterfaceMock) Flag(req FlagRequest) (bool, error) {
if mock.FlagFunc == nil {
panic("InterfaceMock.FlagFunc: method is nil but Interface.Flag was just called")
}
callInfo := struct {
Req FlagRequest
}{
Req: req,
}
mock.lockFlag.Lock()
mock.calls.Flag = append(mock.calls.Flag, callInfo)
mock.lockFlag.Unlock()
return mock.FlagFunc(req)
}
// FlagCalls gets all the calls that were made to Flag.
// Check the length with:
//
// len(mockedInterface.FlagCalls())
func (mock *InterfaceMock) FlagCalls() []struct {
Req FlagRequest
} {
var calls []struct {
Req FlagRequest
}
mock.lockFlag.RLock()
calls = mock.calls.Flag
mock.lockFlag.RUnlock()
return calls
}
// Get calls GetFunc.
func (mock *InterfaceMock) Get(req GetRequest) (store.Comment, error) {
if mock.GetFunc == nil {
panic("InterfaceMock.GetFunc: method is nil but Interface.Get was just called")
}
callInfo := struct {
Req GetRequest
}{
Req: req,
}
mock.lockGet.Lock()
mock.calls.Get = append(mock.calls.Get, callInfo)
mock.lockGet.Unlock()
return mock.GetFunc(req)
}
// GetCalls gets all the calls that were made to Get.
// Check the length with:
//
// len(mockedInterface.GetCalls())
func (mock *InterfaceMock) GetCalls() []struct {
Req GetRequest
} {
var calls []struct {
Req GetRequest
}
mock.lockGet.RLock()
calls = mock.calls.Get
mock.lockGet.RUnlock()
return calls
}
// Info calls InfoFunc.
func (mock *InterfaceMock) Info(req InfoRequest) ([]store.PostInfo, error) {
if mock.InfoFunc == nil {
panic("InterfaceMock.InfoFunc: method is nil but Interface.Info was just called")
}
callInfo := struct {
Req InfoRequest
}{
Req: req,
}
mock.lockInfo.Lock()
mock.calls.Info = append(mock.calls.Info, callInfo)
mock.lockInfo.Unlock()
return mock.InfoFunc(req)
}
// InfoCalls gets all the calls that were made to Info.
// Check the length with:
//
// len(mockedInterface.InfoCalls())
func (mock *InterfaceMock) InfoCalls() []struct {
Req InfoRequest
} {
var calls []struct {
Req InfoRequest
}
mock.lockInfo.RLock()
calls = mock.calls.Info
mock.lockInfo.RUnlock()
return calls
}
// ListFlags calls ListFlagsFunc.
func (mock *InterfaceMock) ListFlags(req FlagRequest) ([]interface{}, error) {
if mock.ListFlagsFunc == nil {
panic("InterfaceMock.ListFlagsFunc: method is nil but Interface.ListFlags was just called")
}
callInfo := struct {
Req FlagRequest
}{
Req: req,
}
mock.lockListFlags.Lock()
mock.calls.ListFlags = append(mock.calls.ListFlags, callInfo)
mock.lockListFlags.Unlock()
return mock.ListFlagsFunc(req)
}
// ListFlagsCalls gets all the calls that were made to ListFlags.
// Check the length with:
//
// len(mockedInterface.ListFlagsCalls())
func (mock *InterfaceMock) ListFlagsCalls() []struct {
Req FlagRequest
} {
var calls []struct {
Req FlagRequest
}
mock.lockListFlags.RLock()
calls = mock.calls.ListFlags
mock.lockListFlags.RUnlock()
return calls
}
// Update calls UpdateFunc.
func (mock *InterfaceMock) Update(comment store.Comment) error {
if mock.UpdateFunc == nil {
panic("InterfaceMock.UpdateFunc: method is nil but Interface.Update was just called")
}
callInfo := struct {
Comment store.Comment
}{
Comment: comment,
}
mock.lockUpdate.Lock()
mock.calls.Update = append(mock.calls.Update, callInfo)
mock.lockUpdate.Unlock()
return mock.UpdateFunc(comment)
}
// UpdateCalls gets all the calls that were made to Update.
// Check the length with:
//
// len(mockedInterface.UpdateCalls())
func (mock *InterfaceMock) UpdateCalls() []struct {
Comment store.Comment
} {
var calls []struct {
Comment store.Comment
}
mock.lockUpdate.RLock()
calls = mock.calls.Update
mock.lockUpdate.RUnlock()
return calls
}
// UserDetail calls UserDetailFunc.
func (mock *InterfaceMock) UserDetail(req UserDetailRequest) ([]UserDetailEntry, error) {
if mock.UserDetailFunc == nil {
panic("InterfaceMock.UserDetailFunc: method is nil but Interface.UserDetail was just called")
}
callInfo := struct {
Req UserDetailRequest
}{
Req: req,
}
mock.lockUserDetail.Lock()
mock.calls.UserDetail = append(mock.calls.UserDetail, callInfo)
mock.lockUserDetail.Unlock()
return mock.UserDetailFunc(req)
}
// UserDetailCalls gets all the calls that were made to UserDetail.
// Check the length with:
//
// len(mockedInterface.UserDetailCalls())
func (mock *InterfaceMock) UserDetailCalls() []struct {
Req UserDetailRequest
} {
var calls []struct {
Req UserDetailRequest
}
mock.lockUserDetail.RLock()
calls = mock.calls.UserDetail
mock.lockUserDetail.RUnlock()
return calls
}
+2 -2
View File
@@ -3,8 +3,8 @@
// Service object encloses Store and add common methods, this is the one consumer should use.
package image
// NOTE: mockery should be installed globally and works with `go generate ./...`
//go:generate mockery --inpackage --name Store --filename image_mock.go
// NOTE: matryer/moq should be installed globally and works with `go generate ./...`
//go:generate moq --out image_mock.go . Store
import (
"bytes"
+271 -100
View File
@@ -1,130 +1,301 @@
// Code generated by mockery v2.14.0. DO NOT EDIT.
// Code generated by moq; DO NOT EDIT.
// github.com/matryer/moq
package image
import (
context "context"
"sync"
time "time"
mock "github.com/stretchr/testify/mock"
)
// MockStore is an autogenerated mock type for the Store type
type MockStore struct {
mock.Mock
}
// Ensure, that StoreMock does implement Store.
// If this is not the case, regenerate this file with moq.
var _ Store = &StoreMock{}
// Cleanup provides a mock function with given fields: ctx, ttl
func (_m *MockStore) Cleanup(ctx context.Context, ttl time.Duration) error {
ret := _m.Called(ctx, ttl)
// StoreMock is a mock implementation of Store.
//
// func TestSomethingThatUsesStore(t *testing.T) {
//
// // make and configure a mocked Store
// mockedStore := &StoreMock{
// CleanupFunc: func(ctx context.Context, ttl time.Duration) error {
// panic("mock out the Cleanup method")
// },
// CommitFunc: func(id string) error {
// panic("mock out the Commit method")
// },
// InfoFunc: func() (StoreInfo, error) {
// panic("mock out the Info method")
// },
// LoadFunc: func(id string) ([]byte, error) {
// panic("mock out the Load method")
// },
// ResetCleanupTimerFunc: func(id string) error {
// panic("mock out the ResetCleanupTimer method")
// },
// SaveFunc: func(id string, img []byte) error {
// panic("mock out the Save method")
// },
// }
//
// // use mockedStore in code that requires Store
// // and then make assertions.
//
// }
type StoreMock struct {
// CleanupFunc mocks the Cleanup method.
CleanupFunc func(ctx context.Context, ttl time.Duration) error
var r0 error
if rf, ok := ret.Get(0).(func(context.Context, time.Duration) error); ok {
r0 = rf(ctx, ttl)
} else {
r0 = ret.Error(0)
}
// CommitFunc mocks the Commit method.
CommitFunc func(id string) error
return r0
}
// InfoFunc mocks the Info method.
InfoFunc func() (StoreInfo, error)
// Commit provides a mock function with given fields: id
func (_m *MockStore) Commit(id string) error {
ret := _m.Called(id)
// LoadFunc mocks the Load method.
LoadFunc func(id string) ([]byte, error)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(id)
} else {
r0 = ret.Error(0)
}
// ResetCleanupTimerFunc mocks the ResetCleanupTimer method.
ResetCleanupTimerFunc func(id string) error
return r0
}
// SaveFunc mocks the Save method.
SaveFunc func(id string, img []byte) error
// Info provides a mock function with given fields:
func (_m *MockStore) Info() (StoreInfo, error) {
ret := _m.Called()
var r0 StoreInfo
if rf, ok := ret.Get(0).(func() StoreInfo); ok {
r0 = rf()
} else {
r0 = ret.Get(0).(StoreInfo)
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Load provides a mock function with given fields: id
func (_m *MockStore) Load(id string) ([]byte, error) {
ret := _m.Called(id)
var r0 []byte
if rf, ok := ret.Get(0).(func(string) []byte); ok {
r0 = rf(id)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]byte)
// calls tracks calls to the methods.
calls struct {
// Cleanup holds details about calls to the Cleanup method.
Cleanup []struct {
// Ctx is the ctx argument value.
Ctx context.Context
// TTL is the ttl argument value.
TTL time.Duration
}
// Commit holds details about calls to the Commit method.
Commit []struct {
// ID is the id argument value.
ID string
}
// Info holds details about calls to the Info method.
Info []struct {
}
// Load holds details about calls to the Load method.
Load []struct {
// ID is the id argument value.
ID string
}
// ResetCleanupTimer holds details about calls to the ResetCleanupTimer method.
ResetCleanupTimer []struct {
// ID is the id argument value.
ID string
}
// Save holds details about calls to the Save method.
Save []struct {
// ID is the id argument value.
ID string
// Img is the img argument value.
Img []byte
}
}
lockCleanup sync.RWMutex
lockCommit sync.RWMutex
lockInfo sync.RWMutex
lockLoad sync.RWMutex
lockResetCleanupTimer sync.RWMutex
lockSave sync.RWMutex
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(id)
} else {
r1 = ret.Error(1)
// Cleanup calls CleanupFunc.
func (mock *StoreMock) Cleanup(ctx context.Context, ttl time.Duration) error {
if mock.CleanupFunc == nil {
panic("StoreMock.CleanupFunc: method is nil but Store.Cleanup was just called")
}
return r0, r1
}
// ResetCleanupTimer provides a mock function with given fields: id
func (_m *MockStore) ResetCleanupTimer(id string) error {
ret := _m.Called(id)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(id)
} else {
r0 = ret.Error(0)
callInfo := struct {
Ctx context.Context
TTL time.Duration
}{
Ctx: ctx,
TTL: ttl,
}
return r0
mock.lockCleanup.Lock()
mock.calls.Cleanup = append(mock.calls.Cleanup, callInfo)
mock.lockCleanup.Unlock()
return mock.CleanupFunc(ctx, ttl)
}
// Save provides a mock function with given fields: id, img
func (_m *MockStore) Save(id string, img []byte) error {
ret := _m.Called(id, img)
var r0 error
if rf, ok := ret.Get(0).(func(string, []byte) error); ok {
r0 = rf(id, img)
} else {
r0 = ret.Error(0)
// CleanupCalls gets all the calls that were made to Cleanup.
// Check the length with:
//
// len(mockedStore.CleanupCalls())
func (mock *StoreMock) CleanupCalls() []struct {
Ctx context.Context
TTL time.Duration
} {
var calls []struct {
Ctx context.Context
TTL time.Duration
}
return r0
mock.lockCleanup.RLock()
calls = mock.calls.Cleanup
mock.lockCleanup.RUnlock()
return calls
}
type mockConstructorTestingTNewMockStore interface {
mock.TestingT
Cleanup(func())
// Commit calls CommitFunc.
func (mock *StoreMock) Commit(id string) error {
if mock.CommitFunc == nil {
panic("StoreMock.CommitFunc: method is nil but Store.Commit was just called")
}
callInfo := struct {
ID string
}{
ID: id,
}
mock.lockCommit.Lock()
mock.calls.Commit = append(mock.calls.Commit, callInfo)
mock.lockCommit.Unlock()
return mock.CommitFunc(id)
}
// NewMockStore creates a new instance of MockStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockStore(t mockConstructorTestingTNewMockStore) *MockStore {
mock := &MockStore{}
mock.Mock.Test(t)
// CommitCalls gets all the calls that were made to Commit.
// Check the length with:
//
// len(mockedStore.CommitCalls())
func (mock *StoreMock) CommitCalls() []struct {
ID string
} {
var calls []struct {
ID string
}
mock.lockCommit.RLock()
calls = mock.calls.Commit
mock.lockCommit.RUnlock()
return calls
}
t.Cleanup(func() { mock.AssertExpectations(t) })
// Info calls InfoFunc.
func (mock *StoreMock) Info() (StoreInfo, error) {
if mock.InfoFunc == nil {
panic("StoreMock.InfoFunc: method is nil but Store.Info was just called")
}
callInfo := struct {
}{}
mock.lockInfo.Lock()
mock.calls.Info = append(mock.calls.Info, callInfo)
mock.lockInfo.Unlock()
return mock.InfoFunc()
}
return mock
// InfoCalls gets all the calls that were made to Info.
// Check the length with:
//
// len(mockedStore.InfoCalls())
func (mock *StoreMock) InfoCalls() []struct {
} {
var calls []struct {
}
mock.lockInfo.RLock()
calls = mock.calls.Info
mock.lockInfo.RUnlock()
return calls
}
// Load calls LoadFunc.
func (mock *StoreMock) Load(id string) ([]byte, error) {
if mock.LoadFunc == nil {
panic("StoreMock.LoadFunc: method is nil but Store.Load was just called")
}
callInfo := struct {
ID string
}{
ID: id,
}
mock.lockLoad.Lock()
mock.calls.Load = append(mock.calls.Load, callInfo)
mock.lockLoad.Unlock()
return mock.LoadFunc(id)
}
// LoadCalls gets all the calls that were made to Load.
// Check the length with:
//
// len(mockedStore.LoadCalls())
func (mock *StoreMock) LoadCalls() []struct {
ID string
} {
var calls []struct {
ID string
}
mock.lockLoad.RLock()
calls = mock.calls.Load
mock.lockLoad.RUnlock()
return calls
}
// ResetCleanupTimer calls ResetCleanupTimerFunc.
func (mock *StoreMock) ResetCleanupTimer(id string) error {
if mock.ResetCleanupTimerFunc == nil {
panic("StoreMock.ResetCleanupTimerFunc: method is nil but Store.ResetCleanupTimer was just called")
}
callInfo := struct {
ID string
}{
ID: id,
}
mock.lockResetCleanupTimer.Lock()
mock.calls.ResetCleanupTimer = append(mock.calls.ResetCleanupTimer, callInfo)
mock.lockResetCleanupTimer.Unlock()
return mock.ResetCleanupTimerFunc(id)
}
// ResetCleanupTimerCalls gets all the calls that were made to ResetCleanupTimer.
// Check the length with:
//
// len(mockedStore.ResetCleanupTimerCalls())
func (mock *StoreMock) ResetCleanupTimerCalls() []struct {
ID string
} {
var calls []struct {
ID string
}
mock.lockResetCleanupTimer.RLock()
calls = mock.calls.ResetCleanupTimer
mock.lockResetCleanupTimer.RUnlock()
return calls
}
// Save calls SaveFunc.
func (mock *StoreMock) Save(id string, img []byte) error {
if mock.SaveFunc == nil {
panic("StoreMock.SaveFunc: method is nil but Store.Save was just called")
}
callInfo := struct {
ID string
Img []byte
}{
ID: id,
Img: img,
}
mock.lockSave.Lock()
mock.calls.Save = append(mock.calls.Save, callInfo)
mock.lockSave.Unlock()
return mock.SaveFunc(id, img)
}
// SaveCalls gets all the calls that were made to Save.
// Check the length with:
//
// len(mockedStore.SaveCalls())
func (mock *StoreMock) SaveCalls() []struct {
ID string
Img []byte
} {
var calls []struct {
ID string
Img []byte
}
mock.lockSave.RLock()
calls = mock.calls.Save
mock.lockSave.RUnlock()
return calls
}
+55 -28
View File
@@ -14,22 +14,30 @@ import (
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestService_SaveAndLoad(t *testing.T) {
store := MockStore{}
store := StoreMock{
SaveFunc: func(id string, img []byte) error {
return nil
},
LoadFunc: func(id string) ([]byte, error) {
return nil, nil
},
}
svc := NewService(&store, ServiceParams{MaxSize: 1500, MaxWidth: 32, MaxHeight: 32})
store.On("Save", "test_id", mock.Anything).Return(nil)
err := svc.SaveWithID("test_id", gopherPNG())
assert.NoError(t, err)
assert.Equal(t, 1, len(store.SaveCalls()))
assert.Equal(t, "test_id", store.SaveCalls()[0].ID)
store.On("Load", "test_id", mock.Anything).Return(nil, nil)
img, err := svc.Load("test_id")
assert.NoError(t, err)
assert.Nil(t, img)
assert.Equal(t, 1, len(store.LoadCalls()))
assert.Equal(t, "test_id", store.LoadCalls()[0].ID)
}
func TestService_Resize(t *testing.T) {
@@ -116,72 +124,91 @@ func TestService_ExtractPictures(t *testing.T) {
}
func TestService_Cleanup(t *testing.T) {
store := MockStore{}
store.On("Cleanup", mock.Anything, mock.Anything).Times(10).Return(nil)
store := StoreMock{
CleanupFunc: func(ctx context.Context, ttl time.Duration) error {
return nil
},
}
svc := NewService(&store, ServiceParams{EditDuration: 20 * time.Millisecond})
// cancel context after 2.1 cleanup TTLs
ctx, cancel := context.WithTimeout(context.Background(), svc.EditDuration/100*15*21)
defer cancel()
svc.Cleanup(ctx)
store.AssertNumberOfCalls(t, "Cleanup", 2)
assert.Equal(t, 2, len(store.CleanupCalls()))
}
func TestService_Submit(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(7).Return(nil)
store.On("ResetCleanupTimer", mock.Anything, mock.Anything).Times(7).Return(nil)
store := StoreMock{
CommitFunc: func(id string) error {
return nil
},
ResetCleanupTimerFunc: func(id string) error {
return nil
},
}
svc := NewService(&store, ServiceParams{ImageAPI: "/blah/", EditDuration: time.Millisecond * 100})
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
store.AssertNumberOfCalls(t, "ResetCleanupTimer", 3)
assert.Equal(t, 3, len(store.ResetCleanupTimerCalls()))
err := svc.SubmitAndCommit(func() []string { return []string{"id4", "id5"} })
assert.NoError(t, err)
svc.Submit(func() []string { return []string{"id6", "id7"} })
store.AssertNumberOfCalls(t, "ResetCleanupTimer", 5)
assert.Equal(t, 5, len(store.ResetCleanupTimerCalls()))
svc.Submit(nil)
store.AssertNumberOfCalls(t, "Commit", 2)
assert.Equal(t, 2, len(store.CommitCalls()))
time.Sleep(time.Millisecond * 175)
store.AssertNumberOfCalls(t, "Commit", 7)
assert.Equal(t, 7, len(store.CommitCalls()))
svc.Close(context.TODO())
}
func TestService_Close(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(5).Return(nil)
store.On("ResetCleanupTimer", mock.Anything, mock.Anything).Times(5).Return(nil)
store := StoreMock{
CommitFunc: func(id string) error {
return nil
},
ResetCleanupTimerFunc: func(id string) error {
return nil
},
}
svc := Service{store: &store, ServiceParams: ServiceParams{ImageAPI: "/blah/", EditDuration: time.Hour * 24}}
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
svc.Submit(func() []string { return []string{"id4", "id5"} })
svc.Submit(nil)
store.AssertNumberOfCalls(t, "ResetCleanupTimer", 5)
assert.Equal(t, 5, len(store.ResetCleanupTimerCalls()))
svc.Close(context.TODO())
store.AssertNumberOfCalls(t, "Commit", 5)
assert.Equal(t, 5, len(store.CommitCalls()))
}
func TestService_SubmitDelay(t *testing.T) {
store := MockStore{}
store.On("Commit", mock.Anything, mock.Anything).Times(5).Return(nil)
store.On("ResetCleanupTimer", mock.Anything, mock.Anything).Times(5).Return(nil)
store := StoreMock{
CommitFunc: func(id string) error {
return nil
},
ResetCleanupTimerFunc: func(id string) error {
return nil
},
}
svc := NewService(&store, ServiceParams{EditDuration: 20 * time.Millisecond})
svc.Submit(func() []string { return []string{"id1", "id2", "id3"} })
time.Sleep(150 * time.Millisecond) // let first batch to pass TTL
svc.Submit(func() []string { return []string{"id4", "id5"} })
svc.Submit(nil)
store.AssertNumberOfCalls(t, "ResetCleanupTimer", 5)
store.AssertNumberOfCalls(t, "Commit", 3)
assert.Equal(t, 5, len(store.ResetCleanupTimerCalls()))
assert.Equal(t, 3, len(store.CommitCalls()))
svc.Close(context.TODO())
store.AssertNumberOfCalls(t, "Commit", 5)
assert.Equal(t, 5, len(store.CommitCalls()))
}
func TestService_Info(t *testing.T) {
store := MockStore{}
store.On("Info", mock.Anything, mock.Anything).Once().Return(StoreInfo{}, nil)
store := StoreMock{InfoFunc: func() (StoreInfo, error) {
return StoreInfo{}, nil
}}
svc := Service{store: &store, ServiceParams: ServiceParams{}}
info, err := svc.Info()
assert.NoError(t, err)
assert.True(t, info.FirstStagingImageTS.IsZero())
store.AssertNumberOfCalls(t, "Info", 1)
assert.Equal(t, 1, len(store.InfoCalls()))
}
func TestService_resize(t *testing.T) {
+86 -40
View File
@@ -1358,11 +1358,10 @@ func TestService_DeleteAll(t *testing.T) {
func TestService_submitImages(t *testing.T) {
lgr.Setup(lgr.Debug, lgr.CallerFile, lgr.CallerFunc)
mockStore := image.MockStore{}
mockStore.On("Commit", "dev/pic1.png").Once().Return(nil)
mockStore.On("Commit", "dev/pic2.png").Once().Return(nil)
mockStore.On("ResetCleanupTimer", "dev/pic1.png").Once().Return(nil)
mockStore.On("ResetCleanupTimer", "dev/pic2.png").Once().Return(nil)
mockStore := image.StoreMock{
CommitFunc: func(id string) error { return nil },
ResetCleanupTimerFunc: func(id string) error { return nil },
}
imgSvc := image.NewService(&mockStore,
image.ServiceParams{
EditDuration: 50 * time.Millisecond,
@@ -1388,13 +1387,25 @@ func TestService_submitImages(t *testing.T) {
assert.NoError(t, err)
b.submitImages(c)
mockStore.AssertNumberOfCalls(t, "ResetCleanupTimer", 2)
assert.Equal(t, 2, len(mockStore.ResetCleanupTimerCalls()))
assert.Equal(t, "dev/pic1.png", mockStore.ResetCleanupTimerCalls()[0].ID)
assert.Equal(t, "dev/pic2.png", mockStore.ResetCleanupTimerCalls()[1].ID)
time.Sleep(b.EditDuration + 100*time.Millisecond)
mockStore.AssertNumberOfCalls(t, "Commit", 2)
assert.Equal(t, 2, len(mockStore.CommitCalls()))
assert.Equal(t, "dev/pic1.png", mockStore.CommitCalls()[0].ID)
assert.Equal(t, "dev/pic2.png", mockStore.CommitCalls()[1].ID)
}
func TestService_ResubmitStagingImages(t *testing.T) {
mockStore := image.MockStore{}
mockStore := image.StoreMock{
InfoFunc: func() (image.StoreInfo, error) {
return image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil
},
CommitFunc: func(id string) error {
return nil
},
ResetCleanupTimerFunc: func(id string) error { return nil },
}
imgSvc := image.NewService(&mockStore,
image.ServiceParams{
EditDuration: 10 * time.Millisecond,
@@ -1407,10 +1418,6 @@ func TestService_ResubmitStagingImages(t *testing.T) {
defer teardown()
b := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvc}
mockStore.On("ResetCleanupTimer", "dev_user/bqf122eq9r8ad657n3ng").Once().Return(nil)
mockStore.On("ResetCleanupTimer", "dev_user/bqf321eq9r8ad657n3ng").Once().Return(nil)
mockStore.On("ResetCleanupTimer", "cached_images/12318fbd4c55e9d177b8b5ae197bc89c5afd8e07-a41fcb00643f28d700504256ec81cbf2e1aac53e").Once().Return(nil)
// create comment with three images without preparing it properly
comment := store.Comment{
ID: "id-0",
@@ -1426,21 +1433,19 @@ func TestService_ResubmitStagingImages(t *testing.T) {
require.NoError(t, err)
// resubmit single comment with three images, of which two are in staging storage
mockStore.On("Info").Once().Return(image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil)
err = b.ResubmitStagingImages([]string{"radio-t"})
assert.NoError(t, err)
// wait for Submit goroutine to commit image
mockStore.On("Commit", "dev_user/bqf122eq9r8ad657n3ng").Once().Return(nil)
mockStore.On("Commit", "dev_user/bqf321eq9r8ad657n3ng").Once().Return(nil)
mockStore.On("Commit", "cached_images/12318fbd4c55e9d177b8b5ae197bc89c5afd8e07-a41fcb00643f28d700504256ec81cbf2e1aac53e").Once().Return(nil)
time.Sleep(b.EditDuration + time.Millisecond*100)
mockStore.AssertNumberOfCalls(t, "Info", 1)
mockStore.AssertNumberOfCalls(t, "Commit", 3)
assert.Equal(t, 1, len(mockStore.InfoCalls()))
assert.Equal(t, 3, len(mockStore.CommitCalls()))
// empty answer
mockStoreEmpty := image.MockStore{}
mockStoreEmpty := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
return image.StoreInfo{FirstStagingImageTS: time.Time{}}, nil
}}
imgSvcEmpty := image.NewService(&mockStoreEmpty,
image.ServiceParams{
EditDuration: 10 * time.Millisecond,
@@ -1449,15 +1454,16 @@ func TestService_ResubmitStagingImages(t *testing.T) {
defer imgSvcEmpty.Close(context.TODO())
bEmpty := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvcEmpty}
// resubmit receive empty timestamp and should do nothing
mockStoreEmpty.On("Info").Once().Return(image.StoreInfo{FirstStagingImageTS: time.Time{}}, nil)
// resubmit receive empty timestamp and should do nothing )
err = bEmpty.ResubmitStagingImages([]string{"radio-t", "non_existent"})
assert.NoError(t, err)
mockStoreEmpty.AssertNumberOfCalls(t, "Info", 1)
assert.Equal(t, 1, len(mockStore.InfoCalls()))
// error from image storage
mockStoreError := image.MockStore{}
mockStoreError := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
return image.StoreInfo{}, fmt.Errorf("mock_err")
}}
imgSvcError := image.NewService(&mockStoreError,
image.ServiceParams{
EditDuration: 10 * time.Millisecond,
@@ -1467,15 +1473,20 @@ func TestService_ResubmitStagingImages(t *testing.T) {
bError := DataStore{Engine: eng, EditDuration: 10 * time.Millisecond, ImageService: imgSvcError}
// resubmit will receive error from image storage and should return it
mockStoreError.On("Info").Once().Return(image.StoreInfo{}, fmt.Errorf("mock_err"))
err = bError.ResubmitStagingImages([]string{"radio-t"})
assert.EqualError(t, err, "mock_err")
mockStoreError.AssertNumberOfCalls(t, "Info", 1)
assert.Equal(t, 1, len(mockStore.InfoCalls()))
assert.Equal(t, 3, len(mockStore.ResetCleanupTimerCalls()))
assert.Equal(t, "dev_user/bqf122eq9r8ad657n3ng", mockStore.ResetCleanupTimerCalls()[0].ID)
assert.Equal(t, "dev_user/bqf321eq9r8ad657n3ng", mockStore.ResetCleanupTimerCalls()[1].ID)
assert.Equal(t, "cached_images/12318fbd4c55e9d177b8b5ae197bc89c5afd8e07-a41fcb00643f28d700504256ec81cbf2e1aac53e", mockStore.ResetCleanupTimerCalls()[2].ID)
}
func TestService_ResubmitStagingImages_EngineError(t *testing.T) {
mockStore := image.MockStore{}
mockStore := image.StoreMock{InfoFunc: func() (image.StoreInfo, error) {
return image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil
}}
imgSvc := image.NewService(&mockStore,
image.ServiceParams{
EditDuration: 10 * time.Millisecond,
@@ -1483,26 +1494,37 @@ func TestService_ResubmitStagingImages_EngineError(t *testing.T) {
})
defer imgSvc.Close(context.TODO())
engineMock := engine.MockInterface{}
first := true
engineMock := engine.InterfaceMock{
FindFunc: func(req engine.FindRequest) ([]store.Comment, error) {
if first {
first = false
return nil, nil
}
return nil, fmt.Errorf("mockError")
},
}
site1Req := engine.FindRequest{Locator: store.Locator{SiteID: "site1", URL: ""}, Sort: "time", Since: time.Time{}.Add(time.Second)}
site2Req := engine.FindRequest{Locator: store.Locator{SiteID: "site2", URL: ""}, Sort: "time", Since: time.Time{}.Add(time.Second)}
engineMock.On("Find", site1Req).Return(nil, nil)
engineMock.On("Find", site2Req).Return(nil, fmt.Errorf("mockError"))
b := DataStore{Engine: &engineMock, EditDuration: 10 * time.Millisecond, ImageService: imgSvc}
// One call without error and one with error
mockStore.On("Info").Once().Return(image.StoreInfo{FirstStagingImageTS: time.Time{}.Add(time.Second)}, nil)
err := b.ResubmitStagingImages([]string{"site1", "site2"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "problem finding comments for site site2: mockError")
mockStore.AssertNumberOfCalls(t, "Info", 1)
assert.Equal(t, 1, len(mockStore.InfoCalls()))
assert.Equal(t, 2, len(engineMock.FindCalls()))
assert.Equal(t, site1Req, engineMock.FindCalls()[0].Req)
assert.Equal(t, site2Req, engineMock.FindCalls()[1].Req)
}
func TestService_alterComment(t *testing.T) {
engineMock := engine.MockInterface{}
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}).Return(false, nil)
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}).Return(false, nil)
engineMock := engine.InterfaceMock{
FlagFunc: func(req engine.FlagRequest) (bool, error) {
return false, nil
},
}
svc := DataStore{Engine: &engineMock}
r := svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"},
@@ -1513,24 +1535,48 @@ func TestService_alterComment(t *testing.T) {
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"}},
store.User{Name: "dev", ID: "devid", Admin: true})
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid"}}, r, "ip not cleaned")
assert.Equal(t, 4, len(engineMock.FlagCalls()))
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[1].Req)
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[2].Req)
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[3].Req)
engineMock = engine.MockInterface{}
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}).Return(false, nil)
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}).Return(true, nil)
first := true
engineMock = engine.InterfaceMock{
FlagFunc: func(req engine.FlagRequest) (bool, error) {
if first {
first = false
return false, nil
}
return true, nil
},
}
svc = DataStore{Engine: &engineMock}
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid", Verified: true}},
store.User{Name: "dev", ID: "devid", Admin: false})
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "", ID: "devid", Verified: true}}, r, "verified set")
assert.Equal(t, 2, len(engineMock.FlagCalls()))
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
assert.Equal(t, engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}, engineMock.FlagCalls()[1].Req)
engineMock = engine.MockInterface{}
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}).Return(true, nil)
engineMock.On("Flag", engine.FlagRequest{Flag: engine.Verified, UserID: "devid"}).Return(false, nil)
first = true
engineMock = engine.InterfaceMock{
FlagFunc: func(req engine.FlagRequest) (bool, error) {
if first {
first = false
return true, nil
}
return false, nil
},
}
svc = DataStore{Engine: &engineMock}
r = svc.alterComment(store.Comment{ID: "123", User: store.User{IP: "127.0.0.1", ID: "devid", Verified: true},
Locator: store.Locator{URL: "javascript:alert('XSS1')"}},
store.User{Name: "dev", ID: "devid", Admin: false})
assert.Equal(t, store.Comment{ID: "123", User: store.User{IP: "", Verified: true, Blocked: true, ID: "devid"},
Deleted: false}, r, "blocked")
assert.Equal(t, 1, len(engineMock.FlagCalls()))
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
}
func Benchmark_ServiceCreate(b *testing.B) {