tests

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: AGPL-3.0 Imports: 39 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateOrgProjectAndAssetAssetVersion

func CreateOrgProjectAndAssetAssetVersion(db shared.DB) (models.Org, models.Project, models.Asset, models.AssetVersion)

func InitDatabaseContainer

func InitDatabaseContainer(initDBSQLPath string) (shared.DB, *pgxpool.Pool, func())

func InitRawDatabaseContainer

func InitRawDatabaseContainer(initDBSQLPath string) (*pgxpool.Pool, func())

func NewContext

func NewContext(r *http.Request, w http.ResponseWriter) shared.Context

func NewUserSession added in v1.11.0

func NewUserSession(t testing.TB, userID string) *mocks.AuthSession

NewUserSession returns a fully-stubbed user-owned shared.AuthSession mock. Every AuthSession method is stubbed with .Maybe() so callers don't need to know in advance which subset the code path under test will invoke — this is the single place to update when the AuthSession interface changes, instead of every test hand-rolling its own mocks.NewAuthSession(t) + .On(...) chain. Call .On(...) on the returned mock afterwards to override a default.

func WithTestApp

func WithTestApp(t *testing.T, sqlInitFile string, testFn func(*TestFixture))

WithTestApp provides a callback-based pattern for tests

func WithTestAppOptions

func WithTestAppOptions(t *testing.T, sqlInitFile string, options TestAppOptions, testFn func(*TestFixture))

Types

type QueryCounter added in v1.12.0

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

QueryCounter is a GORM plugin that counts every statement issued through a *gorm.DB, so benchmarks can report queries/op and spot N+1 patterns.

It is intended for tests/benchmarks only: it keeps one entry per distinct normalized statement in memory.

func NewQueryCounter added in v1.12.0

func NewQueryCounter() *QueryCounter

func (*QueryCounter) Calls added in v1.12.0

func (q *QueryCounter) Calls() int64

func (*QueryCounter) Duration added in v1.12.0

func (q *QueryCounter) Duration() time.Duration

func (*QueryCounter) Initialize added in v1.12.0

func (q *QueryCounter) Initialize(db *gorm.DB) error

Initialize registers the counting callbacks on every GORM processor.

func (*QueryCounter) Name added in v1.12.0

func (q *QueryCounter) Name() string

func (*QueryCounter) Report added in v1.12.0

func (q *QueryCounter) Report(topN, iterations int) []string

Report renders a compact, line-oriented summary. iterations scales the per-call numbers down to "per benchmark iteration".

func (*QueryCounter) Reset added in v1.12.0

func (q *QueryCounter) Reset()

Reset clears all collected data. Call it right before the measured section.

func (*QueryCounter) Rows added in v1.12.0

func (q *QueryCounter) Rows() int64

func (*QueryCounter) TopStats added in v1.12.0

func (q *QueryCounter) TopStats(n int) []QueryStat

TopStats returns the n statements with the most calls, descending.

type QueryStat added in v1.12.0

type QueryStat struct {
	SQL      string
	Calls    int64
	Duration time.Duration
	Rows     int64
}

QueryStat aggregates all executions of a single normalized statement.

type TestApp

type TestApp struct {
	fx.In

	// Core infrastructure
	DB     shared.DB
	Broker shared.PubSubBroker

	// Services
	ConfigService            shared.ConfigService
	LicenseRiskService       shared.LicenseRiskService
	StatisticsService        shared.StatisticsService
	ComponentService         shared.ComponentService
	FirstPartyVulnService    shared.FirstPartyVulnService
	DependencyVulnService    shared.DependencyVulnService
	ArtifactService          shared.ArtifactService
	AssetVersionService      shared.AssetVersionService
	AssetService             shared.AssetService
	ProjectService           shared.ProjectService
	OrgService               shared.OrgService
	ScanService              shared.ScanService
	CSAFService              shared.CSAFService
	ReleaseService           shared.ReleaseService
	OpenSourceInsightService shared.OpenSourceInsightService
	MaliciousPackageChecker  shared.MaliciousPackageChecker

	// Controllers
	AssetController             *controllers.AssetController
	AssetVersionController      *controllers.AssetVersionController
	ScanController              *controllers.ScanController
	ProjectController           *controllers.ProjectController
	OrgController               *controllers.OrgController
	DependencyVulnController    *controllers.DependencyVulnController
	FirstPartyVulnController    *controllers.FirstPartyVulnController
	ComponentController         *controllers.ComponentController
	ArtifactController          *controllers.ArtifactController
	CSAFController              *controllers.CSAFController
	VEXRuleController           *controllers.VEXRuleController
	ExternalReferenceController *controllers.ExternalReferenceController
	StatisticsController        *controllers.StatisticsController
	VulnDBController            *controllers.VulnDBController

	// Repositories
	AssetRepository                 shared.AssetRepository
	AssetVersionRepository          shared.AssetVersionRepository
	ComponentRepository             shared.ComponentRepository
	DependencyVulnRepository        shared.DependencyVulnRepository
	FirstPartyVulnRepository        shared.FirstPartyVulnRepository
	CveRepository                   shared.CveRepository
	CweRepository                   shared.CweRepository
	ExploitRepository               shared.ExploitRepository
	AffectedComponentRepository     shared.AffectedComponentRepository
	ProjectRepository               shared.ProjectRepository
	OrgRepository                   shared.OrganizationRepository
	ArtifactRepository              shared.ArtifactRepository
	VulnEventRepository             shared.VulnEventRepository
	ComponentProjectRepository      shared.ComponentProjectRepository
	StatisticsRepository            shared.StatisticsRepository
	LicenseRiskRepository           shared.LicenseRiskRepository
	GitLabOauth2TokenRepository     shared.GitLabOauth2TokenRepository
	GitlabIntegrationRepository     shared.GitlabIntegrationRepository
	ExternalUserRepository          shared.ExternalUserRepository
	AggregatedVulnRepository        shared.VulnRepository
	VexRuleRepository               shared.VEXRuleRepository
	VexRuleRecommendationRepository shared.VEXRuleRecommendationRepository
	TrustedEntityRepository         shared.TrustedEntityRepository
	ExternalReferenceRepository     shared.ExternalReferenceRepository
	UpstreamVEXRuleRepository       shared.UpstreamVEXRuleRepository

	// Access Control
	RBACProvider shared.RBACProvider

	// Integrations
	GitlabIntegration    *gitlabint.GitlabIntegration
	IntegrationAggregate shared.IntegrationAggregate
	VulnDBService        shared.VulnDBService

	// Daemons
	DaemonRunner         shared.DaemonRunner
	FixedVersionResolver shared.FixedVersionResolver
}

