pgexpect

package module
v0.0.0-...-0632081 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2017 License: MIT Imports: 6 Imported by: 0

README

pgexpect

Test mocks & stubs made easy for Postgres functions.

You can verify that a function was called with the expected parameters. You can also replace the body of a function or make it raise a SQL exception.

How to mock?

Example:

db := GetTestDB()
defer db.Close()

function := pgexpect.Function{
  Name: "some_function_name",
  Args: []pgexpect.Argument{
    {Name: "param1", Type: "integer"},
    {Name: "param2", Type: "text"},
  },
  ReturnType: "SETOF stuff",
  Body: `INSERT INTO ...` // You can add some SQL that the mock should run.
}

expectedCalls := []pgexpect.Call{
  {Values: []interface{}{uint64(123), "Gemnasium is awesome! ;)"}},
}

pgexpect.MockFunction(function, expectedCalls, t, db, func(t *testing.T, db *sqlx.DB) {
  service := SomeDBService{DB: db}
  err := service.CreateStuff(123, "Gemnasium is awesome! ;)")
  if err != nil {
    t.Fatal(err)
  }
})

How to stub?

Example:

db := GetTestDB()
defer db.Close()

function := pgexpect.Function{
  Name: "your_super_smart_function",
  Args: []pgexpect.Argument{
    {Name: "name", Type: "text"},
  },
  ReturnType:     "VOID",
  RaiseErrorCode: "GM007", // in case you need to simulate an error in your SQL function
}

pgexpect.StubFunction(function, t, db, func() {
  service := UserDBService{DB: db}
  _, err := service.Create("john@example.com")
  if err == nil || err != ErrUserEmailAlreadyExists {
    t.Errorf("An ErrUserEmailAlreadyExists error was expected but we got %s", err)
  }
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockFunction

func MockFunction(function Function, expectedCalls []Call, t *testing.T, db *sqlx.DB, testFunc func(t *testing.T, db *sqlx.DB))

MockFunction replace a function and expect calls to it

func StubFunction

func StubFunction(function Function, t *testing.T, db *sqlx.DB, testFunc func())

StubFunction replace a function with a fake version of it

func StubView

func StubView(name, body string)

StubView replace a view with a fake version of it

Types

type Argument

type Argument struct {
	Name string
	Type string
}

Argument define an argument to a function

type Call

type Call struct {
	Values []interface{}
}

Call defines the calls we are expecting to the function

type Function

type Function struct {
	Name           string
	Args           []Argument
	ReturnType     string
	Body           string
	RaiseErrorCode string
}

Function defines the function being replaced

Jump to

Keyboard shortcuts

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