mocks

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: MIT Imports: 4 Imported by: 0

README

Test Mocks

This directory contains mock implementations for testing Weave CLI components.

Available Mocks

MockVectorDBClient

Mock implementation of vectordb.VectorDBClient interface.

Usage:

import (
    "testing"
    "github.com/maximilien/weave-cli/tests/mocks"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
)

func TestYourFunction(t *testing.T) {
    // Create mock
    mockClient := new(mocks.MockVectorDBClient)

    // Set expectations
    mockClient.On("Health", mock.Anything).Return(nil)
    mockClient.On("CreateCollection", mock.Anything, "test", mock.Anything).
        Return(nil)

    // Use in your code
    err := mockClient.Health(context.Background())
    assert.NoError(t, err)

    // Assert all expectations were met
    mockClient.AssertExpectations(t)
}
MockLLMClient

Mock implementation of llm.Client interface for testing embedding generation and LLM interactions.

Usage:

func TestEmbeddingGeneration(t *testing.T) {
    mockLLM := new(mocks.MockLLMClient)

    // Mock embedding generation
    embedding := []float64{0.1, 0.2, 0.3}
    mockLLM.On("GenerateEmbedding", mock.Anything, "test text", "").
        Return(embedding, nil)

    result, err := mockLLM.GenerateEmbedding(context.Background(), "test text", "")
    assert.NoError(t, err)
    assert.Equal(t, embedding, result)

    mockLLM.AssertExpectations(t)
}

Mock Patterns

Basic Mock Setup
mock := new(mocks.MockVectorDBClient)
mock.On("MethodName", arg1, arg2).Return(returnValue, error)
Argument Matchers
// Any argument
mock.On("Method", mock.Anything).Return(nil)

// Specific type
mock.On("Method", mock.AnythingOfType("string")).Return(nil)

// Custom matcher
mock.On("Method", mock.MatchedBy(func(arg string) bool {
    return len(arg) > 0
})).Return(nil)
Return Values
// Simple return
mock.On("Method").Return(value, nil)

// Error return
mock.On("Method").Return(nil, errors.New("test error"))

// Multiple calls with different returns
mock.On("Method").Return(value1, nil).Once()
mock.On("Method").Return(value2, nil).Once()
Verification
// Assert specific method was called
mock.AssertCalled(t, "MethodName", arg1, arg2)

// Assert method was not called
mock.AssertNotCalled(t, "MethodName")

// Assert all expectations
mock.AssertExpectations(t)

Testing Best Practices

  1. Isolate Units: Use mocks to test components in isolation
  2. Test Error Paths: Mock error returns to test error handling
  3. Verify Interactions: Always call AssertExpectations() to verify mocks were used correctly
  4. Clean Setup: Use test helpers to reduce mock setup boilerplate
  5. Meaningful Assertions: Assert on important behaviors, not implementation details

Adding New Mocks

When adding new mocks:

  1. Create a new file: tests/mocks/your_interface.go
  2. Implement all interface methods using mock.Mock
  3. Use mock.Called() and return appropriate values
  4. Document usage in this README
  5. Add examples to help other developers

Dependencies

This package requires:

  • github.com/stretchr/testify/mock - Mock framework
  • github.com/stretchr/testify/assert - Assertions

Install with:

go get github.com/stretchr/testify

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockLLMClient

type MockLLMClient struct {
	mock.Mock
}

MockLLMClient is a mock implementation of llm.Client for testing

func (*MockLLMClient) Complete

func (m *MockLLMClient) Complete(ctx context.Context, prompt string, opts ...llm.Option) (string, error)

Complete mocks the Complete method

func (*MockLLMClient) CompleteStructured

func (m *MockLLMClient) CompleteStructured(ctx context.Context, prompt string, schema interface{}, opts ...llm.Option) (interface{}, error)

CompleteStructured mocks the CompleteStructured method

func (*MockLLMClient) GetMetrics

func (m *MockLLMClient) GetMetrics() *llm.Metrics

GetMetrics mocks the GetMetrics method

type MockVectorDBClient

type MockVectorDBClient struct {
	mock.Mock
}

MockVectorDBClient is a mock implementation of vectordb.VectorDBClient for testing

func (*MockVectorDBClient) CollectionExists

func (m *MockVectorDBClient) CollectionExists(ctx context.Context, name string) (bool, error)

CollectionExists mocks the CollectionExists method

func (*MockVectorDBClient) CreateCollection

func (m *MockVectorDBClient) CreateCollection(ctx context.Context, name string, schema *vectordb.CollectionSchema) error

CreateCollection mocks the CreateCollection method

func (*MockVectorDBClient) CreateDocument

