Go GORM & SQL mocking library
Clone or download
Latest commit 0d27527 Oct 29, 2018

README.md

GoDoc Build Status Go Report Card

Go-Mocket

Go-Mocket is library inspired by DATA-DOG/go-sqlmock As inspiration library, it is the implementation of sql/driver interface but at the same time follows different approaches and has only a similar API. This library helps to mock any DB connection also with jinzhu/gorm, and it was the main goal to create it

List of features in the library:

  • Mock INSERT, UPDATE, SELECT, DELETE
  • Support of transactions
  • 2 API's to use - chaining and via specifying a whole mock object
  • Matching by prepared statements arguments
  • You don't require to change anything inside your code to start using this library
  • Ability to trigger exceptions
  • Attach callbacks to mocked response to add an additional check or modify a response

NOTE, Please be aware that driver catches SQL without DB specifics. Generating of queries is done by SQL package

Install

go get github.com/Selvatico/go-mocket

Usage

There are two possible ways to use mocket:

  • Chaining API
  • Specifying FakeResponse object with all fields manually. Could be useful for cases when mocks stored separately as the list of FakeResponses.
Enabling driver

Somewhere in your code, do this to set up a tests

import (
    "database/sql"
    mocket "github.com/Selvatico/go-mocket"
    "github.com/jinzhu/gorm"
)

func SetupTests() {
    mocket.Catcher.Register()
    // GORM
    db, err := gorm.Open(mocket.DriverName, "any_string") // Could be any connection string
    app.DB = db // Assumption that it will be used everywhere the same
    //OR 
    // Regular sql package usage
    db, err := sql.Open(mocket.DriverName, "any_string")
}

Now, if you use a singleton instance of DB, it will use a mocked connection everywhere.

Chain usage
Example of mocking by pattern
import mocket "github.com/Selvatico/go-mocket"
import "net/http/httptest"

func TestHandler(t *testing.T) {
    request := httptest.NewRequest("POST", "/application", nil)
    recorder := httptest.NewRecorder()

    GlobalMock := mocket.Catcher
    GlobalMock.Logging = true // log mocket behavior

    // field names here mapped to the database schema
    commonReply := []map[string]interface{}{{"some_id": "2", "field": "value"}}
    // Mock only by query pattern
    GlobalMock.NewMock().WithQuery(`"campaigns".name IS NULL AND (("uuid" = test_uuid))`).WithReply(commonReply)
    Post(recorder, request) // call handler

    r := recorder.Result()
    body, _ := ioutil.ReadAll(r.Body)

    // some assertion about results
    //...
}

Documentation

For More Documentation please check Wiki Documentation

License

MIT License

Copyright (c) 2017 Seredenko Dmitry

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.