Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Koryabkin committed Dec 4, 2019
1 parent 8cf05c2 commit 4fbf605
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 82 deletions.
82 changes: 0 additions & 82 deletions sqlmock_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ package sqlmock
import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"reflect"
"testing"
"time"
)
Expand Down Expand Up @@ -641,83 +639,3 @@ func TestPingExpectationsContextTimeout(t *testing.T) {
t.Errorf("expected Ping to return after context timeout, but it did not in a timely fashion")
}
}

type failArgument struct{}

func (f failArgument) Match(_ driver.Value) bool {
return false
}

func Test_sqlmock_Exec(t *testing.T) {
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.ExpectBegin()
_, err = mock.(*sqlmock).Exec("", []driver.Value{})
if err == nil {
t.Errorf("error expected")
return
}

expected := NewResult(1, 1)
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
WillReturnResult(expected).
WithArgs("test")

matchErr := errors.New("matcher sqlmock.failArgument could not match 0 argument driver.NamedValue - {Name: Ordinal:1 Value:{}}")
mock.ExpectExec("SELECT (.+) FROM animals WHERE (.+)").
WillReturnError(matchErr).
WithArgs(failArgument{})

mock.ExpectExec("").WithArgs(failArgument{})

mock.(*sqlmock).expected = mock.(*sqlmock).expected[1:]
query := "SELECT name, email FROM users WHERE name = ?"
result, err := mock.(*sqlmock).Exec(query, []driver.Value{"test"})
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Results are not equal. Expected: %v, Actual: %v", expected, result)
return
}

failQuery := "SELECT name, sex FROM animals WHERE sex = ?"
_, err = mock.(*sqlmock).Exec(failQuery, []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
mock.(*sqlmock).ordered = false
_, err = mock.(*sqlmock).Exec("", []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
}

func Test_sqlmock_Query(t *testing.T) {
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()
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "[email protected]")
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows)
query := "SELECT name, email FROM users WHERE name = ?"
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
if err != nil {
t.Error(err)
return
}
defer rows.Close()
_, err = mock.(*sqlmock).Query(query, []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
}
80 changes: 80 additions & 0 deletions sqlmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1260,3 +1260,83 @@ func Test_sqlmock_Prepare_and_Exec(t *testing.T) {
}
defer rows.Close()
}

type failArgument struct{}

func (f failArgument) Match(_ driver.Value) bool {
return false
}

func Test_sqlmock_Exec(t *testing.T) {
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.ExpectBegin()
_, err = mock.(*sqlmock).Exec("", []driver.Value{})
if err == nil {
t.Errorf("error expected")
return
}

expected := NewResult(1, 1)
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
WillReturnResult(expected).
WithArgs("test")

matchErr := errors.New("matcher sqlmock.failArgument could not match 0 argument driver.NamedValue - {Name: Ordinal:1 Value:{}}")
mock.ExpectExec("SELECT (.+) FROM animals WHERE (.+)").
WillReturnError(matchErr).
WithArgs(failArgument{})

mock.ExpectExec("").WithArgs(failArgument{})

mock.(*sqlmock).expected = mock.(*sqlmock).expected[1:]
query := "SELECT name, email FROM users WHERE name = ?"
result, err := mock.(*sqlmock).Exec(query, []driver.Value{"test"})
if err != nil {
t.Error(err)
return
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("Results are not equal. Expected: %v, Actual: %v", expected, result)
return
}

failQuery := "SELECT name, sex FROM animals WHERE sex = ?"
_, err = mock.(*sqlmock).Exec(failQuery, []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
mock.(*sqlmock).ordered = false
_, err = mock.(*sqlmock).Exec("", []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
}

func Test_sqlmock_Query(t *testing.T) {
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()
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "[email protected]")
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows)
query := "SELECT name, email FROM users WHERE name = ?"
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
if err != nil {
t.Error(err)
return
}
defer rows.Close()
_, err = mock.(*sqlmock).Query(query, []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
}

0 comments on commit 4fbf605

Please sign in to comment.