go-sqltest

module
v0.0.0-...-e3b2d30 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2024 License: BSD-3-Clause

README

sqltest Build Status

SqlTest is a test code generator for the Go programming language. It runs your tests on real db, records sql-traffic into sqlmock and makes your tests work without real db.

Installation

Once you have installed Go, run these commands to install the sqlmockgen tool:

go get github.com/guzenok1/go-sqltest
go install github.com/guzenok1/go-sqltest/sqlmockgen

Running sqlmockgen

The sqlmockgen command is used to generate offline tests code according to you special named test functions. It takes 1 argument:

  • absolute or relative import path of package to generate offline tests for;

and supports the following flags:

  • -out: a file to which to write the resulting source code;

  • -db: a connection string to real db;

  • -copyright: copyright file used to add copyright header to the resulting source code;

Example for installed:

sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .

Example for gotten:

go run github.com/guzenok1/go-sqltest/sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .

Example for go generate:

//go:generate go run github.com/guzenok1/go-sqltest/sqlmockgen -out=sql_test.go -db=postgresql://postgres:postgres@localhost:5432/test?sslmode=disable .

For an example of the sqlmockgen using, see the sample/ directory.

Test function naming agreement

Your function for test db initialization (migrations, data fixtures, etc.) should be:

func initTestDb(dbUrl string) (*sql.DB, error) {
  // open connection and prepare data
}

Your test functions should be like:

func test<TESTNAME>(*testing.T, *sql.DB) {
  // test your code with db connection
}

(with different <TESTNAME>)

Then sqlmockgen will generate go tests:

func Test<TESTNAME>(*testing.T) {
  db, err := test<TESTNAME>SqlMock()
  if err != nil {
    t.Fatal(err)
  }
  test<TESTNAME>(t, db)
}

func test<TESTNAME>SqlMock() (*sql.DB, error) {
    // generated code here:
    // github.com/DATA-DOG/go-sqlmock initialization.
}

Directories

Path Synopsis
model
Package model contains the data model necessary for generating sqlmock implementations.
Package model contains the data model necessary for generating sqlmock implementations.

Jump to

Keyboard shortcuts

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