testing

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package testing provides optional testing utilities for EchoNext applications. This is a contrib package and is completely optional - you can use standard testing if preferred.

Package testing provides optional testing utilities for EchoNext applications.

This is a contrib package and is completely optional. You can use standard testing if you prefer, or use these helpers to reduce boilerplate code in your tests.

Features:

  • APIClient for testing HTTP endpoints
  • FixtureManager for managing test data
  • Factory pattern for creating test entities
  • Suite base class with setup/teardown
  • IntegrationSuite with transaction rollback

Example usage:

import (
    "testing"
    "github.com/abdussamadbello/echonext"
    echonexttest "github.com/abdussamadbello/echonext/pkg/contrib/testing"
)

func TestUserAPI(t *testing.T) {
    app := echonext.New()
    // Register your routes...

    client := echonexttest.NewAPIClient(app)

    // Test GET request
    resp := client.GET("/users/1")
    resp.AssertStatus(t, 200)

    var user User
    resp.JSON(&user)
    if user.ID != 1 {
        t.Errorf("Expected user ID 1, got %d", user.ID)
    }

    // Test POST request
    newUser := CreateUserRequest{Name: "John", Email: "john@example.com"}
    resp = client.POST("/users", newUser)
    resp.AssertStatus(t, 201)
}

Using fixtures:

func TestWithFixtures(t *testing.T) {
    fixtures := echonexttest.NewFixtureManager(db)
    defer fixtures.Clear()

    // Load test data
    fixtures.Load(
        &User{ID: 1, Name: "Alice"},
        &User{ID: 2, Name: "Bob"},
    )

    // Run tests...
}

Using test suite:

type UserTestSuite struct {
    echonexttest.Suite
}

