gtu

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2021 License: MIT Imports: 6 Imported by: 0

README

GormTestUtils

Test utils for Gorm you may find useful in your project.

Install

go get -v github.com/joegasewicz/gorm-test-utils

Setup & Teardown

Initiate GTU & pass your custom setup & teardown function.


import "github.com/joegasewicz/gorm-test-utils"


func TestMain(m *testing.M) {
    gtu.Utils.Init(m, &gtu.Utils.TestConfig{
        myDatabase,
        myCustomSetup,
        myCustomTeardown,
        []string{"my_table", "another_table"},
    })
}

Get JSON back from reponse body

Example for if JSON response is as follows:

{
  "my_model": {
    "Name": "John"
  }
}
// w := httptest.NewRecorder()
var resData map[string]myModel
gtu.Utils.GetJSON(w, &resData)

assert.Equal(t, 200, w.Code)
assert.Equal(t, "John", resData["my_model"].Name)

Create an entity


gtu.Utils.CreateEntity(t, &myModel)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Utils = GormTestUtils{}

Functions

This section is empty.

Types

type GormTestUtils

type GormTestUtils struct {
	IGormTestUtils
	// contains filtered or unexported fields
}

func (*GormTestUtils) CreateEntity

func (g *GormTestUtils) CreateEntity(t *testing.T, entity interface{})

func (*GormTestUtils) GetJSON

func (g *GormTestUtils) GetJSON(w *httptest.ResponseRecorder, e interface{})

Destructure the struct value from the returned response JSON

func (*GormTestUtils) Init

func (g *GormTestUtils) Init(m *testing.M, config *TestConfig)

type IGormTestUtils

type IGormTestUtils interface {
	// Main init config
	Init(m *testing.M, config TestConfig)
	CreateEntity(t *testing.T, entity interface{})
	// Destructure the struct value from the returned response JSON
	GetJSON(key string)
}

type TestConfig

type TestConfig struct {
	DB       *gorm.DB
	SetUp    func()
	Teardown func()
	Tables   []string
}

Jump to

Keyboard shortcuts

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