All Projects → pashagolub → pgxmock

pashagolub / pgxmock

Licence: other
pgx mock driver for golang to test database interactions

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to pgxmock

PixelTest
Fast, modern, simple iOS snapshot testing written purely in Swift.
Stars: ✭ 56 (-42.27%)
Mutual labels:  tdd
ginkgo4j
A Java BDD Testing Framework (based on RSpec and Ginkgo)
Stars: ✭ 25 (-74.23%)
Mutual labels:  tdd
baserepo
Base repository
Stars: ✭ 71 (-26.8%)
Mutual labels:  tdd
kata
TDD, Refactoring kata in many languages
Stars: ✭ 14 (-85.57%)
Mutual labels:  tdd
IreneBot
Irene Bot for Discord in Python
Stars: ✭ 15 (-84.54%)
Mutual labels:  postgres
cukinia
A simple on-target system test framework for Linux
Stars: ✭ 24 (-75.26%)
Mutual labels:  tdd
mockit
A tool that integrates SQL, HTTP,interface,Redis mock
Stars: ✭ 13 (-86.6%)
Mutual labels:  sqlmock
clunk
Clojure Postgres w/out JDBC
Stars: ✭ 25 (-74.23%)
Mutual labels:  postgres
heltin
Robust client registry for individuals receiving mental healthcare services.
Stars: ✭ 18 (-81.44%)
Mutual labels:  postgres
pg-search-sequelize
Postgres full-text search in Node.js and Sequelize.
Stars: ✭ 31 (-68.04%)
Mutual labels:  postgres
solidity-tdd
Solidity Test Driven Development Boilerplate Project
Stars: ✭ 27 (-72.16%)
Mutual labels:  tdd
bdd-for-all
Flexible and easy to use library to enable your behavorial driven development (BDD) teams to easily collaborate while promoting automation, transparency and reporting.
Stars: ✭ 42 (-56.7%)
Mutual labels:  tdd
mocha-cakes-2
A BDD plugin for Mocha testing framework
Stars: ✭ 44 (-54.64%)
Mutual labels:  tdd
utest
Lightweight unit testing framework for C/C++ projects. Suitable for embedded devices.
Stars: ✭ 18 (-81.44%)
Mutual labels:  tdd
postgresql lwrp
Express 42 postgresql cookbook
Stars: ✭ 57 (-41.24%)
Mutual labels:  postgres
django-test-addons
Testing support for different database system like Mongo, Redis, Neo4j, Memcache, Django Rest Framework for django
Stars: ✭ 20 (-79.38%)
Mutual labels:  tdd
event bus postgres
🐘 Postgres event store for event_bus
Stars: ✭ 49 (-49.48%)
Mutual labels:  postgres
java-8-matchers
Hamcrest Matchers for Java 8 features
Stars: ✭ 23 (-76.29%)
Mutual labels:  tdd
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (-48.45%)
Mutual labels:  postgres
shyft
⬡ Shyft is a server-side framework for building powerful GraphQL APIs 🚀
Stars: ✭ 56 (-42.27%)
Mutual labels:  postgres

Go Reference Go Report Card Coverage Status

pgx driver mock for Golang

pgxmock is a mock library implementing pgx - PostgreSQL Driver and Toolkit. It's based on the well-known sqlmock library for sql/driver.

pgxmock has one and only purpose - to simulate pgx behavior in tests, without needing a real database connection. It helps to maintain correct TDD workflow.

  • this library is not complete but considered to be stable (issues and pull requests are welcome);
  • written based on go1.15 version, however, should be compatible with go1.11 and above;
  • does not require any modifications to your source code;
  • has strict by default expectation order matching;
  • has no third party dependencies except pgx packages.

Install

go get github.com/pashagolub/pgxmock

Documentation and Examples

Visit godoc for general examples and public api reference.

See implementation examples:

Something you may want to test

package main

import (
	"context"

	pgx "github.com/jackc/pgx/v5"
)

type PgxIface interface {
	Begin(context.Context) (pgx.Tx, error)
	Close(context.Context) error
}

func recordStats(db PgxIface, userID, productID int) (err error) {
	tx, err := db.Begin(context.Background())
	if err != nil {
		return
	}

	defer func() {
		switch err {
		case nil:
			err = tx.Commit(context.Background())
		default:
			_ = tx.Rollback(context.Background())
		}
	}()

	if _, err = tx.Exec(context.Background(), "UPDATE products SET views = views + 1"); err != nil {
		return
	}
	if _, err = tx.Exec(context.Background(), "INSERT INTO product_viewers (user_id, product_id) VALUES (?, ?)", userID, productID); err != nil {
		return
	}
	return
}