TestApp provides access to all services and controllers via FX

func NewTestApp

func NewTestApp(t testing.TB, db shared.DB, pool *pgxpool.Pool, opts *TestAppOptions) (*TestApp, *fxtest.App, error)

NewTestApp creates a test application with all dependencies wired via FX It uses the same FX modules as production for consistency

func NewTestAppWithT

func NewTestAppWithT(t testing.TB, db shared.DB, pool *pgxpool.Pool, opts *TestAppOptions) (*TestApp, *fxtest.App)

NewTestAppWithT creates a test application tied to a testing.T It automatically stops the app when the test completes

type TestAppOptions

type TestAppOptions struct {
	// Additional FX options to include
	ExtraOptions []fx.Option
	// Whether to suppress FX logging
	SuppressLogs bool
	// Custom broker (if nil, a default in-memory broker will be provided)
	Broker shared.PubSubBroker
}

TestAppOptions configures the test application

type TestFixture

type TestFixture struct {
	T   testing.TB
	App *TestApp
	DB  shared.DB
	// pool is the underlying pgx connection pool
	// its used by the database broker and other components
	Pool *pgxpool.Pool
}

TestFixture provides a complete test environment with database and FX app

func NewTestFixture

func NewTestFixture(t testing.TB, sqlInitFile string, options *TestAppOptions) *TestFixture

NewTestFixture creates a complete test environment with database container and FX app

func (*TestFixture) CreateAsset

func (f *TestFixture) CreateAsset(projectID uuid.UUID, name string) models.Asset

CreateAsset creates a test asset

func (*TestFixture) CreateAssetVersion

func (f *TestFixture) CreateAssetVersion(assetID uuid.UUID, name string, isDefault bool) models.AssetVersion

CreateAssetVersion creates a test asset version

func (*TestFixture) CreateDaemonRunner

func (f *TestFixture) CreateDaemonRunner() *daemons.DaemonRunner

CreateDaemonRunner creates a DaemonRunner instance with all dependencies from the test app

func (*TestFixture) CreateOrg

func (f *TestFixture) CreateOrg(name string) models.Org

CreateOrg creates a test organization

func (*TestFixture) CreateOrgProjectAssetAndVersion

func (f *TestFixture) CreateOrgProjectAssetAndVersion() (models.Org, models.Project, models.Asset, models.AssetVersion)

CreateOrgProjectAssetAndVersion creates a full test hierarchy using FX services

func (*TestFixture) CreateProject

func (f *TestFixture) CreateProject(orgID uuid.UUID, name string) models.Project

CreateProject creates a test project

type TestGitlabClientFactory

type TestGitlabClientFactory struct {
	*mocks.GitlabClientFacade
}

func (TestGitlabClientFactory) FromAccessToken

func (f TestGitlabClientFactory) FromAccessToken(accessToken string, baseURL string) (shared.GitlabClientFacade, error)

func (TestGitlabClientFactory) FromIntegration

func (TestGitlabClientFactory) FromIntegrationUUID

func (TestGitlabClientFactory) FromOauth2Token

func (f TestGitlabClientFactory) FromOauth2Token(_ context.Context, token models.GitLabOauth2Token, enableClientCache bool) (shared.GitlabClientFacade, error)

Jump to

Keyboard shortcuts

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