testutil

package
v1.0.77 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 18 Imported by: 0

README

Test Utilities

This package provides utilities and helpers for writing tests.

Available Utilities

Setup (setup.go)
  • GetTestConfig() - Load test configuration
  • SkipIfShort() - Skip test in short mode
  • SkipIfNoPostgres() - Skip if PostgreSQL unavailable
  • SkipIfNoRedis() - Skip if Redis unavailable
Database (db.go)
  • SetupTestDB() - Create test database connection
  • CleanupTestDB() - Clean up test database
  • CreateTestUser() - Create a test user
  • CreateTestAPIKey() - Create a test API key
HTTP (http.go)
  • CreateTestServer() - Create test HTTP server
  • MakeRequest() - Make HTTP request
  • ParseResponse() - Parse JSON response
  • AssertStatusCode() - Assert status code
  • CreateAuthToken() - Create test JWT token
Mocks (mocks.go)
  • CreateMockEmailService() - Create mock email service
  • MockEmailService - Mock email service implementation

Usage Example

package integration

import (
    "testing"
    "github.com/Kizsoft-Solution-Limited/uniroute/tests/testutil"
)

func TestAuthFlow(t *testing.T) {
    // Setup
    db := testutil.SetupTestDB(t)
    defer testutil.CleanupTestDB(t, db)
    
    // Create test user
    userID := testutil.CreateTestUser(t, db, "test@example.com", "password", "Test User")
    
    // Create test server
    server := testutil.CreateTestServer(t, func(r *gin.Engine) {
        // Setup your router
    })
    defer server.Close()
    
    // Make request
    resp := testutil.MakeRequest(t, "POST", server.URL+"/auth/login", 
        map[string]string{"email": "test@example.com", "password": "password"}, "")
    
    // Assert
    testutil.AssertStatusCode(t, resp, 200)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertStatusCode

func AssertStatusCode(t *testing.T, resp *http.Response, expected int)

AssertStatusCode asserts the response status code

func CleanupTestDB

func CleanupTestDB(t *testing.T, pool *pgxpool.Pool)

CleanupTestDB cleans up test database

func CreateAuthToken

func CreateAuthToken(t *testing.T, userID, email string, roles []string) string

CreateAuthToken creates a test JWT token

func CreateMockEmailService

func CreateMockEmailService(t *testing.T) *email.EmailService

CreateMockEmailService creates a mock email service

func CreateTestAPIKey

func CreateTestAPIKey(t *testing.T, pool *pgxpool.Pool, userID uuid.UUID, name string) uuid.UUID

CreateTestAPIKey creates a test API key

func CreateTestUser

func CreateTestUser(t *testing.T, pool *pgxpool.Pool, email, password, name string) uuid.UUID

CreateTestUser creates a test user in the database

func LoadFixture

func LoadFixture(t *testing.T, filename string, key string) map[string]interface{}

LoadFixture loads a fixture from the fixtures directory

func LoadFixtureRaw

func LoadFixtureRaw(t *testing.T, filename string) []byte

LoadFixtureRaw loads a fixture file as raw bytes

func MakeRequest

func MakeRequest(t *testing.T, method, url string, body interface{}, token string) *http.Response

MakeRequest makes an HTTP request to the test server

func ParseResponse

func ParseResponse(t *testing.T, resp *http.Response, v interface{})

ParseResponse parses JSON response body

func SetupTestDB

func SetupTestDB(t *testing.T) *pgxpool.Pool

SetupTestDB creates a test database connection

func SkipIfNoPostgres

func SkipIfNoPostgres(t *testing.T)

SkipIfNoPostgres skips the test if PostgreSQL is not available

func SkipIfNoRedis

func SkipIfNoRedis(t *testing.T)

SkipIfNoRedis skips the test if Redis is not available

func SkipIfShort

func SkipIfShort(t *testing.T)

SkipIfShort skips the test if -short flag is set

Types

type MockEmailService

type MockEmailService struct {
	SentEmails []SentEmail
}

MockEmailService is a mock implementation of email service This can be extended to track sent emails for testing

func (*MockEmailService) ClearSentEmails

func (m *MockEmailService) ClearSentEmails()

ClearSentEmails clears the sent emails list

func (*MockEmailService) GetSentEmails

func (m *MockEmailService) GetSentEmails() []SentEmail

GetSentEmails returns all sent emails

func (*MockEmailService) SendEmail

func (m *MockEmailService) SendEmail(to, subject, body string) error

SendEmail mocks sending an email

type SentEmail

type SentEmail struct {
	To      string
	Subject string
	Body    string
}

type TestConfig

type TestConfig struct {
	PostgresURL string
	RedisURL    string
	JWTSecret   string
	FrontendURL string
}

TestConfig holds test configuration

func GetTestConfig

func GetTestConfig(t *testing.T) *TestConfig

GetTestConfig loads test configuration from environment or defaults

type TestServer

type TestServer struct {
	Server *httptest.Server
	Router *gin.Engine
}

TestServer wraps a test HTTP server

func CreateTestServer

func CreateTestServer(t *testing.T, setupRouter func(*gin.Engine)) *TestServer

CreateTestServer creates a test HTTP server

func (*TestServer) Close

func (ts *TestServer) Close()

Close closes the test server

Jump to

Keyboard shortcuts

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