GithubHelp home page GithubHelp logo

igorm's Introduction

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

igorm's People

Contributors

kacperjurak avatar happyyi008 avatar

Stargazers

Franco Gino Ferraguti ๐Ÿฆ” avatar Milad Roudgarian avatar  avatar Toan Nhu avatar Taiga Miyazaki avatar ik5 avatar Benjamin Morehouse avatar dracarch avatar Tarek Badr avatar Emma Thompson avatar Brent Hughes avatar Max Blaushild avatar

Watchers

James Cloos avatar  avatar

igorm's Issues

Tag a release

Hello! We are using this package through artifactory and there are certain issues with fetching an unversioned packaged. Can you tag latest master as something like v1.0.0?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.