use mockery for mock code generation (#185)

close 	#181

Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/185
This commit is contained in:
6543 2023-02-13 23:13:30 +00:00
parent 9a3d1c36dc
commit 42b3f8d1b7
3 changed files with 113 additions and 35 deletions

View File

@ -4,13 +4,15 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"codeberg.org/codeberg/pages/server/database" "codeberg.org/codeberg/pages/server/database"
) )
func TestMockCert(t *testing.T) { func TestMockCert(t *testing.T) {
db, err := database.NewTmpDB() db := database.NewMockCertDB(t)
assert.NoError(t, err) db.Mock.On("Put", mock.Anything, mock.Anything).Return(nil)
cert, err := mockCert("example.com", "some error msg", "codeberg.page", db) cert, err := mockCert("example.com", "some error msg", "codeberg.page", db)
assert.NoError(t, err) assert.NoError(t, err)
if assert.NotEmpty(t, cert) { if assert.NotEmpty(t, cert) {

View File

@ -8,6 +8,9 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
//go:generate go install github.com/vektra/mockery/v2@latest
//go:generate mockery --name CertDB --output . --filename mock.go --inpackage --case underscore
type CertDB interface { type CertDB interface {
Close() error Close() error
Put(name string, cert *certificate.Resource) error Put(name string, cert *certificate.Resource) error

View File

@ -1,49 +1,122 @@
// Code generated by mockery v2.20.0. DO NOT EDIT.
package database package database
import ( import (
"fmt" certificate "github.com/go-acme/lego/v4/certificate"
"time" mock "github.com/stretchr/testify/mock"
"github.com/OrlovEvgeny/go-mcache"
"github.com/go-acme/lego/v4/certificate"
) )
var _ CertDB = tmpDB{} // MockCertDB is an autogenerated mock type for the CertDB type
type MockCertDB struct {
type tmpDB struct { mock.Mock
intern *mcache.CacheDriver
ttl time.Duration
} }
func (p tmpDB) Close() error { // Close provides a mock function with given fields:
_ = p.intern.Close() func (_m *MockCertDB) Close() error {
return nil ret := _m.Called()
}
func (p tmpDB) Put(name string, cert *certificate.Resource) error { var r0 error
return p.intern.Set(name, cert, p.ttl) if rf, ok := ret.Get(0).(func() error); ok {
} r0 = rf()
} else {
func (p tmpDB) Get(name string) (*certificate.Resource, error) { r0 = ret.Error(0)
cert, has := p.intern.Get(name)
if !has {
return nil, fmt.Errorf("cert for %q not found", name)
} }
return cert.(*certificate.Resource), nil
return r0
} }
func (p tmpDB) Delete(key string) error { // Delete provides a mock function with given fields: key
p.intern.Remove(key) func (_m *MockCertDB) Delete(key string) error {
return nil ret := _m.Called(key)
var r0 error
if rf, ok := ret.Get(0).(func(string) error); ok {
r0 = rf(key)
} else {
r0 = ret.Error(0)
}
return r0
} }
func (p tmpDB) Items(page, pageSize int) ([]*Cert, error) { // Get provides a mock function with given fields: name
return nil, fmt.Errorf("items not implemented for tmpDB") func (_m *MockCertDB) Get(name string) (*certificate.Resource, error) {
ret := _m.Called(name)
var r0 *certificate.Resource
var r1 error
if rf, ok := ret.Get(0).(func(string) (*certificate.Resource, error)); ok {
return rf(name)
}
if rf, ok := ret.Get(0).(func(string) *certificate.Resource); ok {
r0 = rf(name)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*certificate.Resource)
}
}
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(name)
} else {
r1 = ret.Error(1)
}
return r0, r1
} }
func NewTmpDB() (CertDB, error) { // Items provides a mock function with given fields: page, pageSize
return &tmpDB{ func (_m *MockCertDB) Items(page int, pageSize int) ([]*Cert, error) {
intern: mcache.New(), ret := _m.Called(page, pageSize)
ttl: time.Minute,
}, nil var r0 []*Cert
var r1 error
if rf, ok := ret.Get(0).(func(int, int) ([]*Cert, error)); ok {
return rf(page, pageSize)
}
if rf, ok := ret.Get(0).(func(int, int) []*Cert); ok {
r0 = rf(page, pageSize)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*Cert)
}
}
if rf, ok := ret.Get(1).(func(int, int) error); ok {
r1 = rf(page, pageSize)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// Put provides a mock function with given fields: name, cert
func (_m *MockCertDB) Put(name string, cert *certificate.Resource) error {
ret := _m.Called(name, cert)
var r0 error
if rf, ok := ret.Get(0).(func(string, *certificate.Resource) error); ok {
r0 = rf(name, cert)
} else {
r0 = ret.Error(0)
}
return r0
}
type mockConstructorTestingTNewMockCertDB interface {
mock.TestingT
Cleanup(func())
}
// NewMockCertDB creates a new instance of MockCertDB. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockCertDB(t mockConstructorTestingTNewMockCertDB) *MockCertDB {
mock := &MockCertDB{}
mock.Mock.Test(t)
t.Cleanup(func() { mock.AssertExpectations(t) })
return mock
} }