DropGardenロゴ

testenv package - github.com/surrealdb/surrealdb.go/contrib/testenv - Go Packages

Documentation

Overview

Package testenv provides utilities for testing the SurrealDB Go SDK and SurrealDB.

It includes functions to create connections to SurrealDB instances over WebSocket and HTTP, as well as helper functions for testing purposes.

Index

Constants

View Source

const (
    // DefaultWSURL is the default WebSocket URL for SurrealDB.
    DefaultWSURL = "ws://localhost:8000"

    // EnvWSURL is the environment variable that specifies the SurrealDB WebSocket URL.
    // If not set, it defaults to DefaultWSURL.
    EnvWSURL = "SURREALDB_URL"

    // EnvReconnectionCheckInterval is the environment variable that specifies the
    // reconnection check interval for WebSocket connections.
    EnvReconnectionCheckInterval = "SURREALDB_RECONNECTION_CHECK_INTERVAL"

    // EnvSurrealDBConnectionImpl is the environment variable that specifies
    // the SurrealDB connection implementation to use.
    // If set to "gws", it uses the gws package; otherwise, it
    // defaults to the gorillaws package.
    EnvSurrealDBConnectionImpl = "SURREALDB_CONNECTION_IMPL"

    // EnvSurrealCBORImpl is the environment variable that specifies
    // the SurrealDB CBOR implementation to use.
    EnvSurrealCBORImpl = "SURREALDB_CBOR_IMPL"
)

Variables

This section is empty.

Functions

added in v0.9.0

func GetSurrealDBURL() string

func GetSurrealDBWSURL() string

added in v0.9.0

Init initializes the testing environment. It cleans up the specified tables in the namespace/database. If no tables are specified, it will clean up all tables in the database.

func MustNew(namespace, database string, tables ...string) *surrealdb.DB

added in v0.10.0

func MustParseSurrealDBURL() *url.URL

added in v0.10.0

func MustParseSurrealDBWSURL() *url.URL

func New(namespace, database string, tables ...string) (*surrealdb.DB, error)

New creates a new SurrealDB connection with the specified database and tables. The connection information is derived from environment variables. It supports both WebSocket and HTTP connections based on the URL scheme.

Types

added in v0.11.0

type CBORImpl int

CBORImpl specifies which CBOR implementation to use

const (
    // CBORImplDefault uses the default implementation from connection.NewConfig (currently surrealcbor)
    CBORImplDefault CBORImpl = iota
    // CBORImplSurrealCBOR explicitly uses the surrealcbor implementation
    CBORImplSurrealCBOR
    // CBORImplFxamackerCBOR explicitly uses the fxamacker/cbor implementation
    CBORImplFxamackerCBOR
)

added in v0.11.0

func (c CBORImpl) String() string

String returns the string representation of CBORImpl

added in v0.9.0

type Config struct {
    // Endpoint is the SurrealDB endpoint URL.
    Endpoint string

    Namespace string
    Database  string
    Tables    []string

    // ReconnectDuration is the duration to wait before attempting to reconnect
    // to the SurrealDB instance after a disconnection.
    ReconnectDuration time.Duration

    // CBORImpl specifies which CBOR implementation to use.
    // Default is CBORImplDefault which uses the default from connection.NewConfig.
    CBORImpl CBORImpl
}

added in v0.9.0

func MustNewConfig(namespace, database string, tables ...string) *Config

added in v0.9.0

func NewConfig(namespace, database string, tables ...string) (*Config, error)

added in v0.9.0

func (c *Config) MustNew() *surrealdb.DB

added in v0.9.0

func (c *Config) New() (*surrealdb.DB, error)

added in v0.9.0

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

TestLogHandler is a slog.Handler that prints message index (starting from 0) level, and message content, without the timestamp. This allows test log output to be deterministic.

added in v0.9.0

func NewTestLogHandler() *TestLogHandler

Example

Output:

[0] INFO: Application started
[1] WARN: Cache miss key=user:123
[2] ERROR: Database connection failed retry=3

Example (ComplexAttributes) Example (EmptyGroup)

Output:

[0] INFO: message key=value

Example (Enabled)

Output:

[0] DEBUG: debug enabled
[1] INFO: info enabled
[2] WARN: warn enabled
[3] ERROR: error enabled

Example (IndexIncrement)

Output:

[0] INFO: test message
[1] INFO: test message
[2] INFO: test message
[3] INFO: test message
[4] INFO: test message

Example (MultipleMessages)

Output:

[0] INFO: first message
[1] WARN: warning message
[2] ERROR: error message
[3] DEBUG: debug message

Example (NestedGroupAttribute) Example (NestedGroups)

Output:

[0] INFO: query executed app.db.query=SELECT * FROM users, app.db.duration=100ms

Example (WithAttributes)

Output:

[0] INFO: message with attrs key1=value1, count=42, enabled=true

Example (WithAttrs)

Output:

[0] INFO: first
[1] INFO: second request_id=123
[2] INFO: third request_id=123, count=42

Example (WithAttrsAndGroup)

Output:

[0] INFO: handled service=api, request.method=GET, request.status=200

Example (WithGroup)

Output:

[0] INFO: grouped message mygroup.key=value

added in v0.9.0

func NewTestLogHandlerWithFrames() *TestLogHandler

Example

added in v0.9.0

func NewTestLogHandlerWithOptions(opts ...TestLogHandlerOption) *TestLogHandler

NewTestLogHandlerWithOptions creates a TestLogHandler with custom options

Example (IgnoreDebug)

Output:

[0] INFO: Application started
[1] WARN: Warning: Low memory
[2] ERROR: Connection failed

Example (IgnoreErrors)

Output:

[0] ERROR: Database error table=users
[1] INFO: Server started

added in v0.9.0

added in v0.9.0

added in v0.9.0

added in v0.9.0

func (h *TestLogHandler) WithGroup(name string) slog.Handler

added in v0.9.0

type TestLogHandlerOption func(*TestLogHandler)

TestLogHandlerOption is a function that configures a TestLogHandler

added in v0.9.0

func WithFrames() TestLogHandlerOption

WithFrames enables frame information in log output

added in v0.9.0

func WithIgnoreDebug() TestLogHandlerOption

WithIgnoreDebug configures the handler to ignore DEBUG level messages

added in v0.9.0

func WithIgnoreErrorPrefixes(prefixes ...string) TestLogHandlerOption

WithIgnoreErrorPrefixes sets prefixes for error messages that should be ignored

脚注

    Webmention0

    Comments(0)

    まだコメントはありません。

    Powered by Webmention.io

    Octothorpes

    clippings
    あさだあめ

    本を読んだりするおじさん。Amazonのアソシエイトとして、適格販売により収入を得ています。