1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-01-10 00:28:42 +02:00
go-sqlmock/expectations_test.go

106 lines
3.0 KiB
Go
Raw Normal View History

2014-02-05 17:21:07 +03:00
package sqlmock
import (
2014-05-29 17:11:13 +03:00
"database/sql/driver"
"fmt"
"regexp"
2014-05-29 17:11:13 +03:00
"testing"
"time"
2014-02-05 17:21:07 +03:00
)
func TestQueryExpectationArgComparison(t *testing.T) {
2014-05-29 17:11:13 +03:00
e := &queryBasedExpectation{}
2016-02-23 11:14:34 +02:00
against := []driver.Value{int64(5)}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err)
2014-05-29 17:11:13 +03:00
}
e.args = []driver.Value{5, "str"}
2016-02-23 11:14:34 +02:00
against = []driver.Value{int64(5)}
if err := e.argsMatches(against); err == nil {
2014-05-29 17:11:13 +03:00
t.Error("arguments should not match, since the size is not the same")
}
2016-02-23 11:14:34 +02:00
against = []driver.Value{int64(3), "str"}
if err := e.argsMatches(against); err == nil {
2014-05-29 17:11:13 +03:00
t.Error("arguments should not match, since the first argument (int value) is different")
}
2016-02-23 11:14:34 +02:00
against = []driver.Value{int64(5), "st"}
if err := e.argsMatches(against); err == nil {
2014-05-29 17:11:13 +03:00
t.Error("arguments should not match, since the second argument (string value) is different")
}
2016-02-23 11:14:34 +02:00
against = []driver.Value{int64(5), "str"}
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
2014-05-29 17:11:13 +03:00
}
const longForm = "Jan 2, 2006 at 3:04pm (MST)"
tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
2016-02-23 11:14:34 +02:00
e.args = []driver.Value{5, tm}
2014-05-29 17:11:13 +03:00
2016-02-23 11:14:34 +02:00
against = []driver.Value{int64(5), tm}
if err := e.argsMatches(against); err != nil {
t.Error("arguments should match, but it did not")
2014-05-29 17:11:13 +03:00
}
e.args = []driver.Value{5, AnyArg()}
2016-02-23 11:14:34 +02:00
if err := e.argsMatches(against); err != nil {
t.Errorf("arguments should match, but it did not: %s", err)
2014-05-29 17:11:13 +03:00
}
2014-02-05 17:21:07 +03:00
}
2016-01-26 16:19:23 +02:00
func TestQueryExpectationArgComparisonBool(t *testing.T) {
var e *queryBasedExpectation
e = &queryBasedExpectation{args: []driver.Value{true}}
against := []driver.Value{true}
2016-02-23 11:14:34 +02:00
if err := e.argsMatches(against); err != nil {
2016-01-26 16:19:23 +02:00
t.Error("arguments should match, since arguments are the same")
}
e = &queryBasedExpectation{args: []driver.Value{false}}
against = []driver.Value{false}
2016-02-23 11:14:34 +02:00
if err := e.argsMatches(against); err != nil {
2016-01-26 16:19:23 +02:00
t.Error("arguments should match, since argument are the same")
}
e = &queryBasedExpectation{args: []driver.Value{true}}
against = []driver.Value{false}
2016-02-23 11:14:34 +02:00
if err := e.argsMatches(against); err == nil {
2016-01-26 16:19:23 +02:00
t.Error("arguments should not match, since argument is different")
}
e = &queryBasedExpectation{args: []driver.Value{false}}
against = []driver.Value{true}
2016-02-23 11:14:34 +02:00
if err := e.argsMatches(against); err == nil {
2016-01-26 16:19:23 +02:00
t.Error("arguments should not match, since argument is different")
}
}
func TestQueryExpectationSqlMatch(t *testing.T) {
e := &ExpectedExec{}
2016-01-26 16:19:23 +02:00
e.sqlRegex = regexp.MustCompile("SELECT x FROM")
if !e.queryMatches("SELECT x FROM someting") {
t.Errorf("Sql must have matched the query")
}
e.sqlRegex = regexp.MustCompile("SELECT COUNT\\(x\\) FROM")
if !e.queryMatches("SELECT COUNT(x) FROM someting") {
t.Errorf("Sql must have matched the query")
}
}
2016-01-21 21:09:44 +02:00
func ExampleExpectedExec() {
db, mock, _ := New()
result := NewErrorResult(fmt.Errorf("some error"))
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
res, _ := db.Exec("INSERT something")
_, err := res.LastInsertId()
fmt.Println(err)
// Output: some error
}