-
-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from DATA-DOG/go1.8
Support for go 1.8 SQL features
- Loading branch information
Showing
14 changed files
with
1,030 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ go: | |
- 1.5 | ||
- 1.6 | ||
- 1.7 | ||
- 1.8 | ||
- tip | ||
|
||
script: go test -race -coverprofile=coverage.txt -covermode=atomic | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// +build !go1.8 | ||
|
||
package sqlmock | ||
|
||
import ( | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// WillReturnRows specifies the set of resulting rows that will be returned | ||
// by the triggered query | ||
func (e *ExpectedQuery) WillReturnRows(rows *Rows) *ExpectedQuery { | ||
e.rows = &rowSets{sets: []*Rows{rows}} | ||
return e | ||
} | ||
|
||
func (e *queryBasedExpectation) argsMatches(args []namedValue) error { | ||
if nil == e.args { | ||
return nil | ||
} | ||
if len(args) != len(e.args) { | ||
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args)) | ||
} | ||
for k, v := range args { | ||
// custom argument matcher | ||
matcher, ok := e.args[k].(Argument) | ||
if ok { | ||
// @TODO: does it make sense to pass value instead of named value? | ||
if !matcher.Match(v.Value) { | ||
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k]) | ||
} | ||
continue | ||
} | ||
|
||
dval := e.args[k] | ||
// convert to driver converter | ||
darg, err := driver.DefaultParameterConverter.ConvertValue(dval) | ||
if err != nil { | ||
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err) | ||
} | ||
|
||
if !driver.IsValue(darg) { | ||
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg) | ||
} | ||
|
||
if !reflect.DeepEqual(darg, v.Value) { | ||
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// +build go1.8 | ||
|
||
package sqlmock | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// WillReturnRows specifies the set of resulting rows that will be returned | ||
// by the triggered query | ||
func (e *ExpectedQuery) WillReturnRows(rows ...*Rows) *ExpectedQuery { | ||
sets := make([]*Rows, len(rows)) | ||
for i, r := range rows { | ||
sets[i] = r | ||
} | ||
e.rows = &rowSets{sets: sets} | ||
return e | ||
} | ||
|
||
func (e *queryBasedExpectation) argsMatches(args []namedValue) error { | ||
if nil == e.args { | ||
return nil | ||
} | ||
if len(args) != len(e.args) { | ||
return fmt.Errorf("expected %d, but got %d arguments", len(e.args), len(args)) | ||
} | ||
// @TODO should we assert either all args are named or ordinal? | ||
for k, v := range args { | ||
// custom argument matcher | ||
matcher, ok := e.args[k].(Argument) | ||
if ok { | ||
if !matcher.Match(v.Value) { | ||
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k]) | ||
} | ||
continue | ||
} | ||
|
||
dval := e.args[k] | ||
if named, isNamed := dval.(sql.NamedArg); isNamed { | ||
dval = named.Value | ||
if v.Name != named.Name { | ||
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name) | ||
} | ||
} else if k+1 != v.Ordinal { | ||
return fmt.Errorf("argument %d: ordinal position: %d does not match expected: %d", k, k+1, v.Ordinal) | ||
} | ||
|
||
// convert to driver converter | ||
darg, err := driver.DefaultParameterConverter.ConvertValue(dval) | ||
if err != nil { | ||
return fmt.Errorf("could not convert %d argument %T - %+v to driver value: %s", k, e.args[k], e.args[k], err) | ||
} | ||
|
||
if !driver.IsValue(darg) { | ||
return fmt.Errorf("argument %d: non-subset type %T returned from Value", k, darg) | ||
} | ||
|
||
if !reflect.DeepEqual(darg, v.Value) { | ||
return fmt.Errorf("argument %d expected [%T - %+v] does not match actual [%T - %+v]", k, darg, darg, v.Value, v.Value) | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// +build go1.8 | ||
|
||
package sqlmock | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"testing" | ||
) | ||
|
||
func TestQueryExpectationNamedArgComparison(t *testing.T) { | ||
e := &queryBasedExpectation{} | ||
against := []namedValue{{Value: int64(5), Name: "id"}} | ||
if err := e.argsMatches(against); err != nil { | ||
t.Errorf("arguments should match, since the no expectation was set, but got err: %s", err) | ||
} | ||
|
||
e.args = []driver.Value{ | ||
sql.Named("id", 5), | ||
sql.Named("s", "str"), | ||
} | ||
|
||
if err := e.argsMatches(against); err == nil { | ||
t.Error("arguments should not match, since the size is not the same") | ||
} | ||
|
||
against = []namedValue{ | ||
{Value: int64(5), Name: "id"}, | ||
{Value: "str", Name: "s"}, | ||
} | ||
|
||
if err := e.argsMatches(against); err != nil { | ||
t.Errorf("arguments should have matched, but it did not: %v", err) | ||
} | ||
|
||
against = []namedValue{ | ||
{Value: int64(5), Name: "id"}, | ||
{Value: "str", Name: "username"}, | ||
} | ||
|
||
if err := e.argsMatches(against); err == nil { | ||
t.Error("arguments matched, but it should have not due to Name") | ||
} | ||
|
||
e.args = []driver.Value{int64(5), "str"} | ||
|
||
against = []namedValue{ | ||
{Value: int64(5), Ordinal: 0}, | ||
{Value: "str", Ordinal: 1}, | ||
} | ||
|
||
if err := e.argsMatches(against); err == nil { | ||
t.Error("arguments matched, but it should have not due to wrong Ordinal position") | ||
} | ||
|
||
against = []namedValue{ | ||
{Value: int64(5), Ordinal: 1}, | ||
{Value: "str", Ordinal: 2}, | ||
} | ||
|
||
if err := e.argsMatches(against); err != nil { | ||
t.Errorf("arguments should have matched, but it did not: %v", err) | ||
} | ||
} |
Oops, something went wrong.