testutil

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

Test Utilities Package

This package provides common testing utilities to reduce code duplication across test files in the gorilla DataFrame library.

Purpose

During similarity analysis of the codebase, we identified extensive duplication in test functions:

  • Memory Management Tests: 95-99% similarity
  • SQL Translator Tests: 92-100% similarity
  • Expression Tests: 96-99% similarity
  • DataFrame Creation: 90%+ similarity across 50+ test functions

This package consolidates these patterns into reusable utilities.

Code Duplication Reduction

Before testutil: Each test required 15-25 lines of setup code
After testutil: Tests require 3-6 lines of setup code
Reduction: 60-75% code reduction in test setup and assertions

Core Utilities

Memory Management
// Replace: mem := memory.NewGoAllocator()
mem := testutil.SetupMemoryTest(t)
defer mem.Release()
DataFrame Creation
// Standard employee dataset (name, age, department, salary)
df := testutil.CreateTestDataFrame(mem.Allocator)
defer df.Release()

// Simple 2-column dataset (name, age)
simple := testutil.CreateSimpleTestDataFrame(mem.Allocator)
defer simple.Release()

// Custom configurations
custom := testutil.CreateTestDataFrame(mem.Allocator,
    testutil.WithRowCount(10),
    testutil.WithActiveColumn(),
)
defer custom.Release()
Assertions
// Replace complex DataFrame comparisons
testutil.AssertDataFrameEqual(t, expected, actual)

// Verify DataFrame structure
testutil.AssertDataFrameNotEmpty(t, df)
testutil.AssertDataFrameHasColumns(t, df, []string{"name", "age"})
SQL Testing
// Complete SQL test environment with registered tables
sqlCtx := testutil.SetupSQLTest(t)
defer sqlCtx.Release()

// Simple SQL testing environment
simple := testutil.SetupSimpleSQLTest(t)
defer simple.Release()

// Custom test tables
data := map[string]interface{}{
    "id": []int64{1, 2, 3},
    "name": []string{"A", "B", "C"},
}
table := testutil.CreateTestTableWithData(mem.Allocator, "products", data)
defer table.Release()

Migration Examples

Before (15+ lines)
func TestSomeFeature(t *testing.T) {
    mem := memory.NewGoAllocator()
    
    names := series.New("name", []string{"Alice", "Bob", "Charlie", "David"}, mem)
    ages := series.New("age", []int64{25, 30, 35, 28}, mem)
    departments := series.New("department", []string{"Engineering", "Sales", "Engineering", "Marketing"}, mem)
    salaries := series.New("salary", []int64{100000, 80000, 120000, 75000}, mem)
    
    df := dataframe.New(names, ages, departments, salaries)
    defer df.Release()
    
    require.NotNil(t, df)
    assert.Greater(t, df.Len(), 0)
    assert.Greater(t, df.Width(), 0)
    assert.True(t, df.HasColumn("name"))
    assert.True(t, df.HasColumn("age"))
    
    // ... test logic
}
After (6 lines)
func TestSomeFeature(t *testing.T) {
    mem := testutil.SetupMemoryTest(t)
    defer mem.Release()
    
    df := testutil.CreateTestDataFrame(mem.Allocator)
    defer df.Release()
    
    testutil.AssertDataFrameNotEmpty(t, df)
    testutil.AssertDataFrameHasColumns(t, df, []string{"name", "age", "department", "salary"})
    
    // ... test logic
}

Import Cycle Avoidance

The testutil package is designed to avoid import cycles:

  • ✅ Tests can import testutil
  • ✅ testutil imports series and dataframe packages
  • ❌ Internal packages should not import testutil to avoid cycles

For internal package tests, copy the patterns demonstrated in examples_test.go.

Files

  • testutil.go - Core DataFrame and memory utilities
  • sql.go - SQL-specific test utilities
  • testutil_test.go - Tests for core utilities
  • sql_test.go - Tests for SQL utilities
  • examples_test.go - Migration examples and pattern demonstrations

Benefits

  1. Maintainability: Centralized common patterns
  2. Consistency: Standardized test setup across packages
  3. Productivity: Faster test development with reusable utilities
  4. Quality: Fewer places for test bugs to hide
  5. Readability: Tests focus on business logic, not setup boilerplate

Documentation

Overview

Package testutil provides common testing utilities to reduce code duplication across test files in the gorilla DataFrame library.

This package consolidates common patterns identified in test similarity analysis: - Memory allocator setup and cleanup - Standard test DataFrame creation - Resource lifecycle management - Common test assertions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertDataFrameEqual

func AssertDataFrameEqual(t *testing.T, expected, actual *dataframe.DataFrame)

AssertDataFrameEqual performs deep equality comparison of DataFrames. This consolidates common DataFrame assertion patterns.

func AssertDataFrameHasColumns

