## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# storage/storagetest

> Package storagetest holds mocks of storage.Provider, MockProvider, and of storage.Presigner, MockPresigner, for tests of code that stores objects.

The Markdown of https://gasmod.dev/reference/storage/storagetest/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/storage/storagetest"
```

Package storagetest holds mocks of storage.Provider, [MockProvider](#MockProvider), and
of storage.Presigner, [MockPresigner](#MockPresigner), for tests of code that stores
objects.

## Usage

A mock records each call, and returns what the function in the field
named after the method returns:

```go
m := &storagetest.MockProvider{
	GetObjectFn: func(context.Context, *storage.GetObjectInput) (*storage.Object, error) {
		return nil, errOffline
	},
}
_, err := m.GetObject(ctx, &storage.GetObjectInput{})
// err is errOffline, and m.CallCount("GetObject") is 1
```

A mock keeps none of the storage package's rules: it checks no key,
stores nothing and reads no body. To test against those rules without a
server, use gas/storage/memory.

## Defaults

With a field nil, its method succeeds with a zero output, never a nil
one:

- CreateBucket, DeleteBucket and DeleteObject return nil.
- HeadBucket returns a Bucket with the name asked for.
- ListBuckets and ListObjects return an empty page.
- PutObject returns a zero PutObjectOutput, without reading the body.
- CopyObject returns a zero CopyObjectOutput.
- PresignUploadURL and PresignDownloadURL return a PresignURLResponse
with only its Method, PUT or GET.

The exception: HeadObject and GetObject return storage.ErrObjectNotFound,
as a store that holds no object does, so that code under test never gets
a nil object with a nil error.

## Calls

Each method appends a [Call](#Call) with its name and a copy of its input,
before it calls its field. A nil input is recorded as a nil pointer of
its type.

A copy shares no pointer or map with the caller's input, so a caller
that reuses an input, as a listing loop does with its ContinuationToken,
does not change a recorded call. The Body of a PutObjectInput is the
same reader.

The recorded calls are read and cleared with:

- Calls, which returns a deep copy of the calls in the order they were
made, so that changing what it returns does not change them either.
- CallCount, which counts those of one method.
- Reset, which forgets them all and keeps the fields.

The methods may be called from several goroutines at once, as the calls
are kept under a lock, which Calls, CallCount and Reset take too.
Setting a field while a method runs is a data race, and a nil mock
panics.


## API reference

<a id="Call"></a>

### type Call

```go
type Call struct {
	// Method is the name of the method called.
	Method string
	// Args are the method's arguments after the context: a copy of its
	// input.
	Args []any
}
```

Call is one call of a mock's method.

<a id="MockPresigner"></a>

### type MockPresigner

```go
type MockPresigner struct {

	// PresignUploadURLFn is what PresignUploadURL returns. When it is nil,
	// PresignUploadURL returns a zero PresignURLResponse with Method PUT.
	PresignUploadURLFn func(
		ctx context.Context,
		in *storage.PresignUploadURLInput,
	) (*storage.PresignURLResponse, error)
	// PresignDownloadURLFn is what PresignDownloadURL returns. When it is
	// nil, PresignDownloadURL returns a zero PresignURLResponse with
	// Method GET.
	PresignDownloadURLFn func(
		ctx context.Context,
		in *storage.PresignDownloadURLInput,
	) (*storage.PresignURLResponse, error)
	// contains filtered or unexported fields
}
```

MockPresigner is a storage.Presigner that records its calls, as
described in the package documentation. The zero MockPresigner is ready
to use.

<a id="MockPresigner.CallCount"></a>

#### func (*MockPresigner) CallCount

```go
func (r *MockPresigner) CallCount(method string) int
```

CallCount returns how many calls were made to method.

<a id="MockPresigner.Calls"></a>

#### func (*MockPresigner) Calls

```go
func (r *MockPresigner) Calls() []Call
```

Calls returns a deep copy of the calls, in the order they were made:
changing an input it returns changes no recorded call.

<a id="MockPresigner.PresignDownloadURL"></a>

#### func (*MockPresigner) PresignDownloadURL

```go
func (m *MockPresigner) PresignDownloadURL(
	ctx context.Context,
	in *storage.PresignDownloadURLInput,
) (*storage.PresignURLResponse, error)
```

PresignDownloadURL records the call and returns PresignDownloadURLFn's
result.

<a id="MockPresigner.PresignUploadURL"></a>

#### func (*MockPresigner) PresignUploadURL

```go
func (m *MockPresigner) PresignUploadURL(
	ctx context.Context,
	in *storage.PresignUploadURLInput,
) (*storage.PresignURLResponse, error)
```

PresignUploadURL records the call and returns PresignUploadURLFn's
result.

<a id="MockPresigner.Reset"></a>

#### func (*MockPresigner) Reset

```go
func (r *MockPresigner) Reset()
```

Reset forgets every call. The fields are kept.

<a id="MockProvider"></a>

### type MockProvider

```go
type MockProvider struct {

	// CreateBucketFn is what CreateBucket returns. When it is nil,
	// CreateBucket returns nil.
	CreateBucketFn func(ctx context.Context, in *storage.CreateBucketInput) error
	// HeadBucketFn is what HeadBucket returns. When it is nil, HeadBucket
	// returns a Bucket with the name asked for.
	HeadBucketFn func(ctx context.Context, in *storage.HeadBucketInput) (*storage.Bucket, error)
	// DeleteBucketFn is what DeleteBucket returns. When it is nil,
	// DeleteBucket returns nil.
	DeleteBucketFn func(ctx context.Context, in *storage.DeleteBucketInput) error
	// ListBucketsFn is what ListBuckets returns. When it is nil,
	// ListBuckets returns an empty page.
	ListBucketsFn func(
		ctx context.Context,
		in *storage.ListBucketsInput,
	) (*storage.ListBucketsOutput, error)
	// HeadObjectFn is what HeadObject returns. When it is nil, HeadObject
	// returns storage.ErrObjectNotFound.
	HeadObjectFn func(ctx context.Context, in *storage.HeadObjectInput) (*storage.ObjectInfo, error)
	// PutObjectFn is what PutObject returns. When it is nil, PutObject
	// returns a zero PutObjectOutput, without reading the body.
	PutObjectFn func(ctx context.Context, in *storage.PutObjectInput) (*storage.PutObjectOutput, error)
	// GetObjectFn is what GetObject returns. When it is nil, GetObject
	// returns storage.ErrObjectNotFound.
	GetObjectFn func(ctx context.Context, in *storage.GetObjectInput) (*storage.Object, error)
	// DeleteObjectFn is what DeleteObject returns. When it is nil,
	// DeleteObject returns nil.
	DeleteObjectFn func(ctx context.Context, in *storage.DeleteObjectInput) error
	// ListObjectsFn is what ListObjects returns. When it is nil,
	// ListObjects returns an empty page.
	ListObjectsFn func(
		ctx context.Context,
		in *storage.ListObjectsInput,
	) (*storage.ListObjectsOutput, error)
	// CopyObjectFn is what CopyObject returns. When it is nil, CopyObject
	// returns a zero CopyObjectOutput.
	CopyObjectFn func(
		ctx context.Context,
		in *storage.CopyObjectInput,
	) (*storage.CopyObjectOutput, error)
	// contains filtered or unexported fields
}
```

MockProvider is a storage.Provider that records its calls, as described
in the package documentation. The zero MockProvider is ready to use.

<a id="MockProvider.CallCount"></a>

#### func (*MockProvider) CallCount

```go
func (r *MockProvider) CallCount(method string) int
```

CallCount returns how many calls were made to method.

<a id="MockProvider.Calls"></a>

#### func (*MockProvider) Calls

```go
func (r *MockProvider) Calls() []Call
```

Calls returns a deep copy of the calls, in the order they were made:
changing an input it returns changes no recorded call.

<a id="MockProvider.CopyObject"></a>

#### func (*MockProvider) CopyObject

```go
func (m *MockProvider) CopyObject(
	ctx context.Context,
	in *storage.CopyObjectInput,
) (*storage.CopyObjectOutput, error)
```

CopyObject records the call and returns CopyObjectFn's result.

<a id="MockProvider.CreateBucket"></a>

#### func (*MockProvider) CreateBucket

```go
func (m *MockProvider) CreateBucket(ctx context.Context, in *storage.CreateBucketInput) error
```

CreateBucket records the call and returns CreateBucketFn's result.

<a id="MockProvider.DeleteBucket"></a>

#### func (*MockProvider) DeleteBucket

```go
func (m *MockProvider) DeleteBucket(ctx context.Context, in *storage.DeleteBucketInput) error
```

DeleteBucket records the call and returns DeleteBucketFn's result.

<a id="MockProvider.DeleteObject"></a>

#### func (*MockProvider) DeleteObject

```go
func (m *MockProvider) DeleteObject(ctx context.Context, in *storage.DeleteObjectInput) error
```

DeleteObject records the call and returns DeleteObjectFn's result.

<a id="MockProvider.GetObject"></a>

#### func (*MockProvider) GetObject

```go
func (m *MockProvider) GetObject(
	ctx context.Context,
	in *storage.GetObjectInput,
) (*storage.Object, error)
```

GetObject records the call and returns GetObjectFn's result.

<a id="MockProvider.HeadBucket"></a>

#### func (*MockProvider) HeadBucket

```go
func (m *MockProvider) HeadBucket(
	ctx context.Context,
	in *storage.HeadBucketInput,
) (*storage.Bucket, error)
```

HeadBucket records the call and returns HeadBucketFn's result.

<a id="MockProvider.HeadObject"></a>

#### func (*MockProvider) HeadObject

```go
func (m *MockProvider) HeadObject(
	ctx context.Context,
	in *storage.HeadObjectInput,
) (*storage.ObjectInfo, error)
```

HeadObject records the call and returns HeadObjectFn's result.

<a id="MockProvider.ListBuckets"></a>

#### func (*MockProvider) ListBuckets

```go
func (m *MockProvider) ListBuckets(
	ctx context.Context,
	in *storage.ListBucketsInput,
) (*storage.ListBucketsOutput, error)
```

ListBuckets records the call and returns ListBucketsFn's result.

<a id="MockProvider.ListObjects"></a>

#### func (*MockProvider) ListObjects

```go
func (m *MockProvider) ListObjects(
	ctx context.Context,
	in *storage.ListObjectsInput,
) (*storage.ListObjectsOutput, error)
```

ListObjects records the call and returns ListObjectsFn's result.

<a id="MockProvider.PutObject"></a>

#### func (*MockProvider) PutObject

```go
func (m *MockProvider) PutObject(
	ctx context.Context,
	in *storage.PutObjectInput,
) (*storage.PutObjectOutput, error)
```

PutObject records the call and returns PutObjectFn's result.

<a id="MockProvider.Reset"></a>

#### func (*MockProvider) Reset

```go
func (r *MockProvider) Reset()
```

Reset forgets every call. The fields are kept.