func TestUserSuite(t *testing.T) {
    suite := echonexttest.NewSuite(app, db)
    // Run your tests with suite...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIClient

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

APIClient provides a convenient way to test EchoNext APIs

func NewAPIClient

func NewAPIClient(app *echonext.App) *APIClient

NewAPIClient creates a new API testing client

func (*APIClient) DELETE

func (c *APIClient) DELETE(path string) *Response

DELETE sends a DELETE request

func (*APIClient) GET

func (c *APIClient) GET(path string) *Response

GET sends a GET request and returns the response

func (*APIClient) PATCH

func (c *APIClient) PATCH(path string, body interface{}) *Response

PATCH sends a PATCH request with the given body

func (*APIClient) POST

func (c *APIClient) POST(path string, body interface{}) *Response

POST sends a POST request with the given body

func (*APIClient) PUT

func (c *APIClient) PUT(path string, body interface{}) *Response

PUT sends a PUT request with the given body

func (*APIClient) WithAuth

func (c *APIClient) WithAuth(token string) *APIClient

WithAuth sets the Authorization header with a Bearer token

func (*APIClient) WithBasicAuth

func (c *APIClient) WithBasicAuth(username, password string) *APIClient

WithBasicAuth sets the Authorization header with Basic auth

func (*APIClient) WithHeader

func (c *APIClient) WithHeader(key, value string) *APIClient

WithHeader adds a header to all requests

type Factory

type Factory[T any] struct {
	// contains filtered or unexported fields
}

Factory provides a fluent interface for creating test data

func NewFactory

func NewFactory[T any](db *gorm.DB, build func() T) *Factory[T]

NewFactory creates a new factory for the given model type

func (*Factory[T]) Build

func (f *Factory[T]) Build() *T

Build creates a new entity without persisting it

func (*Factory[T]) BuildMany

func (f *Factory[T]) BuildMany(count int) []*T

BuildMany creates multiple entities without persisting them

func (*Factory[T]) Create

func (f *Factory[T]) Create() (*T, error)

Create creates and persists a new entity

func (*Factory[T]) CreateMany

func (f *Factory[T]) CreateMany(count int) ([]*T, error)

CreateMany creates multiple entities

func (*Factory[T]) With

func (f *Factory[T]) With(field string, value interface{}) *Factory[T]

With sets field overrides for the factory

type FixtureManager

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

FixtureManager manages test fixtures for database testing

func NewFixtureManager

func NewFixtureManager(db *gorm.DB) *FixtureManager

NewFixtureManager creates a new fixture manager

func (*FixtureManager) Clear

func (f *FixtureManager) Clear() error

Clear removes all loaded fixtures from the database

func (*FixtureManager) ClearAll

func (f *FixtureManager) ClearAll() error

ClearAll truncates all tables (use with caution!)

func (*FixtureManager) ClearTable

func (f *FixtureManager) ClearTable(tableName string) error

ClearTable truncates a specific table

func (*FixtureManager) Count

func (f *FixtureManager) Count(tableName string) int

Count returns the number of loaded fixtures for a table

func (*FixtureManager) Get

func (f *FixtureManager) Get(tableName string, index int) (interface{}, error)

Get retrieves a loaded fixture by type and index

func (*FixtureManager) Load

func (f *FixtureManager) Load(records ...interface{}) error

Load inserts fixtures into the database

func (*FixtureManager) LoadMany

func (f *FixtureManager) LoadMany(records interface{}) error

LoadMany inserts multiple records of the same type

type IntegrationSuite

type IntegrationSuite struct {
	Suite
	// contains filtered or unexported fields
}

IntegrationSuite extends Suite with transaction rollback for each test

func NewIntegrationSuite

func NewIntegrationSuite(app *echonext.App, db *gorm.DB) *IntegrationSuite

NewIntegrationSuite creates a new integration test suite with transaction support

func (*IntegrationSuite) BeginTx

func (s *IntegrationSuite) BeginTx() error

BeginTx starts a transaction before each test

func (*IntegrationSuite) RollbackTx

func (s *IntegrationSuite) RollbackTx() error

RollbackTx rolls back the transaction after each test

type Response

type Response struct {
	StatusCode int
	Body       []byte
	Header     http.Header
	// contains filtered or unexported fields
}

Response represents an HTTP response for testing

func (*Response) AssertError

func (r *Response) AssertError(t TestingT) *Response

AssertError checks if the response is an error (4xx or 5xx)

func (*Response) AssertJSON

func (r *Response) AssertJSON(t TestingT, expected interface{}) *Response

AssertJSON checks if the response body matches the expected JSON

func (*Response) AssertStatus

func (r *Response) AssertStatus(t TestingT, expected int) *Response

AssertStatus checks if the response has the expected status code

func (*Response) AssertSuccess

func (r *Response) AssertSuccess(t TestingT) *Response

AssertSuccess checks if the response is successful (2xx)

func (*Response) Error

func (r *Response) Error() error

Error returns any error that occurred during the request

func (*Response) GetHeader

func (r *Response) GetHeader(key string) string

GetHeader returns the value of a response header

func (*Response) IsError

func (r *Response) IsError() bool

IsError returns true if the status code is 4xx or 5xx

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

IsSuccess returns true if the status code is 2xx

func (*Response) JSON

func (r *Response) JSON(target interface{}) error

JSON unmarshals the response body into the target

func (*Response) Status

func (r *Response) Status() int

Status returns the HTTP status code

func (*Response) String

func (r *Response) String() string

String returns the response body as a string

type Suite

type Suite struct {
	App      *echonext.App
	DB       *gorm.DB
	Client   *APIClient
	Fixtures *FixtureManager
}

Suite provides a base test suite with common setup/teardown

func NewSuite

func NewSuite(app *echonext.App, db *gorm.DB) *Suite

NewSuite creates a new test suite

func (*Suite) AssertRecordCount

func (s *Suite) AssertRecordCount(t TestingT, model interface{}, expected int64, conditions ...interface{})

AssertRecordCount checks the count of records matching conditions

func (*Suite) AssertRecordExists

func (s *Suite) AssertRecordExists(t TestingT, model interface{}, conditions ...interface{})

AssertRecordExists checks if a record exists in the database

func (*Suite) AssertRecordNotExists

func (s *Suite) AssertRecordNotExists(t TestingT, model interface{}, conditions ...interface{})

AssertRecordNotExists checks if a record does not exist in the database

func (*Suite) LoadFixtures

func (s *Suite) LoadFixtures(records ...interface{}) error

LoadFixtures is a convenience method to load test fixtures

func (*Suite) Setup

func (s *Suite) Setup() error

Setup is called before each test (override in your suite)

func (*Suite) Teardown

func (s *Suite) Teardown() error

Teardown is called after each test (override in your suite)

func (*Suite) WithAuth

func (s *Suite) WithAuth(token string) *APIClient

WithAuth returns a client with authentication

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Failed() bool
}

TestingT is an interface that matches the standard testing.T

Jump to

Keyboard shortcuts

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