testseed

package
v0.19.786 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0

README

testseed

Test fixture utilities for ctl-api integration tests.

Setup

type TestService struct {
    fx.In

    DB     *gorm.DB `name:"psql"`
    Seeder *testseed.Seeder
}

func (s *MyTestSuite) SetupSuite() {
    options := append(
        tests.CtlApiFXOptions(),
        fx.Provide(testseed.New),
        fx.Populate(&s.service),
    )

    s.app = fxtest.New(s.T(), options...)
    s.app.RequireStart()
}

Creating Fixtures

Each entity type has two or three functions:

  • Build* -- creates an in-memory struct with fake defaults (no DB)
  • Create* -- builds and persists to the database (uses account/org from context if set)
  • Ensure* -- creates, persists, and sets the entity's ID in the context (Account and Org only)
Setting up an account and org in SetupTest

EnsureAccount and EnsureOrg return an updated context with the IDs set. Subsequent Create* calls on the seeder will pick up these IDs automatically.

func (s *MyTestSuite) SetupTest() {
    s.ctx = context.Background()
    s.ctx, s.testAcc = s.service.Seeder.EnsureAccount(s.ctx, s.T())
    s.ctx, s.testOrg = s.service.Seeder.EnsureOrg(s.ctx, s.T())
}
Creating entities

Create* methods build a struct with fake defaults and persist it in one step. If the context has an account or org ID (set via Ensure*), those are used instead of generating new ones.

// Create an account (standalone, not added to context)
acct := s.service.Seeder.CreateAccount(s.ctx, s.T())

// Create an org (uses account from context for CreatedByID)
org := s.service.Seeder.CreateOrg(s.ctx, s.T())

// Create an app (uses org and account from context)
testApp := s.service.Seeder.CreateApp(s.ctx, s.T())

// Create an install (uses org and account from context)
install := s.service.Seeder.CreateInstall(s.ctx, s.T())
Building without persisting, then saving manually

Build* functions create in-memory structs with generated IDs and fake defaults. You can override fields and save to the DB yourself.

// Build an app in memory
myApp := testseed.BuildApp()

// Override with the test account and org
myApp.CreatedBy = *s.testAcc
myApp.CreatedByID = s.testAcc.ID
myApp.Org = s.testOrg
myApp.OrgID = s.testOrg.ID

// Persist to the database
res := s.service.DB.WithContext(s.ctx).Create(myApp)
require.NoError(s.T(), res.Error)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildAccount

func BuildAccount() *app.Account

BuildAccount creates an app.Account with fake defaults.

func BuildApp

func BuildApp() *app.App

BuildApp creates an app.App with fake defaults.

func BuildInstall

func BuildInstall() *app.Install

BuildInstall creates an app.Install with fake defaults.

func BuildOrg

func BuildOrg() *app.Org

BuildOrg creates an app.Org with fake defaults.

Types

type Params

type Params struct {
	fx.In

	DB *gorm.DB `name:"psql"`
}

type Seeder

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

func New

func New(params Params) *Seeder

func (*Seeder) CreateAccount

func (s *Seeder) CreateAccount(ctx context.Context, t *testing.T) *app.Account

CreateAccount builds and persists an account to the database.

func (*Seeder) CreateApp

func (s *Seeder) CreateApp(ctx context.Context, t *testing.T) *app.App

CreateApp builds and persists an app to the database. Uses org/account from context if available.

func (*Seeder) CreateInstall

func (s *Seeder) CreateInstall(ctx context.Context, t *testing.T) *app.Install

CreateInstall builds and persists an install to the database. Uses org/account from context if available.

func (*Seeder) CreateOrg

func (s *Seeder) CreateOrg(ctx context.Context, t *testing.T) *app.Org

CreateOrg builds and persists an org to the database. Uses account from context if available for CreatedByID.

func (*Seeder) EnsureAccount

func (s *Seeder) EnsureAccount(ctx context.Context, t *testing.T) (context.Context, *app.Account)

EnsureAccount creates a test account and sets its ID in the context via cctx.SetAccountIDContext.

func (*Seeder) EnsureOrg

func (s *Seeder) EnsureOrg(ctx context.Context, t *testing.T) (context.Context, *app.Org)

EnsureOrg creates a test org and sets its ID in the context via cctx.SetOrgIDContext.

Jump to

Keyboard shortcuts

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