func main() {
	// @NOTE: the real connection is not required for tests
	db, err := pgx.Connect(context.Background(), "postgres://rolname@hostname/dbname")
	if err != nil {
		panic(err)
	}
	defer db.Close(context.Background())

	if err = recordStats(db, 1 /*some user id*/, 5 /*some product id*/); err != nil {
		panic(err)
	}
}

Tests with pgxmock

package main

import (
	"context"
	"fmt"
	"testing"

	"github.com/pashagolub/pgxmock"
)

// a successful case
func TestShouldUpdateStats(t *testing.T) {
	mock, err := pgxmock.NewConn()
	if err != nil {
		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
	}
	defer mock.Close(context.Background())

	mock.ExpectBegin()
	mock.ExpectExec("UPDATE products").WillReturnResult(pgxmock.NewResult("UPDATE", 1))
	mock.ExpectExec("INSERT INTO product_viewers").WithArgs(2, 3).WillReturnResult(pgxmock.NewResult("INSERT", 1))
	mock.ExpectCommit()

	// now we execute our method
	if err = recordStats(mock, 2, 3); err != nil {
		t.Errorf("error was not expected while updating stats: %s", err)
	}

	// we make sure that all expectations were met
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expectations: %s", err)
	}
}

// a failing test case
func TestShouldRollbackStatUpdatesOnFailure(t *testing.T) {
	mock, err := pgxmock.NewConn()
	if err != nil {
		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
	}
	defer mock.Close(context.Background())

	mock.ExpectBegin()
	mock.ExpectExec("UPDATE products").WillReturnResult(pgxmock.NewResult("UPDATE", 1))
	mock.ExpectExec("INSERT INTO product_viewers").
		WithArgs(2, 3).
		WillReturnError(fmt.Errorf("some error"))
	mock.ExpectRollback()

	// now we execute our method
	if err = recordStats(mock, 2, 3); err == nil {
		t.Errorf("was expecting an error, but there was none")
	}

	// we make sure that all expectations were met
	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expectations: %s", err)
	}
}

Customize SQL query matching

There were plenty of requests from users regarding SQL query string validation or different matching option. We have now implemented the QueryMatcher interface, which can be passed through an option when calling pgxmock.New or pgxmock.NewWithDSN.

This now allows to include some library, which would allow for example to parse and validate SQL AST. And create a custom QueryMatcher in order to validate SQL in sophisticated ways.

By default, pgxmock is preserving backward compatibility and default query matcher is pgxmock.QueryMatcherRegexp which uses expected SQL string as a regular expression to match incoming query string. There is an equality matcher: QueryMatcherEqual which will do a full case sensitive match.

In order to customize the QueryMatcher, use the following:

	mock, err := pgxmock.New(context.Background(), sqlmock.QueryMatcherOption(pgxmock.QueryMatcherEqual))

The query matcher can be fully customized based on user needs. pgxmock will not provide a standard sql parsing matchers.

Matching arguments like time.Time

There may be arguments which are of struct type and cannot be compared easily by value like time.Time. In this case pgxmock provides an Argument interface which can be used in more sophisticated matching. Here is a simple example of time argument matching:

type AnyTime struct{}

// Match satisfies sqlmock.Argument interface
func (a AnyTime) Match(v interface{}) bool {
	_, ok := v.(time.Time)
	return ok
}

func TestAnyTimeArgument(t *testing.T) {
	t.Parallel()
	db, mock, err := New()
	if err != nil {
		t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
	}
	defer db.Close()

	mock.ExpectExec("INSERT INTO users").
		WithArgs("john", AnyTime{}).
		WillReturnResult(NewResult(1, 1))

	_, err = db.Exec("INSERT INTO users(name, created_at) VALUES (?, ?)", "john", time.Now())
	if err != nil {
		t.Errorf("error '%s' was not expected, while inserting a row", err)
	}

	if err := mock.ExpectationsWereMet(); err != nil {
		t.Errorf("there were unfulfilled expectations: %s", err)
	}
}

It only asserts that argument is of time.Time type.

Run tests

go test -race

Contributions

Feel free to open a pull request. Note, if you wish to contribute an extension to public (exported methods or types) - please open an issue before, to discuss whether these changes can be accepted. All backward incompatible changes are and will be treated cautiously

License

The three clause BSD license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].