🤡 Use uber mock for mocks

This commit is contained in:
Dan Jones 2025-01-24 16:27:05 -06:00
commit ecae0d5f83
11 changed files with 2306 additions and 144 deletions

View file

@ -15,6 +15,13 @@ tasks:
- go fmt ./...
- go mod tidy
gen:
desc: Generate files
sources:
- '**/*.go'
cmds:
- go generate ./...
vet:
desc: Vet go code
sources:

View file

@ -4,22 +4,31 @@ import (
"fmt"
"testing"
"codeberg.org/danjones000/combluotion/config"
"codeberg.org/danjones000/combluotion/internal/testmocks"
vocab "github.com/go-ap/activitypub"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"codeberg.org/danjones000/combluotion/config"
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
)
func getStore(t *testing.T) *storeMock.MockStore {
t.Helper()
ctrl := gomock.NewController(t)
return storeMock.NewMockStore(ctrl)
}
func TestEmptyBaseURL(t *testing.T) {
c := config.Config{}
a, er := NewApp("0.0.0", c, testmocks.GetStore())
a, er := NewApp("0.0.0", c, getStore(t))
assert.Nil(t, a)
assert.EqualError(t, er, "missing BaseURL")
}
func TestDefaultEnvironment(t *testing.T) {
store := getStore(t)
c := config.Config{BaseURL: "http://localhost:1234/"}
a, er := NewApp("0.0.0", c, testmocks.GetStore())
a, er := NewApp("0.0.0", c, store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, config.Dev, a.Environment())
@ -40,8 +49,9 @@ func TestGivenEnvironment(t *testing.T) {
for _, c := range cases {
t.Run(string(c.given), func(t *testing.T) {
store := getStore(t)
conf := config.Config{BaseURL: "http://localhost:1234/", Env: c.given}
a, er := NewApp("0.0.0", conf, testmocks.GetStore())
a, er := NewApp("0.0.0", conf, store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, conf, a.Config())
@ -52,9 +62,10 @@ func TestGivenEnvironment(t *testing.T) {
}
func TestService(t *testing.T) {
store := getStore(t)
base := "http://localhost:1234/"
conf := config.Config{BaseURL: base}
a, er := NewApp("0.0.0.0", conf, testmocks.GetStore())
a, er := NewApp("0.0.0.0", conf, store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, vocab.IRI(base), a.ServiceIRI())
@ -75,9 +86,10 @@ func TestStrings(t *testing.T) {
for _, c := range cases {
t.Run(c.given, func(t *testing.T) {
store := getStore(t)
conf := config.Config{BaseURL: "http://localhost:1234/", Name: c.given}
expStr := fmt.Sprintf("%s (%s)", c.exp, "0.0.0.0")
a, er := NewApp("0.0.0.0", conf, testmocks.GetStore())
a, er := NewApp("0.0.0.0", conf, store)
assert.NoError(t, er)
if assert.NotNil(t, a) {
assert.Equal(t, c.exp, a.Name())

1
go.mod
View file

@ -12,6 +12,7 @@ require (
github.com/go-ap/storage-sqlite v0.0.0-20240910151457-20fa80d963aa
github.com/openshift/osin v1.0.2-0.20220317075346-0f4d38c6e53f
github.com/stretchr/testify v1.9.0
go.uber.org/mock v0.5.0
)
require (

2
go.sum
View file

@ -83,6 +83,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/fastjson v1.6.4 h1:uAUNq9Z6ymTgGhcm0UynUAB6tlbakBrz6CQFax3BXVQ=
github.com/valyala/fastjson v1.6.4/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=

View file

@ -1,45 +0,0 @@
package testmocks
import (
"codeberg.org/danjones000/combluotion/config"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/filters"
)
type st struct{}
func (s *st) Bootstrap(config.Config) error {
return nil
}
func (s *st) Load(iri vocab.IRI, filters ...filters.Check) (vocab.Item, error) {
i := vocab.ActorNew(iri, vocab.ActorType)
return i, nil
}
func (s *st) Save(v vocab.Item) (vocab.Item, error) {
return v, nil
}
func (s *st) Delete(v vocab.Item) error {
return nil
}
func (s *st) Create(col vocab.CollectionInterface) (vocab.CollectionInterface, error) {
return col, nil
}
func (s *st) AddTo(col vocab.IRI, it vocab.Item) error {
return nil
}
func (s *st) RemoveFrom(col vocab.IRI, it vocab.Item) error {
return nil
}
func (s *st) Close() {
}
func GetStore() *st {
return &st{}
}

File diff suppressed because it is too large Load diff

View file

@ -1,20 +0,0 @@
package testmocks
import (
"crypto"
vocab "github.com/go-ap/activitypub"
proc "github.com/go-ap/processing"
)
func (s *st) LoadKey(vocab.IRI) (crypto.PrivateKey, error) {
return nil, nil
}
func (s *st) LoadMetadata(vocab.IRI) (*proc.Metadata, error) {
return nil, nil
}
func (s *st) SaveMetadata(proc.Metadata, vocab.IRI) error {
return nil
}

View file

@ -1,37 +0,0 @@
package testmocks
import (
"github.com/openshift/osin"
)
func (s *st) LoadAccess(string) (*osin.AccessData, error) {
return nil, nil
}
func (s *st) SaveAccess(*osin.AccessData) error {
return nil
}
func (s *st) RemoveAccess(string) error {
return nil
}
func (s *st) LoadAuthorize(string) (*osin.AuthorizeData, error) {
return nil, nil
}
func (s *st) SaveAuthorize(*osin.AuthorizeData) error {
return nil
}
func (s *st) RemoveAuthorize(string) error {
return nil
}
func (s *st) LoadRefresh(string) (*osin.AccessData, error) {
return nil, nil
}
func (s *st) RemoveRefresh(string) error {
return nil
}

View file

@ -1,30 +0,0 @@
package testmocks
import (
"github.com/openshift/osin"
)
func (s *st) Clone() osin.Storage {
n := *s
return &n
}
func (s *st) GetClient(string) (osin.Client, error) {
return &osin.DefaultClient{}, nil
}
func (s *st) CreateClient(osin.Client) error {
return nil
}
func (s *st) UpdateClient(osin.Client) error {
return nil
}
func (s *st) RemoveClient(string) error {
return nil
}
func (s *st) ListClients() (cl []osin.Client, er error) {
return
}

View file

@ -3,16 +3,23 @@ package store
import (
"testing"
"codeberg.org/danjones000/combluotion/config"
"codeberg.org/danjones000/combluotion/internal/testmocks"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
"codeberg.org/danjones000/combluotion/config"
storeMock "codeberg.org/danjones000/combluotion/internal/testmocks/store"
)
var f StoreFactory = func(config.Config) (Store, error) {
return testmocks.GetStore(), nil
func getStoreFactory(t *testing.T) (*gomock.Controller, StoreFactory) {
t.Helper()
ctrl := gomock.NewController(t)
return ctrl, func(config.Config) (Store, error) {
return storeMock.NewMockStore(ctrl), nil
}
}
func TestAddFactory(t *testing.T) {
_, f := getStoreFactory(t)
AddFactory("mock", f)
defer delete(factories, "mock")
_, ok := factories["mock"]
@ -25,9 +32,10 @@ func TestGetFactoryNil(t *testing.T) {
}
func TestGetFactoryNotNil(t *testing.T) {
_, f := getStoreFactory(t)
AddFactory("mock", f)
defer delete(factories, "mock")
f := GetFactory("mock")
f = GetFactory("mock")
assert.NotNil(t, f)
}
@ -39,6 +47,7 @@ func TestMakeStoreError(t *testing.T) {
}
func TestMakeStoreNoError(t *testing.T) {
_, f := getStoreFactory(t)
AddFactory("mock", f)
defer delete(factories, "mock")
s, e := MakeStore("mock", config.Config{})
@ -47,6 +56,7 @@ func TestMakeStoreNoError(t *testing.T) {
}
func TestMakeStoreNoName(t *testing.T) {
_, f := getStoreFactory(t)
AddFactory("mock", f)
defer delete(factories, "mock")
s, e := MakeStore("", config.Config{

View file

@ -7,6 +7,8 @@ import (
"github.com/openshift/osin"
)
//go:generate mockgen -source store.go -destination ../internal/testmocks/store/store_mock.go -package store -typed
type ClientSaver interface {
// UpdateClient updates the client (identified by it's id) and replaces the values with the values of client.
UpdateClient(c osin.Client) error