payloadfakes

package
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package payloadfakes provides mock implementations of various Payload CMS services. These mocks can be used in unit tests to simulate the behavior of the Payload CMS API without making actual network requests.

## Services

The following mocks are provided:

  • MockCollectionService: A mock implementation of the CollectionService interface.
  • MockGlobalsService: A mock implementation of the GlobalsService interface.
  • MockService: A mock implementation of the Service interface.
  • MockMediaService: A mock implementation of the MediaService interface.

Each mock service allows you to define the behavior of its methods by setting the corresponding function fields. By default, the methods return an empty payloadcms.Response and a nil error.

## Usage

1. Create an instance of the mock service you need. 2. Define the behavior of the methods you are going to use by setting the corresponding function fields. 3. Use the mock service instance in your tests.

This allows you to fully control and assert the behavior of your code when interacting with the Payload CMS API.

## Example

func TestPayload(t *testing.T) {

// Create a new mock collection service
mockCollectionService := payloadfakes.NewMockCollectionService()

// Define the behavior of the FindByID method
mockCollectionService.FindByIDFunc = func(ctx context.Context,
	collection payloadcms.Collection,
	id int,
	out any,
) (payloadcms.Response, error) {
    // Custom logic for the mock implementation
    return payloadcms.Response{}, nil
}

// Use the mock collection service in your tests
myFunctionUsingCollectionService(mockCollectionService)

}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockCollectionService

type MockCollectionService struct {
	FindByIDFunc   func(ctx context.Context, collection payloadcms.Collection, id any, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	FindBySlugFunc func(ctx context.Context, collection payloadcms.Collection, slug string, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	ListFunc       func(ctx context.Context, collection payloadcms.Collection, params payloadcms.ListParams, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	CreateFunc     func(ctx context.Context, collection payloadcms.Collection, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	UpdateByIDFunc func(ctx context.Context, collection payloadcms.Collection, id any, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	DeleteByIDFunc func(ctx context.Context, collection payloadcms.Collection, id any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
}

MockCollectionService is a mock implementation of the CollectionService interface.

func NewMockCollectionService

func NewMockCollectionService() *MockCollectionService

NewMockCollectionService creates a new fake collections stub.

func (*MockCollectionService) Create

Create calls the mock implementation.

func (*MockCollectionService) DeleteByID

DeleteByID calls the mock implementation.

func (*MockCollectionService) FindByID

FindByID calls the mock implementation.

func (*MockCollectionService) FindBySlug

func (m *MockCollectionService) FindBySlug(ctx context.Context, collection payloadcms.Collection, slug string, out any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)

FindBySlug calls the mock implementation.

func (*MockCollectionService) List

List calls the mock implementation.

func (*MockCollectionService) UpdateByID

UpdateByID calls the mock implementation.

type MockGlobalsService

type MockGlobalsService struct {
	GetFunc    func(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
	UpdateFunc func(ctx context.Context, global payloadcms.Global, in any, opts ...payloadcms.RequestOption) (payloadcms.Response, error)
}

MockGlobalsService is a mock implementation of the GlobalsService interface.

func NewMockGlobalsService

func NewMockGlobalsService() *MockGlobalsService

NewMockGlobalsService creates a new fake globals stub.

func (*MockGlobalsService) Get

Get calls the mock implementation.

func (*MockGlobalsService) Update

Update calls the mock implementation.

type MockMediaService

type MockMediaService struct {
	UploadFunc        func(ctx context.Context, f *os.File, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
	UploadFromURLFunc func(ctx context.Context, url string, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)
}

MockMediaService is a mock implementation of the MediaService interface.

func NewMockMediaService

func NewMockMediaService() *MockMediaService

NewMockMediaService creates a new fake media service stub.

func (*MockMediaService) Upload

func (m *MockMediaService) Upload(ctx context.Context, f *os.File, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)

Upload calls the mock implementation.

func (*MockMediaService) UploadFromURL

func (m *MockMediaService) UploadFromURL(ctx context.Context, url string, in, out any, opts payloadcms.MediaOptions) (payloadcms.Response, error)

UploadFromURL calls the mock implementation.

type MockService

type MockService struct {
	DoFunc            func(ctx context.Context, method, path string, body any, v any) (payloadcms.Response, error)
	DoWithRequestFunc func(ctx context.Context, req *http.Request, v any) (payloadcms.Response, error)
	GetFunc           func(ctx context.Context, path string, v any) (payloadcms.Response, error)
	PostFunc          func(ctx context.Context, path string, in any) (payloadcms.Response, error)
	PutFunc           func(ctx context.Context, path string, in any) (payloadcms.Response, error)
	DeleteFunc        func(ctx context.Context, path string, v any) (payloadcms.Response, error)
}

MockService is a mock implementation of the Service interface.

func NewMockService

func NewMockService() *MockService

NewMockService creates a new fake service stub.

func (*MockService) Delete

func (m *MockService) Delete(ctx context.Context, path string, v any) (payloadcms.Response, error)

Delete calls the mock implementation.

func (*MockService) Do

func (m *MockService) Do(ctx context.Context, method, path string, body any, v any) (payloadcms.Response, error)

Do calls the mock implementation.

func (*MockService) DoWithRequest

func (m *MockService) DoWithRequest(ctx context.Context, req *http.Request, v any) (payloadcms.Response, error)

DoWithRequest calls the mock implementation.

func (*MockService) Get

func (m *MockService) Get(ctx context.Context, path string, v any) (payloadcms.Response, error)

Get calls the mock implementation.

func (*MockService) Post

func (m *MockService) Post(ctx context.Context, path string, in any) (payloadcms.Response, error)

Post calls the mock implementation.

func (*MockService) Put

func (m *MockService) Put(ctx context.Context, path string, in any) (payloadcms.Response, error)

Put calls the mock implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL