mock

package
v0.0.0-...-360b72e Latest Latest
Warning

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

Go to latest
Published: May 7, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Example (TopFunction)
package main

import (
	"context"
	"fmt"

	"knative.dev/pkg/test/gcs"
)

// Example on how to override errors
const (
	bkt  = "NewBkt"
	proj = "NewProject"
)

func topFunction(c gcs.Client) error {
	ctx := context.Background()
	if err := c.NewStorageBucket(ctx, bkt, proj); err != nil {
		return err
	}

	// Should have returned error, but SetError override to nil
	if _, err := c.ReadObject(ctx, bkt, "non-existent-file"); err != nil {
		return err
	}

	if _, err := c.ListChildrenFiles(ctx, bkt, ""); err != nil {
		return err
	}

	if _, err := c.ListChildrenFiles(ctx, bkt, ""); err != nil {
		return err
	}

	// Should not have returned error, but SetError override to NewNoBucketError(bkt)
	if _, err := c.ListChildrenFiles(ctx, bkt, ""); err != nil {
		return err
	}

	return nil
}

func main() {
	mockClient := newClientMocker()

	// Call to ReadObject, first call should return error, but returns nil
	// because it is overridden.
	mockClient.SetError(
		map[Method]*ReturnError{
			MethodReadObject: {
				NumCall: uint8(0),
				Err:     nil,
			},
			MethodListChildrenFiles: {
				NumCall: uint8(2),
				Err:     NewNoBucketError(bkt),
			},
		})

	fmt.Println(topFunction(mockClient))
}
Output:

no bucket "NewBkt"

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// MethodNewStorageBucket mocks MethodNewStorageBucket.
	MethodNewStorageBucket = Method("NewStorageBucket")
	// MethodDeleteStorageBucket mocks NewDeleteStorageBucket.
	MethodDeleteStorageBucket = Method("NewDeleteStorageBucket")
	// MethodListChildrenFiles mocks ListChildrenFiles.
	MethodListChildrenFiles = Method("ListChildrenFiles")
	// MethodListDirectChildren mocks ListDirectChildren.
	MethodListDirectChildren = Method("ListDirectChildren")
	// MethodAttrObject mocks AttrObject.
	MethodAttrObject = Method("AttrObject")
	// MethodCopyObject mocks CopyObject.
	MethodCopyObject = Method("CopyObject")
	// MethodReadObject mocks ReadObject.
	MethodReadObject = Method("ReadObject")
	// MethodWriteObject mocks WriteObject.
	MethodWriteObject = Method("WriteObject")
	// MethodDeleteObject mocks DeleteObject.
	MethodDeleteObject = Method("DeleteObject")
	// MethodDownload mocks Download.
	MethodDownload = Method("Download")
	// MethodUpload mocks Upload.
	MethodUpload = Method("Upload")
)

Functions

func NewBucketExistError

func NewBucketExistError(bkt string) error

func NewNoBucketError

func NewNoBucketError(bkt string) error

func NewNoObjectError

func NewNoObjectError(bkt, obj, path string) error

func NewNotEmptyBucketError

func NewNotEmptyBucketError(bkt string) error

Types

type Method

type Method string

type ReturnError

type ReturnError struct {
	NumCall uint8
	Err     error
}

ReturnError is a custom error for specific methods

Jump to

Keyboard shortcuts

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