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 ¶
- type APIClient
- func (c *APIClient) DELETE(path string) *Response
- func (c *APIClient) GET(path string) *Response
- func (c *APIClient) PATCH(path string, body interface{}) *Response
- func (c *APIClient) POST(path string, body interface{}) *Response
- func (c *APIClient) PUT(path string, body interface{}) *Response
- func (c *APIClient) WithAuth(token string) *APIClient
- func (c *APIClient) WithBasicAuth(username, password string) *APIClient
- func (c *APIClient) WithHeader(key, value string) *APIClient
- type Factory
- type FixtureManager
- func (f *FixtureManager) Clear() error
- func (f *FixtureManager) ClearAll() error
- func (f *FixtureManager) ClearTable(tableName string) error
- func (f *FixtureManager) Count(tableName string) int
- func (f *FixtureManager) Get(tableName string, index int) (interface{}, error)
- func (f *FixtureManager) Load(records ...interface{}) error
- func (f *FixtureManager) LoadMany(records interface{}) error
- type IntegrationSuite
- type Response
- func (r *Response) AssertError(t TestingT) *Response
- func (r *Response) AssertJSON(t TestingT, expected interface{}) *Response
- func (r *Response) AssertStatus(t TestingT, expected int) *Response
- func (r *Response) AssertSuccess(t TestingT) *Response
- func (r *Response) Error() error
- func (r *Response) GetHeader(key string) string
- func (r *Response) IsError() bool
- func (r *Response) IsSuccess() bool
- func (r *Response) JSON(target interface{}) error
- func (r *Response) Status() int
- func (r *Response) String() string
- type Suite
- func (s *Suite) AssertRecordCount(t TestingT, model interface{}, expected int64, conditions ...interface{})
- func (s *Suite) AssertRecordExists(t TestingT, model interface{}, conditions ...interface{})
- func (s *Suite) AssertRecordNotExists(t TestingT, model interface{}, conditions ...interface{})
- func (s *Suite) LoadFixtures(records ...interface{}) error
- func (s *Suite) Setup() error
- func (s *Suite) Teardown() error
- func (s *Suite) WithAuth(token string) *APIClient
- type TestingT
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 ¶
NewAPIClient creates a new API testing client
func (*APIClient) WithBasicAuth ¶
WithBasicAuth sets the Authorization header with Basic auth
func (*APIClient) WithHeader ¶
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 ¶
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]) CreateMany ¶
CreateMany creates multiple entities
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 ¶
AssertError checks if the response is an error (4xx or 5xx)
func (*Response) AssertJSON ¶
AssertJSON checks if the response body matches the expected JSON
func (*Response) AssertStatus ¶
AssertStatus checks if the response has the expected status code
func (*Response) AssertSuccess ¶
AssertSuccess checks if the response is successful (2xx)
type Suite ¶
Suite provides a base test suite with common setup/teardown
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 ¶
AssertRecordExists checks if a record exists in the database
func (*Suite) AssertRecordNotExists ¶
AssertRecordNotExists checks if a record does not exist in the database
func (*Suite) LoadFixtures ¶
LoadFixtures is a convenience method to load test fixtures