testutil

package
v0.0.0-...-c7d4cb4 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: MIT Imports: 30 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeycloakTestUserPassword  = "test123"
	KeycloakAdminTestPassword = keycloakAdminPassword
)

Exported test credentials used by integration/e2e tests.

Variables

This section is empty.

Functions

func AssertAggregateID

func AssertAggregateID(t *testing.T, evt event.DomainEvent, expectedID string)

AssertAggregateID checks aggregate ID in the event

func AssertEventCount

func AssertEventCount(t *testing.T, events []event.DomainEvent, expected int)

AssertEventCount checks count of published events

func AssertEventPublished

func AssertEventPublished(t *testing.T, events []event.DomainEvent, eventType string) event.DomainEvent

AssertEventPublished checks that event of specific type was published

func AssertEventType

func AssertEventType(t *testing.T, evt event.DomainEvent, expectedType string)

AssertEventType checks event type

func AssertNoError

func AssertNoError(t *testing.T, err error, msgAndArgs ...any)

AssertNoError checks absence of error and stops test

func AssertNotZeroUUID

func AssertNotZeroUUID(t *testing.T, id uuid.UUID, msgAndArgs ...any)

AssertNotZeroUUID checks, that UUID not empty

func AssertTimeAfter

func AssertTimeAfter(t *testing.T, actual, expected time.Time, msgAndArgs ...any)

AssertTimeAfter checks, that actual time after expected

func AssertTimeApproximatelyEqual

func AssertTimeApproximatelyEqual(t *testing.T, expected, actual time.Time, delta time.Duration, msgAndArgs ...any)

AssertTimeApproximatelyEqual checks, that two time approximately equal with acceptable tolerance delta (usually time.Second or time.Millisecond)

func AssertTimeBefore

func AssertTimeBefore(t *testing.T, actual, expected time.Time, msgAndArgs ...any)

AssertTimeBefore checks, that actual time before expected

func AssertTimeNotZero

func AssertTimeNotZero(t *testing.T, tm time.Time, msgAndArgs ...any)

AssertTimeNotZero checks, that time not nil

func AssertUUIDEqual

func AssertUUIDEqual(t *testing.T, expected, actual uuid.UUID, msgAndArgs ...any)

AssertUUIDEqual checks equality of two UUIDs

func AssertVersion

func AssertVersion(t *testing.T, evt event.DomainEvent, expectedVersion int)

AssertVersion checks aggregate version in the event

func AssertZeroUUID

func AssertZeroUUID(t *testing.T, id uuid.UUID, msgAndArgs ...any)

AssertZeroUUID checks, that UUID empty

func CleanupSharedContainer

func CleanupSharedContainer()

CleanupSharedContainer terminates the shared container. This is typically called from TestMain or when all tests are done.

func CleanupSharedKeycloakContainer

func CleanupSharedKeycloakContainer()

CleanupSharedKeycloakContainer terminates the shared container. This is typically called from TestMain or when all tests are done.

func CleanupSharedRedisContainer

func CleanupSharedRedisContainer()

CleanupSharedRedisContainer terminates the shared container. This is typically called from TestMain or when all tests are done.

func NewTestContext

func NewTestContext(t *testing.T) context.Context

NewTestContext creates context with timeout for tests

func RequireNotZeroUUID

func RequireNotZeroUUID(t *testing.T, id uuid.UUID, msgAndArgs ...any)

RequireNotZeroUUID checks, that UUID not empty and stops test at error

func RequireTimeApproximatelyEqual

func RequireTimeApproximatelyEqual(t *testing.T, expected, actual time.Time, delta time.Duration, msgAndArgs ...any)

RequireTimeApproximatelyEqual checks, that two time approximately equal and stops test at error

func RequireTimeNotZero

func RequireTimeNotZero(t *testing.T, tm time.Time, msgAndArgs ...any)

RequireTimeNotZero checks, that time not nil and stops test at error

func RequireUUIDEqual

func RequireUUIDEqual(t *testing.T, expected, actual uuid.UUID, msgAndArgs ...any)

RequireUUIDEqual checks equality of two UUIDs and stops test at error

func RequireZeroUUID

func RequireZeroUUID(t *testing.T, id uuid.UUID, msgAndArgs ...any)

RequireZeroUUID checks, that UUID empty and stops test at error

func SetupSharedTestMongoDB

func SetupSharedTestMongoDB(t *testing.T) *mongo.Database

SetupSharedTestMongoDB creates a test database using the shared MongoDB container. Each test gets its own isolated database within the shared container. This is much faster than starting a new container for each test. Note: Index creation is disabled by default to prevent MongoDB container crashes. Use SetupSharedTestMongoDBWithOptions(t, true) if you need indexes.

