dbtest

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: Apache-2.0 Imports: 26 Imported by: 0

README

Database Tests

Configuring Local Server to Run Database Tests

In order to test web-tests written for controllers in your service, a local test database needs to be configured before the tests can be executed. In order to do so, follow the steps below:

  1. Download TablePlus to connect to the database to run queries.

  2. In TablePlus, create a new CockroachDB connection.

  3. Navigate to your Golang based service's config/application.yml file and locate the following section.

    data:
      logging:
        level: debug
        slow-threshold: 5s
      cockroach:
        host: ${db.cockroach.host:localhost}
        port: ${db.cockroach.port:26257}
        sslmode: ${db.cockroach.sslmode:disable}
        username: ${spring.datasource.username:root}
        Password: ${spring.datasource.password:root}
        database: exampleservice
    
  4. Use the fields from cockroach section to create the "Cockroach Connection" in TablePlus

    img.png

  5. Once done, create a new database by clicking the database icon or using ⌘+K on Macs or Ctrl + K on Windows and clicking New...

  6. Name the database to match the name in your implementation

    dbtest.WithDBPlayback("testdb")
    

    img.png

  7. Once done, you're all set to run the controller integration web-tests.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableDBRecordMode

func EnableDBRecordMode() suitetest.PackageOptions

EnableDBRecordMode Force enables DB recording mode. Normally recording mode should be enabled via `go test` argument `-record` IMPORTANT: when Record mode is enabled, all tests executing SQL against actual database. Or if Opensearch is being used, any queries to that will be executed against the real opensearch service.

So use this mode on LOCAL DEV ONLY, and have the DB copied before executing

func IsRecording

func IsRecording() bool

IsRecording returns true if copyist is in recording mode

func PrepareData

func PrepareData(di *DI, steps ...DataSetupStep) test.SetupFunc

PrepareData is a convenient function that returns a test.SetupFunc that executes given DataSetupStep in provided order Note: PrepareData accumulate all changes applied to context

func PrepareDataWithScope

func PrepareDataWithScope(di *DI, scope DataSetupScope, steps ...DataSetupStep) test.SetupFunc

PrepareDataWithScope is similar to PrepareData, it applies given DataSetupScope before executing all DataSetupStep. DataSetupScope is used to prepare context and gorm.DB for all given DataSetupStep Note: Different from PrepareData, PrepareDataWithScope doesn't accumulate changes to context

func WithDBPlayback

func WithDBPlayback(dbName string, opts ...DBOptions) test.Options

WithDBPlayback enables DB SQL playback capabilities supported by `copyist` This mode requires apptest.Bootstrap to work, and should not be used together with WithNoopMocks Each top-level test should have corresponding recorded SQL responses in `testdata` folder, or the test will fail. To enable record mode, use `go test ... -record` at CLI, or do it programmatically with EnableDBRecordMode See https://github.com/cockroachdb/copyist for more details

func WithNoopMocks

func WithNoopMocks() test.Options

WithNoopMocks create a noop tx.TxManager and a noop gorm.DB This mode requires apptest.Bootstrap to work, and should not be used together with WithDBPlayback Note: in this mode, gorm.DB's DryRun and SkipDefaultTransaction are enabled

Types

type DBOption

type DBOption struct {
	Host     string
	Port     int
	DBName   string
	Username string
	Password string
	SSL      bool
}

type DBOptions

type DBOptions func(opt *DBOption)

func DBCredentials

func DBCredentials(user, password string) DBOptions

func DBHost

func DBHost(host string) DBOptions

func DBName

func DBName(db string) DBOptions

func DBPort

func DBPort(port int) DBOptions

type DI

type DI struct {
	fx.In
	DB *gorm.DB
}

type DataSetupScope

type DataSetupScope func(ctx context.Context, t *testing.T, db *gorm.DB) (context.Context, *gorm.DB)

func SetupWithGormScopes

func SetupWithGormScopes(scopes ...func(*gorm.DB) *gorm.DB) DataSetupScope

SetupWithGormScopes returns a DataSetupScope that applies given gorm scopes

type DataSetupStep

type DataSetupStep func(ctx context.Context, t *testing.T, db *gorm.DB) context.Context

func SetupDropTables

func SetupDropTables(tables ...string) DataSetupStep

SetupDropTables returns a DataSetupStep that truncate given tables in single DROP TABLE IF EXISTS

func SetupOnce

func SetupOnce(once *sync.Once, steps ...DataSetupStep) DataSetupStep

SetupOnce returns a DataSetupStep that run given DataSetupSteps within the given sync.Once. How sync.Once is scoped is up to caller. e.g. once per test, once per package execution, etc...

func SetupTruncateTables

func SetupTruncateTables(tables ...string) DataSetupStep

SetupTruncateTables returns a DataSetupStep that truncate given tables in the provided order

func SetupUsingModelSeedFile

func SetupUsingModelSeedFile(fsys fs.FS, dest interface{}, filename string, closures ...func(ctx context.Context, db *gorm.DB)) DataSetupStep

SetupUsingModelSeedFile returns a DataSetupStep that load provided yaml file and parse it directly into provided model and save them. when "closures" is provided, it's invoked after seeding is done.

func SetupUsingSQLFile

func SetupUsingSQLFile(fsys fs.FS, filenames ...string) DataSetupStep

SetupUsingSQLFile returns a DataSetupStep that execute the provided sql file in given FS.

func SetupUsingSQLQueries

func SetupUsingSQLQueries(queries ...string) DataSetupStep

SetupUsingSQLQueries returns a DataSetupStep that execute the provided sql queries.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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