Skip to content

Commit 5dc976b

Browse files
author
Nikita Koryabkin
committed
added tests
1 parent 5a7ddb9 commit 5dc976b

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

sqlmock_go18_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ package sqlmock
55
import (
66
"context"
77
"database/sql"
8+
"database/sql/driver"
89
"errors"
10+
"reflect"
911
"testing"
1012
"time"
1113
)
@@ -639,3 +641,43 @@ func TestPingExpectationsContextTimeout(t *testing.T) {
639641
t.Errorf("expected Ping to return after context timeout, but it did not in a timely fashion")
640642
}
641643
}
644+
645+
func Test_sqlmock_Exec(t *testing.T) {
646+
db, mock, err := New()
647+
if err != nil {
648+
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
649+
}
650+
defer db.Close()
651+
query := "SELECT name, email FROM users WHERE name = ?"
652+
653+
expected := NewResult(1, 1)
654+
mock.ExpectExec("SELECT (.+) FROM users WHERE (.+)").
655+
WillReturnResult(expected)
656+
657+
result, err := mock.(*sqlmock).Exec(query, []driver.Value{"test"})
658+
if err != nil {
659+
t.Error(err)
660+
return
661+
}
662+
if !reflect.DeepEqual(result, expected) {
663+
t.Errorf("Results are not equal. Expected: %v, Actual: %v", expected, result)
664+
return
665+
}
666+
}
667+
668+
func Test_sqlmock_Query(t *testing.T) {
669+
db, mock, err := New()
670+
if err != nil {
671+
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
672+
}
673+
defer db.Close()
674+
expectedRows := mock.NewRows([]string{"id", "name", "email"}).AddRow(1, "test", "test@example.com")
675+
mock.ExpectQuery("SELECT (.+) FROM users WHERE (.+)").WillReturnRows(expectedRows)
676+
query := "SELECT name, email FROM users WHERE name = ?"
677+
rows, err := mock.(*sqlmock).Query(query, []driver.Value{"test"})
678+
if err != nil {
679+
t.Error(err)
680+
return
681+
}
682+
defer rows.Close()
683+
}

sqlmock_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,7 @@ func Test_sqlmock_Prepare_and_Exec(t *testing.T) {
12501250
return
12511251
}
12521252
if !reflect.DeepEqual(result, expected) {
1253-
t.Errorf("Eesults not equal. Expected: %v, Actual: %v", expected, result)
1253+
t.Errorf("Results are not equal. Expected: %v, Actual: %v", expected, result)
12541254
return
12551255
}
12561256
rows, err := got.Query([]driver.Value{"test"})

0 commit comments

Comments
 (0)