1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2024-11-16 17:41:57 +02:00
SQL driver mock for go with github.com/jmoiron/sqlx support
Go to file
2020-10-23 15:19:33 +03:00
examples closes #161 removes references to gopkg.in 2019-02-13 09:29:47 +02:00
.gitignore migration from custom namedValue to driver.NamedValue 2019-11-28 13:51:27 +03:00
.travis.yml Add go 1.15 in travis 2020-09-08 08:39:30 +09:00
argument_test.go Correct spelling 2017-11-25 09:32:17 -05:00
argument.go add AnyArg func to provide any kind of argument matcher 2016-02-23 11:24:34 +02:00
column_test.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
column.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
driver_test.go move options to a separate file 2018-09-14 11:03:24 +02:00
driver.go added jmoiron/sqlx support 2020-10-22 11:52:57 +03:00
expectations_before_go18_test.go added tests 2019-12-04 09:02:10 +03:00
expectations_before_go18.go added support for mammoths 2019-12-02 14:39:36 +03:00
expectations_go18_test.go added tests 2019-12-04 09:02:10 +03:00
expectations_go18.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
expectations_go19_test.go fix 2019-12-02 14:54:33 +03:00
expectations_test.go fix 2019-12-02 14:54:33 +03:00
expectations.go fix 2019-12-02 14:48:50 +03:00
go.mod updated go.mod 2020-10-22 11:56:45 +03:00
go.sum updated go.mod 2020-10-22 11:56:45 +03:00
LICENSE add go.mod, closes #160 2019-02-12 14:31:14 +02:00
options.go Add ExpectPing to watch for db Ping calls 2019-11-06 14:15:28 +00:00
query_test.go use configured QueryMatcher in order to match expected SQL to actual, closes #70 2018-12-11 15:20:44 +02:00
query.go Fixed some typos 2019-02-27 19:44:03 +00:00
README.md updated install command in readme 2020-10-23 15:19:33 +03:00
result_test.go Fixed some typos 2019-02-27 19:44:03 +00:00
result.go closes #5 2014-08-14 20:38:46 +03:00
rows_go13_test.go Invalidate memory scanned into sql.RawBytes 2019-06-21 17:03:05 +10:00
rows_go18_test.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
rows_go18.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
rows_test.go Invalidate memory scanned into sql.RawBytes 2019-06-21 17:03:05 +10:00
rows.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
sqlmock_before_go18_test.go Add ExpectPing to watch for db Ping calls 2019-11-06 14:15:28 +00:00
sqlmock_before_go18.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
sqlmock_go18_19.go support for usage of sql.Out as a param value 2019-06-04 17:57:33 -04:00
sqlmock_go18_test.go move tests 2019-12-04 10:44:50 +03:00
sqlmock_go18.go Add Column Metadata #152 2020-06-22 22:26:35 +02:00
sqlmock_go19_test.go added tests 2019-12-03 12:47:50 +03:00
sqlmock_go19.go added tests 2019-12-03 12:47:50 +03:00
sqlmock_test.go added jmoiron/sqlx support 2020-10-22 11:52:57 +03:00
sqlmock.go added jmoiron/sqlx support 2020-10-22 11:52:57 +03:00
statement_before_go18.go added tests 2019-12-03 12:47:50 +03:00
statement_go18.go added tests 2019-12-03 12:47:50 +03:00
statement_test.go Fixed some typos 2019-02-27 19:44:03 +00:00
statement.go added tests 2019-12-03 12:47:50 +03:00
stubs_test.go Fixed some typos 2019-02-27 19:44:03 +00:00

SQL driver mock for Golang (with jmoiron/sqlx support)

Forked from DATA-DOG/go-sqlmock

Added functionality

  • Newx() and NewxWithDNS() which returns *sqlx.DB object instead of *sql.DB

Install

go get -u github.com/zhashkevych/go-sqlxmock@master

Usage Example

Repository Implementation:

type UserRepository interface {
	Insert(user domain.User) (int, error)
	GetById(id int) (domain.User, error)
	Get(username, password string) (domain.User, error)
}


type UserRepository struct {
	db *sqlx.DB
}

func NewUserRepository(db *sqlx.DB) *UserRepository {
	return &UserRepository{db: db}
}

func (r *UserRepository) Insert(user domain.User) (int, error) {
	var id int
	row := r.db.QueryRow("INSERT INTO users (first_name, last_name, username, password) VALUES ($1, $2, $3, $4) RETURNING id",
		user.FirstName, user.LastName, user.Username, user.Password)
	if err := row.Scan(&id); err != nil {
		return 0, err
	}

	return id, nil
}

Unit Tests:

import (
	sqlxmock "github.com/zhashkevych/go-sqlxmock"
	"testing"
)

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

	s := NewUserRepository(db)

	tests := []struct {
		name    string
		s       repository.UserRepository
		user    domain.User
		mock    func()
		want    int
		wantErr bool
	}{
		{
			//When everything works as expected
			name: "OK",
			s:    s,
			user: domain.User{
				FirstName: "first_name",
				LastName:  "last_name",
				Username:  "username",
				Password:  "password",
			},
			mock: func() {
				rows := sqlxmock.NewRows([]string{"id"}).AddRow(1)
				mock.ExpectQuery("INSERT INTO users").WithArgs("first_name", "last_name", "username", "password").WillReturnRows(rows)
			},
			want: 1,
		},
		{
			name:  "Empty Fields",
			s:     s,
			user: domain.User{
				FirstName: "",
				LastName:  "",
				Username:  "username",
				Password:  "password",
			},
			mock: func() {
				rows := sqlxmock.NewRows([]string{"id"}) 
				mock.ExpectQuery("INSERT INTO users").WithArgs("first_name", "last_name", "username", "password").WillReturnRows(rows)
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.mock()
			got, err := tt.s.Insert(tt.user)
			if (err != nil) != tt.wantErr {
				t.Errorf("Get() error new = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if err == nil && got != tt.want {
				t.Errorf("Get() = %v, want %v", got, tt.want)
			}
		})
	}
}