builtin

package
v0.0.0-...-d0b8ee3 Latest Latest
Warning

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

Go to latest
Published: May 12, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Template = `// Code generated by dbtest; DO NOT EDIT.
// github.com/RussellLuo/dbtest

package {{$.DstPkgName}}_test

import (
	"fmt"
	"testing"
	"time"
	"github.com/protogodev/dbtest/builtin"
	"github.com/protogodev/dbtest/spec"

	{{- range $.Imports}}
	{{.ImportString}}
	{{- end}}
)

var (
	testee *builtin.Testee
	sut    {{$.SrcPkgName}}.{{$.InterfaceName}}
)

func TestMain(m *testing.M) {
	t, err := {{.Testee}}
	if err != nil {
		fmt.Printf("err: %v\n", err)
		os.Exit(1)
	}

	testee = t.Complete()
	if err := testee.Validate(); err != nil {
		fmt.Printf("err: %v\n", err)
		os.Exit(1)
	}
	sut = testee.SUT.({{$.SrcPkgName}}.{{$.InterfaceName}})

	// os.Exit() does not respect deferred functions
	code := m.Run()

	_ = testee.DB.Close()
	os.Exit(code)
}

{{- range $.Tests}}
{{- $method := interfaceMethod .Name}}

func Test{{.Name}}(t *testing.T) {
	f := builtin.NewFixture(t, testee.DB, map[string]spec.Rows{
		{{- range $tableName, $rows := .Fixture }}
		"{{$tableName}}": {
			{{- range $rows}}
			{{.LiteralString}},
			{{- end}} {{/* range $rows */}}
		},
		{{- end}} {{/* range $tableName, $rows := .Fixture */}}
	})
	f.SetUp()
	defer f.TearDown()

	// in contains all the input parameters (except ctx) of {{.Name}}.
	type in struct {
		{{- range $method.Params}}
		{{title .Name}} {{.TypeString}} ` + "`dbtest:\"{{.Name}}\"`" + `
		{{- end}}
	}
	
	// out contains all the output parameters of {{.Name}}.
	type out struct {
		{{- range $method.Returns}}
		{{title .Name}} {{.TypeString}} ` + "`dbtest:\"{{.Name}}\"`" + `
		{{- end}}
	}

	tests := []struct {
		name     string
		in       map[string]interface{}
		wantOut  map[string]interface{}
		wantData []spec.DataAssertion
	}{
	    {{- range .Subtests}}
		{
			name: "{{.Name}}",
			in: {{goString .In}},
			wantOut: {{goString .WantOut}},
			wantData: []spec.DataAssertion{
			    {{- range .WantData}}
				{
					Query: "{{.Query}}",
					Result: spec.Rows{
						{{- range .Result}}
						{{.LiteralString}},
						{{- end}} {{/* range .Result */}}
					},
				},
			    {{- end}} {{/* range .WantData */}}
			},
		},
	    {{- end}} {{/* range .Subtests */}}
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			var in in
			if err := testee.Codec.Decode(tt.in, &in); err != nil {
				t.Errorf("err when decoding In: %v", err)
			}

			var gotOut out
			{{fmtArgCSV $method.ReturnArgValueList "gotOut.>Name"}} = sut.{{.Name}}({{fmtArgCSV $method.CallArgList "in.>Name"}})

			encodedOut, err := testee.Codec.Encode(gotOut)
			if err != nil {
				t.Errorf("err when encoding Out: %v", err)
			}

			// Using "%+v" instead of "%#v" as a workaround for https://github.com/go-yaml/yaml/issues/139.
			if fmt.Sprintf("%+v", encodedOut) != fmt.Sprintf("%+v", tt.wantOut) {
				t.Errorf("Out: Got (%+v) != Want (%+v)", encodedOut, tt.wantOut)
			}

			for _, want := range tt.wantData {
				gotResult := f.Query(want.Query)
				if !gotResult.Equal(want.Result) {
					t.Errorf("Result: Got (%#v) != Want (%#v)", gotResult, want.Result)
				}
			}

			if len(tt.wantData) > 0 {
				// This is an unsafe test, reset the fixture.
				f.Reset()
			}
		})
	}
}
{{- end}} {{/* range $.Tests */}}
`

Functions

This section is empty.

Types

type Codec

type Codec interface {
	Decode(in interface{}, out interface{}) (err error)
	Encode(in interface{}) (out interface{}, err error)
}

func DefaultCodec

func DefaultCodec(layout string) Codec

type DB

type DB interface {
	Insert(data map[string]spec.Rows) error
	Delete(keys ...string) error
	Select(query string) (spec.Rows, error)
	Close() error
}

type Fixture

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

func NewFixture

func NewFixture(t *testing.T, db DB, data map[string]spec.Rows) *Fixture

func (*Fixture) Query

func (f *Fixture) Query(query string) (result spec.Rows)

func (*Fixture) Reset

func (f *Fixture) Reset()

func (*Fixture) SetUp

func (f *Fixture) SetUp()

func (*Fixture) TearDown

func (f *Fixture) TearDown()

type Testee

type Testee struct {
	// System under test (http://xunitpatterns.com/SUT.html),
	// which must implement the corresponding interface.
	SUT   interface{}
	DB    DB
	Codec Codec
}

func (*Testee) Complete

func (t *Testee) Complete() *Testee

func (*Testee) Validate

func (t *Testee) Validate() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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