mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-19 20:57:50 +02:00
added tests
This commit is contained in:
parent
5a7ddb9845
commit
5dc976bc43
@ -5,7 +5,9 @@ package sqlmock
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@ -639,3 +641,43 @@ func TestPingExpectationsContextTimeout(t *testing.T) {
|
||||
t.Errorf("expected Ping to return after context timeout, but it did not in a timely fashion")
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
query := "SELECT name, email FROM users WHERE name = ?"
|
||||
|
||||
expected := NewResult(1, 1)
|
||||
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
|
||||
WillReturnResult(expected)
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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", "test@example.com")
|
||||
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()
|
||||
}
|
||||
|
@ -1250,7 +1250,7 @@ func Test_sqlmock_Prepare_and_Exec(t *testing.T) {
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(result, expected) {
|
||||
t.Errorf("Eesults not equal. Expected: %v, Actual: %v", expected, result)
|
||||
t.Errorf("Results are not equal. Expected: %v, Actual: %v", expected, result)
|
||||
return
|
||||
}
|
||||
rows, err := got.Query([]driver.Value{"test"})
|
||||
|
Loading…
x
Reference in New Issue
Block a user