func (m *MockVectorDBClient) CreateDocument(ctx context.Context, collectionName string, document *vectordb.Document) error

CreateDocument mocks the CreateDocument method

func (*MockVectorDBClient) CreateDocuments

func (m *MockVectorDBClient) CreateDocuments(ctx context.Context, collectionName string, documents []*vectordb.Document) error

CreateDocuments mocks the CreateDocuments method

func (*MockVectorDBClient) DeleteCollection

func (m *MockVectorDBClient) DeleteCollection(ctx context.Context, name string) error

DeleteCollection mocks the DeleteCollection method

func (*MockVectorDBClient) DeleteDocument

func (m *MockVectorDBClient) DeleteDocument(ctx context.Context, collectionName, documentID string) error

DeleteDocument mocks the DeleteDocument method

func (*MockVectorDBClient) DeleteDocuments

func (m *MockVectorDBClient) DeleteDocuments(ctx context.Context, collectionName string, documentIDs []string) error

DeleteDocuments mocks the DeleteDocuments method

func (*MockVectorDBClient) DeleteDocumentsByMetadata

func (m *MockVectorDBClient) DeleteDocumentsByMetadata(ctx context.Context, collectionName string, metadata map[string]interface{}) error

DeleteDocumentsByMetadata mocks the DeleteDocumentsByMetadata method

func (*MockVectorDBClient) GetCollectionCount

func (m *MockVectorDBClient) GetCollectionCount(ctx context.Context, name string) (int64, error)

GetCollectionCount mocks the GetCollectionCount method

func (*MockVectorDBClient) GetDefaultSchema

func (m *MockVectorDBClient) GetDefaultSchema(schemaType vectordb.SchemaType, collectionName string) *vectordb.CollectionSchema

GetDefaultSchema mocks the GetDefaultSchema method

func (*MockVectorDBClient) GetDocument

func (m *MockVectorDBClient) GetDocument(ctx context.Context, collectionName, documentID string) (*vectordb.Document, error)

GetDocument mocks the GetDocument method

func (*MockVectorDBClient) GetSchema

func (m *MockVectorDBClient) GetSchema(ctx context.Context, collectionName string) (*vectordb.CollectionSchema, error)

GetSchema mocks the GetSchema method

func (*MockVectorDBClient) Health

func (m *MockVectorDBClient) Health(ctx context.Context) error

Health mocks the Health method

func (*MockVectorDBClient) ListCollections

func (m *MockVectorDBClient) ListCollections(ctx context.Context) ([]vectordb.CollectionInfo, error)

ListCollections mocks the ListCollections method

func (*MockVectorDBClient) ListDocuments

func (m *MockVectorDBClient) ListDocuments(ctx context.Context, collectionName string, limit int, offset int) ([]*vectordb.Document, error)

ListDocuments mocks the ListDocuments method

func (*MockVectorDBClient) SearchBM25

func (m *MockVectorDBClient) SearchBM25(ctx context.Context, collectionName, query string, options *vectordb.QueryOptions) ([]*vectordb.QueryResult, error)

SearchBM25 mocks the SearchBM25 method

func (*MockVectorDBClient) SearchByMetadata

func (m *MockVectorDBClient) SearchByMetadata(ctx context.Context, collectionName string, metadata map[string]interface{}, options *vectordb.QueryOptions) ([]*vectordb.QueryResult, error)

SearchByMetadata mocks the SearchByMetadata method

func (*MockVectorDBClient) SearchHybrid

func (m *MockVectorDBClient) SearchHybrid(ctx context.Context, collectionName, query string, options *vectordb.QueryOptions) ([]*vectordb.QueryResult, error)

SearchHybrid mocks the SearchHybrid method

func (*MockVectorDBClient) SearchSemantic

func (m *MockVectorDBClient) SearchSemantic(ctx context.Context, collectionName, query string, options *vectordb.QueryOptions) ([]*vectordb.QueryResult, error)

SearchSemantic mocks the SearchSemantic method

func (*MockVectorDBClient) UpdateDocument

func (m *MockVectorDBClient) UpdateDocument(ctx context.Context, collectionName string, document *vectordb.Document) error

UpdateDocument mocks the UpdateDocument method

func (*MockVectorDBClient) UpdateSchema

func (m *MockVectorDBClient) UpdateSchema(ctx context.Context, collectionName string, schema *vectordb.CollectionSchema) error

UpdateSchema mocks the UpdateSchema method

func (*MockVectorDBClient) ValidateSchema

func (m *MockVectorDBClient) ValidateSchema(schema *vectordb.CollectionSchema) error

ValidateSchema mocks the ValidateSchema method

Jump to

Keyboard shortcuts

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