testing

package
v0.65.0 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package testing provides comprehensive testing utilities for the go-bricks framework.

This package contains mocks and fixtures that enable developers to write effective unit and integration tests for applications built with go-bricks.

Mocks

The mocks subpackage provides testify-based mock implementations for all major framework interfaces:

  • Database operations (database.Interface, database.Statement, database.Tx)
  • Messaging operations (messaging.Client, messaging.AMQPClient, messaging.RegistryInterface)

Fixtures

The fixtures subpackage provides helper functions and pre-configured mocks for common testing scenarios:

  • Database fixtures with common behaviors (healthy, failing, with data)
  • Messaging fixtures for simulating message flows
  • SQL result builders for consistent test data

Usage

Import the specific subpackages you need:

import (
	"github.com/gaborage/go-bricks/testing/mocks"
	"github.com/gaborage/go-bricks/testing/fixtures"
)

For comprehensive examples and usage patterns, see the go-bricks-demo-project repository (https://github.com/gaborage/go-bricks-demo-project) and TESTING.md documentation.

Index

Constants

View Source
const (
	// TestLoggerLevelDebug is the debug log level used in most tests
	TestLoggerLevelDebug = "debug"
	// TestLoggerLevelError is the error log level for tests requiring minimal output
	TestLoggerLevelError = "error"
	// TestLoggerLevelDisabled completely disables logging in tests
	TestLoggerLevelDisabled = "disabled"
)

Logger Constants These constants define common logger configurations used across test files to avoid duplicating logger initialization strings.

View Source
const (
	TestServiceName  = "test-service"
	TestCacheName    = "test-cache"
	TestKeyName      = "test-key"
	TestValueDefault = "test-value"
)

Service and Component Names Common service/component name constants available for test configurations.

View Source
const (
	TestQueueName    = "test-queue"
	TestExchangeName = "test-exchange"
	TestRoutingKey   = "test-routing-key"
	TestConsumerTag  = "test-consumer"
)

Messaging Constants Common queue, exchange, and routing key names for messaging tests.

View Source
const (
	TestTableUsers      = "users"
	TestTableProducts   = "products"
	TestTableOrders     = "orders"
	TestUsername        = "testuser"
	TestDatabaseName    = "testdb"
	TestHostLocalhost   = "localhost"
	TestPasswordDefault = "testpass"
	TestStatusActive    = "active"
	TestStatusInactive  = "inactive"
	TestSelectQuery     = "SELECT"
	TestInsertQuery     = "INSERT"
	TestUpdateQuery     = "UPDATE"
	TestDeleteQuery     = "DELETE"
)

Database Constants Common database-related test strings (table names, queries, connection strings).

View Source
const (
	TestEmailAlice = "alice@example.com"
	TestEmailBob   = "bob@example.com"
	TestNameAlice  = "Alice"
	TestNameBob    = "Bob"
)

Test User Data Common test user data constants available for test suites.

View Source
const (
	TestTenantOne    = "tenant-1"
	TestTenantTwo    = "tenant-2"
	TestTenantThree  = "tenant-3"
	TestTenantAcme   = "acme"
	TestTenantGlobex = "globex"
)

Multi-Tenant Constants Common tenant identifiers for multi-tenant testing.

View Source
const (
	TestTracerName = "test"
	TestMeterName  = "test-meter"
	TestSpanName   = "test-span"
)

OpenTelemetry Constants Common names for tracing, metrics, and observability tests.

View Source
const (
	// TestShortDelay is a short delay for goroutine synchronization (100ms)
	TestShortDelay = 100 * time.Millisecond
	// TestMediumDelay is a medium delay for async operations (300ms)
	TestMediumDelay = 300 * time.Millisecond
	// TestLongDelay is a longer delay for slow operations (1 second)
	TestLongDelay = 1 * time.Second
	// TestCleanupWaitPeriod is the wait time for cleanup operations (300ms)
	TestCleanupWaitPeriod = 300 * time.Millisecond
	// TestEventuallyTimeout is the timeout for require.Eventually assertions (500ms)
	TestEventuallyTimeout = 500 * time.Millisecond
	// TestEventuallyTick is the polling interval for require.Eventually (50ms)
	TestEventuallyTick = 50 * time.Millisecond
)

Time Duration Constants Common time duration constants available for test synchronization and timeouts.

View Source
const (
	TestPortRedis      = 6379
	TestPortHTTP       = 8080
	TestPortPostgreSQL = 5432
)

Port Numbers Common port numbers for test services.

View Source
const (
	TestVarcharSize           = 100
	TestBytesPerKB            = 1024
	TestBenchmarkLargePayload = 100 * 1024 // 100KB
	TestBenchmarkSmallPayload = 256        // 256 bytes
)

Size Constants Common size values for buffers, payloads, and varchar lengths.

Variables

This section is empty.

Functions

func CAAndServerCertPEM added in v0.65.0

func CAAndServerCertPEM(t testing.TB, hosts ...string) (caPEM, serverCertPEM, serverKeyPEM []byte)

CAAndServerCertPEM mints a throwaway P-256 CA and a server certificate signed by it, all three PEM-encoded ("CERTIFICATE", "CERTIFICATE" and "EC PRIVATE KEY") and valid for one hour. The leaf carries ExtKeyUsageServerAuth and one SAN per host — a DNS name for anything that is not an IP literal, an IP SAN for anything that parses as one. With no hosts it covers DefaultCertHosts.

It is the fixture for a real TLS handshake: caPEM is what the client trusts, serverCertPEM/serverKeyPEM are what the server presents.

func DefaultCertHosts added in v0.65.0

func DefaultCertHosts() []string

DefaultCertHosts returns the SANs a server certificate gets when the caller names none. A caller that has to decide in advance whether an address is covered (a container host resolved only after start, say) reads them from here instead of repeating the list.

func FakePassword added in v0.60.0

func FakePassword(label string) string

FakePassword composes a synthetic password from a label. It is a test fixture only — the value is deterministic and published in this repository, so it must never stand in for a real credential. The label keeps values distinct where a test rotates one; every result is well past config.MinDatabasePasswordLength, under which migration's redactPassword suppresses Flyway output wholesale.

func NewCAAndServerCertPEM added in v0.65.0

func NewCAAndServerCertPEM(hosts ...string) (caPEM, serverCertPEM, serverKeyPEM []byte, err error)

NewCAAndServerCertPEM is CAAndServerCertPEM for callers that have no testing.TB to fail: a TestMain-style container starter, say. It returns the same three PEM blocks and any minting error instead of aborting the test. A fixture only: the throwaway CA and key it mints are for tests, and must never be used to produce production key material.

func PEMFixture added in v0.60.0

func PEMFixture(blockType string) []byte

PEMFixture returns the smallest well-formed PEM block of the given type, with a body that decodes to "foo". Byte-identical to the equivalent literal.

func SelfSignedCert added in v0.62.0

func SelfSignedCert(t testing.TB, key crypto.Signer) *x509.Certificate

SelfSignedCert issues a minimal self-signed leaf certificate for key, valid for one hour. A fixture only: tests that need a certificate to pair with a key (PKCS#12 bundles, TLS) build one here instead of hand-rolling x509 templates.

func SelfSignedCertKeyPEM added in v0.65.0

func SelfSignedCertKeyPEM(t testing.TB) (certPEM, keyPEM []byte)

SelfSignedCertKeyPEM issues a fresh P-256 self-signed certificate and returns it with its private key, both PEM-encoded ("CERTIFICATE" and "EC PRIVATE KEY" blocks) and ready for tls.X509KeyPair. Every call mints a new key, so two calls yield two distinct certificates.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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