mockstorage

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2024 License: MIT Imports: 3 Imported by: 0

README


id: mockstorage title: MockStorage

Release Discord Test Security Linter

A mock storage implementation for Fiber. This storage is not persistent and is only used for testing purposes.

Note: Requires Go 1.21 and above

Table of Contents

Signatures

Structs

type Storage struct {
    // contains filtered or unexported fields
}

type Entry struct {
    Value []byte
    Exp   time.Time
}

type Config struct {
    CustomFuncs *CustomFuncs
}

type CustomFuncs struct {
    GetFunc    func(key string) ([]byte, error)
    SetFunc    func(key string, val []byte, exp time.Duration) error
    DeleteFunc func(key string) error
    ResetFunc  func() error
    CloseFunc  func() error
    ConnFunc   func() map[string]Entry
    KeysFunc   func() ([][]byte, error)
}

Functions

// New creates a new Storage instance. You can optionally pass a Config.
func New(config ...Config) *Storage

// Get retrieves the value associated with the given key.
func (s *Storage) Get(key string) ([]byte, error)

// Set sets the value for the given key, with an optional expiration duration.
func (s *Storage) Set(key string, val []byte, exp time.Duration) error

// Delete removes the value associated with the given key.
func (s *Storage) Delete(key string) error

// Reset clears all values from the storage.
func (s *Storage) Reset() error

// Close performs any necessary cleanup when the storage is no longer needed.
func (s *Storage) Close() error

// Conn returns a copy of the current state of the storage.
func (s *Storage) Conn() map[string]Entry

// Keys returns a list of all keys in the storage.
func (s *Storage) Keys() ([][]byte, error)

// SetCustomFuncs allows you to set custom functions for the storage operations.
func (s *Storage) SetCustomFuncs(custom *CustomFuncs)

Installation

MockStorage is tested on the 2 last Go versions with support for modules. So make sure to initialize one first if you didn't do that yet:

go mod init github.com/<user>/<repo>

And then install the mockstorage implementation:

go get github.com/gofiber/storage/mockstorage

Examples

Import the storage package.

import "github.com/gofiber/storage/mockstorage"

You can use the following possibilities to create a storage:

// Initialize default config
store := mockstorage.New()

// Set a value in the storage.
err := store.Set("key1", []byte("value1"), 0)
if err != nil {
    // handle error
}

// Get a value from the storage.
val, err := store.Get("key1")
if err != nil {
    // handle error
}
fmt.Println(string(val)) // prints "value1"

// Delete a value from the storage.
err = store.Delete("key1")
if err != nil {
	// handle error
}

// Mocking storage operations in tests:
func TestMyFunction(t *testing.T) {
    // Create a new instance of MockStorage
    store := mockstorage.New()

    // Mock the Set function
    store.SetCustomFuncs(&mockstorage.CustomFuncs{
        Set: func(key string, val []byte, exp time.Duration) error {
            if key == "expectedKey" && string(val) == "expectedValue" {
                return nil
            }
            return errors.New("unexpected key or value")
        },
    })

    // Call the function you want to test, which should call store.Set
    err := MyFunction(store)

    // Check that the function behaved as expected
    if err != nil {
        t.Errorf("MyFunction returned an error: %v", err)
    }
}

Note: In the mockstorage package, expiration of data is not handled automatically in the background. The data is only marked as expired and removed when you attempt to Get() it after its expiration time. If you're using a custom Get() function or accessing the data directly using the Conn() function, expired data will not be removed. Keep this in mind when writing your tests.

Config

type Config struct {
	CustomFuncs *CustomFuncs
}

Default Config

var ConfigDefault = Config{
	CustomFuncs: &CustomFuncs{
		GetFunc:    nil,
		SetFunc:    nil,
		DeleteFunc: nil,
		ResetFunc:  nil,
		CloseFunc:  nil,
		ConnFunc:   nil,
		KeysFunc:   nil,
	},
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	CustomFuncs *CustomFuncs
}

Config defines the config for mock storage.

type CustomFuncs

type CustomFuncs struct {
	GetFunc    func(key string) ([]byte, error)
	SetFunc    func(key string, val []byte, exp time.Duration) error
	DeleteFunc func(key string) error
	ResetFunc  func() error
	CloseFunc  func() error
	ConnFunc   func() map[string]Entry
	KeysFunc   func() ([][]byte, error)
}

CustomFuncs allows injecting custom behaviors for testing.

type Entry

type Entry struct {
	Value []byte
	Exp   time.Time
}

Entry struct to hold value and expiration time.

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage is the mock storage adapter.

func New

func New(config ...Config) *Storage

New creates a new mock storage with optional configuration.

func (*Storage) Close

func (s *Storage) Close() error

Close closes the storage (no-op for mock).

func (*Storage) Conn

func (s *Storage) Conn() map[string]Entry

Conn returns the internal data map (for testing purposes).

func (*Storage) Delete

func (s *Storage) Delete(key string) error

Delete removes a key from the storage.

func (*Storage) Get

func (s *Storage) Get(key string) ([]byte, error)

Get retrieves the value for a given key.

func (*Storage) Keys

func (s *Storage) Keys() ([][]byte, error)

Keys returns all keys in the storage.

func (*Storage) Reset

func (s *Storage) Reset() error

Reset clears all keys from the storage.

func (*Storage) Set

func (s *Storage) Set(key string, val []byte, exp time.Duration) error

Set sets the value for a given key with an expiration time.

func (*Storage) SetCustomFuncs

func (s *Storage) SetCustomFuncs(custom *CustomFuncs)

SetCustomFuncs allows setting custom function implementations.

Jump to

Keyboard shortcuts

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