func SetupSharedTestMongoDBWithClient

func SetupSharedTestMongoDBWithClient(t *testing.T) (*mongo.Client, *mongo.Database)

SetupSharedTestMongoDBWithClient creates a test database and returns both client and database. Uses the shared MongoDB container for faster test execution. Note: Index creation is disabled by default to prevent MongoDB container crashes. Use SetupSharedTestMongoDBWithClientOptions(t, true) if you need indexes.

func SetupSharedTestMongoDBWithClientOptions

func SetupSharedTestMongoDBWithClientOptions(t *testing.T, createIndexes bool) (*mongo.Client, *mongo.Database)

SetupSharedTestMongoDBWithClientOptions creates a test database with optional index creation.

func SetupSharedTestMongoDBWithOptions

func SetupSharedTestMongoDBWithOptions(t *testing.T, createIndexes bool) *mongo.Database

SetupSharedTestMongoDBWithOptions creates a test database with optional index creation. Use createIndexes=false for tests that don't need indexes for faster execution.

func SetupTestMongoDB

func SetupTestMongoDB(t *testing.T) *mongo.Database

SetupTestMongoDB creates connection to test MongoDB using shared container. each test receives its own isolated database for safe parallel execution.

func SetupTestMongoDBIsolated

func SetupTestMongoDBIsolated(t *testing.T) *mongo.Database

SetupTestMongoDBIsolated creates new container MongoDB for full isolation. Use this function only when needed full isolation of container. in most cases SetupTestMongoDB is sufficient (isolation at the database).

func SetupTestMongoDBWithClient

func SetupTestMongoDBWithClient(t *testing.T) (*mongo.Client, *mongo.Database)

SetupTestMongoDBWithClient creates connection to test MongoDB and returns client and database. uses shared container for faster tests.

func SetupTestMongoDBWithClientIsolated

func SetupTestMongoDBWithClientIsolated(t *testing.T) (*mongo.Client, *mongo.Database)

SetupTestMongoDBWithClientIsolated creates new container and returns client and database. Use only when needed full isolation of container.

func SetupTestRedis

func SetupTestRedis(t *testing.T) *redis.Client

SetupTestRedis creates a Redis client using the shared container. This is the recommended way to get a Redis client for tests.

func SetupTestRedisIsolated

func SetupTestRedisIsolated(t *testing.T) *redis.Client

SetupTestRedisIsolated creates a new Redis container for complete isolation. Use this only when you need a completely clean Redis instance.

func SetupTestRedisWithPrefix

func SetupTestRedisWithPrefix(t *testing.T) (*redis.Client, string)

SetupTestRedisWithPrefix creates a Redis client and returns a unique key prefix for test isolation. This allows multiple tests to run in parallel without key conflicts.

Types

type KeycloakTokenResponse

type KeycloakTokenResponse struct {
	AccessToken      string `json:"access_token"`
	RefreshToken     string `json:"refresh_token"`
	ExpiresIn        int    `json:"expires_in"`
	RefreshExpiresIn int    `json:"refresh_expires_in"`
	TokenType        string `json:"token_type"`
	IDToken          string `json:"id_token,omitempty"`
	Scope            string `json:"scope,omitempty"`
}

KeycloakTokenResponse represents the token response from Keycloak

type KeycloakUserInfo

type KeycloakUserInfo struct {
	Sub               string   `json:"sub"`
	PreferredUsername string   `json:"preferred_username"`
	Email             string   `json:"email"`
	EmailVerified     bool     `json:"email_verified"`
	Name              string   `json:"name"`
	GivenName         string   `json:"given_name,omitempty"`
	FamilyName        string   `json:"family_name,omitempty"`
	Groups            []string `json:"groups,omitempty"`
	RealmAccess       struct {
		Roles []string `json:"roles"`
	} `json:"realm_access"`
}

KeycloakUserInfo represents user info from Keycloak

type MongoContainer

type MongoContainer struct {
	Container testcontainers.Container
	URI       string
}

MongoContainer represents MongoDB container for tests

type SharedKeycloakContainer

type SharedKeycloakContainer struct {
	Container    testcontainers.Container
	URL          string
	AdminUser    string
	AdminPass    string
	Realm        string
	ClientID     string
	ClientSecret string
}

SharedKeycloakContainer represents a reusable Keycloak container for tests

func GetSharedKeycloakContainer

func GetSharedKeycloakContainer(ctx context.Context) (*SharedKeycloakContainer, error)

GetSharedKeycloakContainer returns a singleton Keycloak container. The container is started once and reused across all tests.

func SetupTestKeycloak