func AssertDataFrameHasColumns(t *testing.T, df *dataframe.DataFrame, expectedColumns []string)

AssertDataFrameHasColumns verifies that a DataFrame has the expected columns.

func AssertDataFrameNotEmpty

func AssertDataFrameNotEmpty(t *testing.T, df *dataframe.DataFrame)

AssertDataFrameNotEmpty verifies that a DataFrame is not empty.

func CreateSimpleTestDataFrame

func CreateSimpleTestDataFrame(allocator memory.Allocator) *dataframe.DataFrame

CreateSimpleTestDataFrame creates a simple 2-column DataFrame for basic testing. This is useful for tests that don't need the full employee dataset.

func CreateTestDataFrame

func CreateTestDataFrame(allocator memory.Allocator, opts ...TestDataFrameOption) *dataframe.DataFrame

CreateTestDataFrame creates a standard test DataFrame with employee data. This consolidates the common createTestDataFrame pattern found across multiple test files.

Default DataFrame includes: - name (string): ["Alice", "Bob", "Charlie", "David"] - age (int64): [25, 30, 35, 28] - department (string): ["Engineering", "Sales", "Engineering", "Marketing"] - salary (int64): [100000, 80000, 120000, 75000]

Example usage:

mem := testutil.SetupMemoryTest(t)
defer mem.Release()
df := testutil.CreateTestDataFrame(mem.Allocator)
defer df.Release()

func CreateTestTableWithData

func CreateTestTableWithData(allocator memory.Allocator, data map[string]interface{}) *dataframe.DataFrame

CreateTestTableWithData creates a DataFrame with specific test data for SQL tests. This allows tests to create custom tables beyond the standard employee table.

Types

type SQLTestContext

type SQLTestContext struct {
	Translator *sql.SQLTranslator
	TestTable  *dataframe.DataFrame
	Allocator  memory.Allocator
	// contains filtered or unexported fields
}

SQLTestContext provides a configured SQL translator with test data for SQL tests.

func SetupSQLTest

func SetupSQLTest(t *testing.T) *SQLTestContext

SetupSQLTest creates a configured SQL translator with a test table for SQL tests. This consolidates the common SQL test setup pattern found across SQL test files.

The test table "employees" contains: - name, age, department, salary columns with sample employee data - Suitable for testing SELECT, WHERE, GROUP BY, HAVING operations

Example usage:

sqlCtx := testutil.SetupSQLTest(t)
defer sqlCtx.Release()

stmt, err := sql.ParseSQL("SELECT name FROM employees WHERE age > 30")
lazy, err := sqlCtx.Translator.TranslateStatement(stmt)

func SetupSimpleSQLTest

func SetupSimpleSQLTest(t *testing.T) *SQLTestContext

SetupSimpleSQLTest creates a minimal SQL test context with simple test data. Useful for basic SQL parsing and translation tests.

func (*SQLTestContext) AssertSQLQueryResult

func (ctx *SQLTestContext) AssertSQLQueryResult(t *testing.T, query string, expectedRowCount int, expectedColumns []string) *dataframe.DataFrame

AssertSQLQueryResult executes a SQL query and validates the result structure. This consolidates common SQL test assertion patterns.

func (*SQLTestContext) ExecuteSQLQuery

func (ctx *SQLTestContext) ExecuteSQLQuery(t *testing.T, query string) *dataframe.DataFrame

ExecuteSQLQuery executes a SQL query and returns the result DataFrame. This consolidates the common pattern of parsing -> translating -> collecting SQL queries.

func (*SQLTestContext) Release

func (ctx *SQLTestContext) Release()

Release cleans up the SQL test context.

type TestDataFrameOption

type TestDataFrameOption func(*testDataFrameConfig)

TestDataFrameOption configures test DataFrame creation.

func WithActiveColumn

func WithActiveColumn() TestDataFrameOption

WithActiveColumn includes an 'active' boolean column.

func WithNulls

func WithNulls() TestDataFrameOption

WithNulls includes null values in test data.

func WithRowCount

func WithRowCount(count int) TestDataFrameOption

WithRowCount sets the number of rows in test data.

type TestMemoryContext

type TestMemoryContext struct {
	Allocator memory.Allocator
	// contains filtered or unexported fields
}

TestMemoryContext provides memory allocator with automatic cleanup.

func SetupMemoryTest

func SetupMemoryTest(tb testing.TB) *TestMemoryContext

SetupMemoryTest creates a memory allocator with automatic cleanup for tests. Returns a TestMemoryContext that should be released with defer.

Example usage:

mem := testutil.SetupMemoryTest(t)
defer mem.Release()

func (*TestMemoryContext) Release

func (tmc *TestMemoryContext) Release()

Release performs cleanup of the memory context.

Jump to

Keyboard shortcuts

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