igorm

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2019 License: MIT Imports: 2 Imported by: 1

README

igorm

Interface wrapper for gorm.DB

"This is an interface-equivalent of gorm.DB so that those who prefer mocking gorm.DB under test can easily do so. It also adds 2 functions Wrap() and Openw(), to facilitate creating gorm.Gormw instances."

Acknowledgments

This code was created entirely by littledot

My only contribution is to export to the external package and create documentation with an example

Why this is not in the gorm package?

Jinzhu, the creator of gorm claims that he is not a fan of mock testing without real database and does not want to change his package (see https://github.com/jinzhu/gorm/pull/805). That's why littledot introduced a simple, parallel interface that does not interfere strongly into the main package. Jinzhu, however, did not implement this function in the gorm, so the igorm package was created (see https://github.com/jinzhu/gorm/pull/1424).

In my opinion, it is always good to have additional options, in this case mock testing.

Example

First, create or change your existing functions to not use *gorm.DB but instead, use igorm.Gormw interface.

func getUser(db igorm.Gormw) *User {
}

From now on you are not forced to use a *gorm.DB instance, but you can use an instance of any structure that implements all methods from the Gormw interface. That structure is the wrapper available in this package. You can obtain it with Openw() function.

db, err := igorm.Openw(dialect, path)
if err != nil {
    log.Fatal(err)
}

And now it's possible.

user := getUser(db)

Of course, for mock testing or for other purposes, you can create your own structure implementing the Gormw interface (which has all its methods) and implement these methods depending on your needs. Then use it as an value to igorm.Gormw type arguements.

Wrapper template

Creating your wrapper can be tiring because it has to implement 74 functions from the Gormw interface. That's why I did it for you.

Look at igorm-mock-template.

Note about interfaces and pointers

Gorm is ussually used as *gorm.DB (pointer to database instance). For interfaces use values. See explanation at this link

// NOT

func getUser(db *igorm.Gormw) *User {
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gormw

type Gormw interface {
	Close() error
	DB() *sql.DB
	New() Gormw
	NewScope(value interface{}) *gorm.Scope
	CommonDB() gorm.SQLCommon
	Callback() *gorm.Callback
	SetLogger(l gorm.Logger)
	LogMode(enable bool) Gormw
	SingularTable(enable bool)
	Where(query interface{}, args ...interface{}) Gormw
	Or(query interface{}, args ...interface{}) Gormw
	Not(query interface{}, args ...interface{}) Gormw
	Limit(value int) Gormw
	Offset(value int) Gormw
	Order(value string, reorder ...bool) Gormw
	Select(query interface{}, args ...interface{}) Gormw
	Omit(columns ...string) Gormw
	Group(query string) Gormw
	Having(query string, values ...interface{}) Gormw
	Joins(query string, args ...interface{}) Gormw
	Scopes(funcs ...func(*gorm.DB) *gorm.DB) Gormw
	Unscoped() Gormw
	Attrs(attrs ...interface{}) Gormw
	Assign(attrs ...interface{}) Gormw
	First(out interface{}, where ...interface{}) Gormw
	Last(out interface{}, where ...interface{}) Gormw
	Find(out interface{}, where ...interface{}) Gormw
	Scan(dest interface{}) Gormw
	Row() *sql.Row
	Rows() (*sql.Rows, error)
	ScanRows(rows *sql.Rows, result interface{}) error
	Pluck(column string, value interface{}) Gormw
	Count(value interface{}) Gormw
	Related(value interface{}, foreignKeys ...string) Gormw
	FirstOrInit(out interface{}, where ...interface{}) Gormw
	FirstOrCreate(out interface{}, where ...interface{}) Gormw
	Update(attrs ...interface{}) Gormw
	Updates(values interface{}, ignoreProtectedAttrs ...bool) Gormw
	UpdateColumn(attrs ...interface{}) Gormw
	UpdateColumns(values interface{}) Gormw
	Save(value interface{}) Gormw
	Create(value interface{}) Gormw
	Delete(value interface{}, where ...interface{}) Gormw
	Raw(sql string, values ...interface{}) Gormw
	Exec(sql string, values ...interface{}) Gormw
	Model(value interface{}) Gormw
	Table(name string) Gormw
	Debug() Gormw
	Begin() Gormw
	Commit() Gormw
	Rollback() Gormw
	RollbackUnlessCommitted() Gormw
	NewRecord(value interface{}) bool
	RecordNotFound() bool
	CreateTable(values ...interface{}) Gormw
	DropTable(values ...interface{}) Gormw
	DropTableIfExists(values ...interface{}) Gormw
	HasTable(value interface{}) bool
	AutoMigrate(values ...interface{}) Gormw
	ModifyColumn(column string, typ string) Gormw
	DropColumn(column string) Gormw
	AddIndex(indexName string, column ...string) Gormw
	AddUniqueIndex(indexName string, column ...string) Gormw
	RemoveIndex(indexName string) Gormw
	AddForeignKey(field string, dest string, onDelete string, onUpdate string) Gormw
	Association(column string) *gorm.Association
	Preload(column string, conditions ...interface{}) Gormw
	Set(name string, value interface{}) Gormw
	InstantSet(name string, value interface{}) Gormw
	Get(name string) (value interface{}, ok bool)
	SetJoinTableHandler(source interface{}, column string, handler gorm.JoinTableHandlerInterface)
	AddError(err error) error
	GetErrors() (errors []error)

	// extra
	Error() error
	RowsAffected() int64
}

Gormw is an interface which DB implements

func Openw

func Openw(dialect string, args ...interface{}) (db Gormw, err error)

Openw is a drop-in replacement for Open()

func Wrap

func Wrap(db *gorm.DB) Gormw

Wrap wraps gorm.DB in an interface

Jump to

Keyboard shortcuts

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