testutils

package
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package testutils provides testing utilities, mocks, and helper functions for the pvetui API package.

This package contains mock implementations of core interfaces, test data generators, and utility functions that make it easier to write comprehensive unit tests for the API client and related components.

Key Components:

  • Mock Implementations: Thread-safe mocks for Logger, Cache, and Config interfaces
  • Test Data Generators: Functions to create test configurations and data
  • Test Utilities: Helper functions for common testing patterns
  • In-Memory Implementations: Simple implementations for testing scenarios

Mock Implementations:

All mock implementations are built using testify/mock and provide:

  • Expectation setting and verification
  • Call recording and replay
  • Thread-safe operation
  • Flexible argument matching

Test Data Generators:

The package provides several functions to generate test data:

  • NewTestConfig(): Basic configuration with sensible defaults
  • NewTestConfigWithToken(): Configuration for API token testing
  • NewTestLogger(): Logger that captures messages for verification
  • NewInMemoryCache(): Simple in-memory cache for testing

Example usage:

func TestAPIClient(t *testing.T) {
	// Create mocks
	mockLogger := &testutils.MockLogger{}
	mockCache := &testutils.MockCache{}
	config := testutils.NewTestConfig()

	// Set expectations
	mockLogger.On("Debug", mock.AnythingOfType("string"), mock.Anything).Return()
	mockCache.On("Get", "test-key", mock.Anything).Return(false, nil)

	// Create client with mocks
	client, err := api.NewClient(config,
		api.WithLogger(mockLogger),
		api.WithCache(mockCache))
	require.NoError(t, err)

	// Test client operations
	// ...

	// Verify expectations
	mockLogger.AssertExpectations(t)
	mockCache.AssertExpectations(t)
}

Thread Safety:

All mock implementations are thread-safe and can be used in concurrent tests. The underlying testify/mock framework handles synchronization automatically.

Best Practices:

  • Always call AssertExpectations() to verify mock usage
  • Use specific argument matchers when possible for better test reliability
  • Reset mocks between test cases if reusing them
  • Use the provided test data generators for consistent test scenarios

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertLogContains

func AssertLogContains(t *testing.T, logger *TestLogger, level string, expectedText string)

AssertLogContains checks if a log message contains the expected text.

Types

type InMemoryCache

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

InMemoryCache is a simple in-memory cache for testing.

func NewInMemoryCache

func NewInMemoryCache() *InMemoryCache

NewInMemoryCache creates a new in-memory cache for testing.

func (*InMemoryCache) Clear

func (c *InMemoryCache) Clear() error

func (*InMemoryCache) Delete

func (c *InMemoryCache) Delete(key string) error

func (*InMemoryCache) Get

func (c *InMemoryCache) Get(key string, dest interface{}) (bool, error)

func (*InMemoryCache) Set

func (c *InMemoryCache) Set(key string, value interface{}, ttl time.Duration) error

type MockCache

type MockCache struct {
	mock.Mock
}

MockCache is a mock implementation of the Cache interface.

func (*MockCache) Clear

func (m *MockCache) Clear() error

func (*MockCache) Delete

func (m *MockCache) Delete(key string) error

func (*MockCache) Get

func (m *MockCache) Get(key string, dest interface{}) (bool, error)

func (*MockCache) Set

func (m *MockCache) Set(key string, value interface{}, ttl time.Duration) error

type MockConfig

type MockConfig struct {
	mock.Mock
}

MockConfig is a mock implementation of the Config interface.

func (*MockConfig) GetAPIToken

func (m *MockConfig) GetAPIToken() string

func (*MockConfig) GetAddr

func (m *MockConfig) GetAddr() string

func (*MockConfig) GetInsecure

func (m *MockConfig) GetInsecure() bool

func (*MockConfig) GetPassword

func (m *MockConfig) GetPassword() string

func (*MockConfig) GetRealm

func (m *MockConfig) GetRealm() string

func (*MockConfig) GetTokenID

func (m *MockConfig) GetTokenID() string

func (*MockConfig) GetTokenSecret

func (m *MockConfig) GetTokenSecret() string

func (*MockConfig) GetUser

func (m *MockConfig) GetUser() string

func (*MockConfig) IsUsingTokenAuth

func (m *MockConfig) IsUsingTokenAuth() bool

type MockLogger

type MockLogger struct {
	mock.Mock
}

MockLogger is a mock implementation of the Logger interface using testify/mock.

This mock provides full expectation setting and verification capabilities for testing code that depends on the Logger interface. All methods support flexible argument matching and call verification.

Example usage:

mockLogger := &MockLogger{}
mockLogger.On("Debug", "Processing user: %s", "john").Return()
mockLogger.On("Error", mock.AnythingOfType("string"), mock.Anything).Return()

// Use mockLogger in your code
// ...

// Verify all expectations were met
mockLogger.AssertExpectations(t)

func (*MockLogger) Debug

func (m *MockLogger) Debug(format string, args ...interface{})

func (*MockLogger) Error

func (m *MockLogger) Error(format string, args ...interface{})

func (*MockLogger) Info

func (m *MockLogger) Info(format string, args ...interface{})

type TestConfig

type TestConfig struct {
	Addr        string
	User        string
	Password    string
	Realm       string
	TokenID     string
	TokenSecret string
	Insecure    bool
}

TestConfig is a simple test implementation of the Config interface.

func NewTestConfig

func NewTestConfig() *TestConfig

NewTestConfig creates a test configuration with sensible defaults.

func NewTestConfigWithToken

func NewTestConfigWithToken() *TestConfig

NewTestConfigWithToken creates a test configuration using token authentication.

func (*TestConfig) GetAPIToken

func (c *TestConfig) GetAPIToken() string

func (*TestConfig) GetAddr

func (c *TestConfig) GetAddr() string

func (*TestConfig) GetInsecure

func (c *TestConfig) GetInsecure() bool

func (*TestConfig) GetPassword

func (c *TestConfig) GetPassword() string

func (*TestConfig) GetRealm

func (c *TestConfig) GetRealm() string

func (*TestConfig) GetTokenID

func (c *TestConfig) GetTokenID() string

func (*TestConfig) GetTokenSecret

func (c *TestConfig) GetTokenSecret() string

func (*TestConfig) GetUser

func (c *TestConfig) GetUser() string

func (*TestConfig) IsUsingTokenAuth

func (c *TestConfig) IsUsingTokenAuth() bool

type TestLogger

type TestLogger struct {
	DebugMessages []string
	InfoMessages  []string
	ErrorMessages []string
}

TestLogger is a simple test logger that captures log messages.

func NewTestLogger

func NewTestLogger() *TestLogger

NewTestLogger creates a new test logger.

func (*TestLogger) Debug

func (l *TestLogger) Debug(format string, args ...interface{})

func (*TestLogger) Error

func (l *TestLogger) Error(format string, args ...interface{})

func (*TestLogger) Info

func (l *TestLogger) Info(format string, args ...interface{})

func (*TestLogger) Reset

func (l *TestLogger) Reset()

Jump to

Keyboard shortcuts

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