func SetupTestKeycloak(t *testing.T) *SharedKeycloakContainer

SetupTestKeycloak creates a Keycloak container using the shared container. This is the recommended way to get a Keycloak container for tests.

func SetupTestKeycloakIsolated

func SetupTestKeycloakIsolated(t *testing.T) *SharedKeycloakContainer

SetupTestKeycloakIsolated creates a new Keycloak container for complete isolation. Use this only when you need a completely clean Keycloak instance.

func (*SharedKeycloakContainer) ClientExists

func (c *SharedKeycloakContainer) ClientExists(ctx context.Context) (bool, error)

ClientExists checks if the OAuth2 client exists

func (*SharedKeycloakContainer) GetAdminToken

func (c *SharedKeycloakContainer) GetAdminToken(ctx context.Context) (string, error)

GetAdminToken obtains an admin access token

func (*SharedKeycloakContainer) GetGroups

func (c *SharedKeycloakContainer) GetGroups(ctx context.Context) ([]string, error)

GetGroups retrieves all groups

func (*SharedKeycloakContainer) GetRealmRoles

func (c *SharedKeycloakContainer) GetRealmRoles(ctx context.Context) ([]string, error)

GetRealmRoles retrieves all realm roles

func (*SharedKeycloakContainer) GetUserInfo

func (c *SharedKeycloakContainer) GetUserInfo(ctx context.Context, accessToken string) (*KeycloakUserInfo, error)

GetUserInfo retrieves user info using an access token

func (*SharedKeycloakContainer) GetUserToken

func (c *SharedKeycloakContainer) GetUserToken(
	ctx context.Context,
	username, password string,
) (*KeycloakTokenResponse, error)

GetUserToken obtains an access token for a test user using direct access grants.

func (*SharedKeycloakContainer) GetUsers

func (c *SharedKeycloakContainer) GetUsers(ctx context.Context) ([]string, error)

GetUsers retrieves all users in the realm

func (*SharedKeycloakContainer) RealmExists

func (c *SharedKeycloakContainer) RealmExists(ctx context.Context) (bool, error)

RealmExists checks if the realm exists

type SharedMongoContainer

type SharedMongoContainer struct {
	Container testcontainers.Container
	URI       string
	// contains filtered or unexported fields
}

SharedMongoContainer represents a reusable MongoDB container for tests

func GetSharedMongoContainer

func GetSharedMongoContainer(ctx context.Context) (*SharedMongoContainer, error)

GetSharedMongoContainer returns a singleton MongoDB container. The container is started once and reused across all tests. It will be terminated when the test binary exits. If the container has crashed, it will be automatically recreated.

func (*SharedMongoContainer) GetClient

func (c *SharedMongoContainer) GetClient(ctx context.Context) (*mongo.Client, error)

GetClient returns a shared MongoDB client, creating one if needed. The client is reused across all tests to avoid connection exhaustion.

type SharedRedisContainer

type SharedRedisContainer struct {
	Container testcontainers.Container
	Addr      string
	// contains filtered or unexported fields
}

SharedRedisContainer represents a reusable Redis container for tests

func GetSharedRedisContainer

func GetSharedRedisContainer(ctx context.Context) (*SharedRedisContainer, error)

GetSharedRedisContainer returns a singleton Redis container. The container is started once and reused across all tests.

func (*SharedRedisContainer) GetClient

func (c *SharedRedisContainer) GetClient(ctx context.Context) (*redis.Client, error)

GetClient returns a shared Redis client, creating one if needed.

type TestSuite

type TestSuite struct {

	// Mocks
	ChatRepo         *mocks.MockChatRepository
	MessageRepo      *mocks.MockMessageRepository
	UserRepo         *mocks.MockUserRepository
	WorkspaceRepo    *mocks.MockWorkspaceRepository
	NotificationRepo *mocks.MockNotificationRepository
	EventBus         *mocks.MockEventBus
	EventStore       *mocks.MockEventStore

	// Use Cases - Chat
	CreateChat     *chatapp.CreateChatUseCase
	AddParticipant *chatapp.AddParticipantUseCase
	ConvertToTask  *chatapp.ConvertToTaskUseCase
	ChangeStatus   *chatapp.ChangeStatusUseCase
	AssignUser     *chatapp.AssignUserUseCase
	// contains filtered or unexported fields
}

TestSuite represents full set for integration testing

func NewTestSuite

func NewTestSuite(t *testing.T) *TestSuite

NewTestSuite creates New test suite with all initialized components

func (*TestSuite) Reset

func (s *TestSuite) Reset()

Reset clears all repositories (helpful between tests)

Jump to

Keyboard shortcuts

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