...

Source file src/github.com/DATA-DOG/go-sqlmock/expectations_go18_test.go

Documentation: github.com/DATA-DOG/go-sqlmock

     1  //go:build go1.8
     2  // +build go1.8
     3  
     4  package sqlmock
     5  
     6  import (
     7  	"database/sql"
     8  	"database/sql/driver"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestQueryExpectationArgComparison(t *testing.T) {
    14  	e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
    15  	against := []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
    16  	if err := e.argsMatches(against); err == nil {
    17  		t.Error("arguments should not match, since argument was passed, but noArgs was set")
    18  	}
    19  
    20  	e.noArgs = false
    21  	if err := e.argsMatches(against); err != nil {
    22  		t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
    23  	}
    24  
    25  	e.args = []driver.Value{5, "str"}
    26  
    27  	against = []driver.NamedValue{{Value: int64(5), Ordinal: 1}}
    28  	if err := e.argsMatches(against); err == nil {
    29  		t.Error("arguments should not match, since the size is not the same")
    30  	}
    31  
    32  	against = []driver.NamedValue{
    33  		{Value: int64(3), Ordinal: 1},
    34  		{Value: "str", Ordinal: 2},
    35  	}
    36  	if err := e.argsMatches(against); err == nil {
    37  		t.Error("arguments should not match, since the first argument (int value) is different")
    38  	}
    39  
    40  	against = []driver.NamedValue{
    41  		{Value: int64(5), Ordinal: 1},
    42  		{Value: "st", Ordinal: 2},
    43  	}
    44  	if err := e.argsMatches(against); err == nil {
    45  		t.Error("arguments should not match, since the second argument (string value) is different")
    46  	}
    47  
    48  	against = []driver.NamedValue{
    49  		{Value: int64(5), Ordinal: 1},
    50  		{Value: "str", Ordinal: 2},
    51  	}
    52  	if err := e.argsMatches(against); err != nil {
    53  		t.Errorf("arguments should match, but it did not: %s", err)
    54  	}
    55  
    56  	const longForm = "Jan 2, 2006 at 3:04pm (MST)"
    57  	tm, _ := time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
    58  	e.args = []driver.Value{5, tm}
    59  
    60  	against = []driver.NamedValue{
    61  		{Value: int64(5), Ordinal: 1},
    62  		{Value: tm, Ordinal: 2},
    63  	}
    64  	if err := e.argsMatches(against); err != nil {
    65  		t.Error("arguments should match, but it did not")
    66  	}
    67  
    68  	e.args = []driver.Value{5, AnyArg()}
    69  	if err := e.argsMatches(against); err != nil {
    70  		t.Errorf("arguments should match, but it did not: %s", err)
    71  	}
    72  }
    73  
    74  func TestQueryExpectationArgComparisonBool(t *testing.T) {
    75  	var e *queryBasedExpectation
    76  
    77  	e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
    78  	against := []driver.NamedValue{
    79  		{Value: true, Ordinal: 1},
    80  	}
    81  	if err := e.argsMatches(against); err != nil {
    82  		t.Error("arguments should match, since arguments are the same")
    83  	}
    84  
    85  	e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
    86  	against = []driver.NamedValue{
    87  		{Value: false, Ordinal: 1},
    88  	}
    89  	if err := e.argsMatches(against); err != nil {
    90  		t.Error("arguments should match, since argument are the same")
    91  	}
    92  
    93  	e = &queryBasedExpectation{args: []driver.Value{true}, converter: driver.DefaultParameterConverter}
    94  	against = []driver.NamedValue{
    95  		{Value: false, Ordinal: 1},
    96  	}
    97  	if err := e.argsMatches(against); err == nil {
    98  		t.Error("arguments should not match, since argument is different")
    99  	}
   100  
   101  	e = &queryBasedExpectation{args: []driver.Value{false}, converter: driver.DefaultParameterConverter}
   102  	against = []driver.NamedValue{
   103  		{Value: true, Ordinal: 1},
   104  	}
   105  	if err := e.argsMatches(against); err == nil {
   106  		t.Error("arguments should not match, since argument is different")
   107  	}
   108  }
   109  
   110  func TestQueryExpectationNamedArgComparison(t *testing.T) {
   111  	e := &queryBasedExpectation{converter: driver.DefaultParameterConverter, noArgs: true}
   112  	against := []driver.NamedValue{{Value: int64(5), Name: "id"}}
   113  	if err := e.argsMatches(against); err == nil {
   114  		t.Error("arguments should not match, since argument was passed, but noArgs was set")
   115  	}
   116  
   117  	e.noArgs = false
   118  	if err := e.argsMatches(against); err != nil {
   119  		t.Error("arguments should match, since argument was passed, but no expected args or noArgs was set")
   120  	}
   121  
   122  	e.args = []driver.Value{
   123  		sql.Named("id", 5),
   124  		sql.Named("s", "str"),
   125  	}
   126  
   127  	if err := e.argsMatches(against); err == nil {
   128  		t.Error("arguments should not match, since the size is not the same")
   129  	}
   130  
   131  	against = []driver.NamedValue{
   132  		{Value: int64(5), Name: "id"},
   133  		{Value: "str", Name: "s"},
   134  	}
   135  
   136  	if err := e.argsMatches(against); err != nil {
   137  		t.Errorf("arguments should have matched, but it did not: %v", err)
   138  	}
   139  
   140  	against = []driver.NamedValue{
   141  		{Value: int64(5), Name: "id"},
   142  		{Value: "str", Name: "username"},
   143  	}
   144  
   145  	if err := e.argsMatches(against); err == nil {
   146  		t.Error("arguments matched, but it should have not due to Name")
   147  	}
   148  
   149  	e.args = []driver.Value{int64(5), "str"}
   150  
   151  	against = []driver.NamedValue{
   152  		{Value: int64(5), Ordinal: 0},
   153  		{Value: "str", Ordinal: 1},
   154  	}
   155  
   156  	if err := e.argsMatches(against); err == nil {
   157  		t.Error("arguments matched, but it should have not due to wrong Ordinal position")
   158  	}
   159  
   160  	against = []driver.NamedValue{
   161  		{Value: int64(5), Ordinal: 1},
   162  		{Value: "str", Ordinal: 2},
   163  	}
   164  
   165  	if err := e.argsMatches(against); err != nil {
   166  		t.Errorf("arguments should have matched, but it did not: %v", err)
   167  	}
   168  }
   169  
   170  type panicConverter struct {
   171  }
   172  
   173  func (s panicConverter) ConvertValue(v interface{}) (driver.Value, error) {
   174  	panic(v)
   175  }
   176  
   177  func Test_queryBasedExpectation_attemptArgMatch(t *testing.T) {
   178  	e := &queryBasedExpectation{converter: new(panicConverter), args: []driver.Value{"test"}}
   179  	values := []driver.NamedValue{
   180  		{Ordinal: 1, Name: "test", Value: "test"},
   181  	}
   182  	if err := e.attemptArgMatch(values); err == nil {
   183  		t.Errorf("error expected")
   184  	}
   185  }
   186  

View as plain text