Last active
July 31, 2023 09:07
-
-
Save pnmcosta/c40357cc3bccef8e92b79aea75a9286f to your computer and use it in GitHub Desktop.
Pocketbase ApiScenario TestAppFactory with mocks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//go:generate go run -mod=mod github.com/golang/mock/mockgen -package main -destination=adapter_mock.go -source=./adapter.go -build_flags=-mod=mod | |
type ( | |
ExternalAdapter interface { | |
ExternalCall() error | |
} | |
externalAdapter struct { | |
} | |
) | |
var _ ExternalAdapter = (*externalAdapter)(nil) | |
func (a *externalAdapter) ExternalCall() error { | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code generated by MockGen. DO NOT EDIT. | |
// Source: ./adapter.go | |
// Package main is a generated GoMock package. | |
package main | |
import ( | |
reflect "reflect" | |
gomock "github.com/golang/mock/gomock" | |
) | |
// MockExternalAdapter is a mock of ExternalAdapter interface. | |
type MockExternalAdapter struct { | |
ctrl *gomock.Controller | |
recorder *MockExternalAdapterMockRecorder | |
} | |
// MockExternalAdapterMockRecorder is the mock recorder for MockExternalAdapter. | |
type MockExternalAdapterMockRecorder struct { | |
mock *MockExternalAdapter | |
} | |
// NewMockExternalAdapter creates a new mock instance. | |
func NewMockExternalAdapter(ctrl *gomock.Controller) *MockExternalAdapter { | |
mock := &MockExternalAdapter{ctrl: ctrl} | |
mock.recorder = &MockExternalAdapterMockRecorder{mock} | |
return mock | |
} | |
// EXPECT returns an object that allows the caller to indicate expected use. | |
func (m *MockExternalAdapter) EXPECT() *MockExternalAdapterMockRecorder { | |
return m.recorder | |
} | |
// ExternalCall mocks base method. | |
func (m *MockExternalAdapter) ExternalCall() error { | |
m.ctrl.T.Helper() | |
ret := m.ctrl.Call(m, "ExternalCall") | |
ret0, _ := ret[0].(error) | |
return ret0 | |
} | |
// ExternalCall indicates an expected call of ExternalCall. | |
func (mr *MockExternalAdapterMockRecorder) ExternalCall() *gomock.Call { | |
mr.mock.ctrl.T.Helper() | |
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExternalCall", reflect.TypeOf((*MockExternalAdapter)(nil).ExternalCall)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module pbdemo | |
go 1.20 | |
require ( | |
github.com/golang/mock v1.6.0 | |
github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61 | |
github.com/pocketbase/pocketbase v0.16.10 | |
) | |
require ( | |
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect | |
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect | |
github.com/aws/aws-sdk-go v1.44.312 // indirect | |
github.com/aws/aws-sdk-go-v2 v1.19.1 // indirect | |
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect | |
github.com/aws/aws-sdk-go-v2/config v1.18.30 // indirect | |
github.com/aws/aws-sdk-go-v2/credentials v1.13.29 // indirect | |
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.6 // indirect | |
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.74 // indirect | |
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.36 // indirect | |
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.30 // indirect | |
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.37 // indirect | |
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.28 // indirect | |
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 // indirect | |
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.31 // indirect | |
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.30 // indirect | |
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.5 // indirect | |
github.com/aws/aws-sdk-go-v2/service/s3 v1.37.1 // indirect | |
github.com/aws/aws-sdk-go-v2/service/sso v1.12.14 // indirect | |
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.14 // indirect | |
github.com/aws/aws-sdk-go-v2/service/sts v1.20.1 // indirect | |
github.com/aws/smithy-go v1.13.5 // indirect | |
github.com/disintegration/imaging v1.6.2 // indirect | |
github.com/domodwyer/mailyak/v3 v3.6.1 // indirect | |
github.com/dustin/go-humanize v1.0.1 // indirect | |
github.com/fatih/color v1.15.0 // indirect | |
github.com/gabriel-vasile/mimetype v1.4.2 // indirect | |
github.com/ganigeorgiev/fexpr v0.3.0 // indirect | |
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 // indirect | |
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect | |
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect | |
github.com/golang/protobuf v1.5.3 // indirect | |
github.com/google/uuid v1.3.0 // indirect | |
github.com/google/wire v0.5.0 // indirect | |
github.com/googleapis/gax-go/v2 v2.12.0 // indirect | |
github.com/inconshreveable/mousetrap v1.1.0 // indirect | |
github.com/jmespath/go-jmespath v0.4.0 // indirect | |
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | |
github.com/mattn/go-colorable v0.1.13 // indirect | |
github.com/mattn/go-isatty v0.0.19 // indirect | |
github.com/mattn/go-sqlite3 v1.14.17 // indirect | |
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect | |
github.com/pocketbase/dbx v1.10.0 // indirect | |
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect | |
github.com/spf13/cast v1.5.1 // indirect | |
github.com/spf13/cobra v1.7.0 // indirect | |
github.com/spf13/pflag v1.0.5 // indirect | |
github.com/valyala/bytebufferpool v1.0.0 // indirect | |
github.com/valyala/fasttemplate v1.2.2 // indirect | |
go.opencensus.io v0.24.0 // indirect | |
gocloud.dev v0.32.0 // indirect | |
golang.org/x/crypto v0.11.0 // indirect | |
golang.org/x/image v0.9.0 // indirect | |
golang.org/x/mod v0.12.0 // indirect | |
golang.org/x/net v0.12.0 // indirect | |
golang.org/x/oauth2 v0.10.0 // indirect | |
golang.org/x/sync v0.3.0 // indirect | |
golang.org/x/sys v0.10.0 // indirect | |
golang.org/x/term v0.10.0 // indirect | |
golang.org/x/text v0.11.0 // indirect | |
golang.org/x/time v0.3.0 // indirect | |
golang.org/x/tools v0.11.0 // indirect | |
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect | |
google.golang.org/api v0.134.0 // indirect | |
google.golang.org/appengine v1.6.7 // indirect | |
google.golang.org/genproto/googleapis/rpc v0.0.0-20230726155614-23370e0ffb3e // indirect | |
google.golang.org/grpc v1.57.0 // indirect | |
google.golang.org/protobuf v1.31.0 // indirect | |
lukechampine.com/uint128 v1.3.0 // indirect | |
modernc.org/cc/v3 v3.41.0 // indirect | |
modernc.org/ccgo/v3 v3.16.14 // indirect | |
modernc.org/libc v1.24.1 // indirect | |
modernc.org/mathutil v1.6.0 // indirect | |
modernc.org/memory v1.6.0 // indirect | |
modernc.org/opt v0.1.3 // indirect | |
modernc.org/sqlite v1.24.0 // indirect | |
modernc.org/strutil v1.1.3 // indirect | |
modernc.org/token v1.1.0 // indirect | |
) | |
replace github.com/pocketbase/pocketbase => github.com/pnmcosta/pocketbase v0.0.0-20230731075920-4239dd427f2c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"log" | |
"net/http" | |
"github.com/labstack/echo/v5" | |
"github.com/pocketbase/pocketbase" | |
"github.com/pocketbase/pocketbase/apis" | |
"github.com/pocketbase/pocketbase/core" | |
) | |
func BindHandlers(app core.App, adapter ExternalAdapter) { | |
app.OnBeforeServe().Add(func(e *core.ServeEvent) error { | |
e.Router.GET("/test", func(c echo.Context) error { | |
err := adapter.ExternalCall() | |
if err != nil { | |
return apis.NewApiError(http.StatusInternalServerError, "error external call", err) | |
} | |
return c.NoContent(http.StatusOK) | |
}) | |
return nil | |
}) | |
} | |
func main() { | |
app := pocketbase.New() | |
adapter := &externalAdapter{} | |
BindHandlers(app, adapter) | |
if err := app.Start(); err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"testing" | |
gomock "github.com/golang/mock/gomock" | |
"github.com/pocketbase/pocketbase/tests" | |
) | |
func TestMain(t *testing.T) { | |
scenarios := []tests.ApiScenario{ | |
{ | |
Name: "should 500", | |
Method: http.MethodGet, | |
Url: "/test", | |
ExpectedStatus: http.StatusInternalServerError, | |
ExpectedContent: []string{""}, | |
TestAppFactory: NewTestAppFactoryWithMocks(func(mea *MockExternalAdapter) { | |
mea. | |
EXPECT(). | |
ExternalCall(). | |
Return(fmt.Errorf("failed")). | |
Times(1) | |
}), | |
}, | |
{ | |
Name: "should 200", | |
Method: http.MethodGet, | |
Url: "/test", | |
ExpectedStatus: http.StatusOK, | |
ExpectedContent: []string{""}, | |
TestAppFactory: NewTestAppFactoryWithMocks(func(mea *MockExternalAdapter) { | |
mea. | |
EXPECT(). | |
ExternalCall(). | |
Return(nil). | |
Times(1) | |
}), | |
}, | |
} | |
for _, scenario := range scenarios { | |
scenario.Test(t) | |
} | |
} | |
func NewTestAppFactoryWithMocks(expect func(*MockExternalAdapter)) func(t *testing.T) (*tests.TestApp, error) { | |
return func(t *testing.T) (*tests.TestApp, error) { | |
app, err := tests.NewTestApp() | |
if err != nil { | |
return nil, err | |
} | |
ctrl := gomock.NewController(t) | |
externalAdapter := NewMockExternalAdapter(ctrl) | |
if expect != nil { | |
expect(externalAdapter) | |
} | |
BindHandlers(app, externalAdapter) | |
return app, nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment