1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2024-11-28 08:49:01 +02:00

added tests

This commit is contained in:
Nikita Koryabkin 2019-12-04 10:38:13 +03:00
parent 03d1707c76
commit 8cf05c23ad

View File

@ -654,17 +654,28 @@ func Test_sqlmock_Exec(t *testing.T) {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
query := "SELECT name, email FROM users WHERE name = ?"
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(errors.New("matcher %T could not match %d argument %T - %+v")).
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)
@ -675,7 +686,14 @@ func Test_sqlmock_Exec(t *testing.T) {
return
}
_, err = mock.(*sqlmock).Exec(query, []driver.Value{failArgument{}})
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
@ -697,4 +715,9 @@ func Test_sqlmock_Query(t *testing.T) {
return
}
defer rows.Close()
_, err = mock.(*sqlmock).Query(query, []driver.Value{failArgument{}})
if err == nil {
t.Errorf("error expected")
return
}
}