...

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

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

     1  package sqlmock
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"errors"
     6  	"fmt"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  type CustomConverter struct{}
    12  
    13  func (s CustomConverter) ConvertValue(v interface{}) (driver.Value, error) {
    14  	switch v.(type) {
    15  	case string:
    16  		return v.(string), nil
    17  	case []string:
    18  		return v.([]string), nil
    19  	case int:
    20  		return v.(int), nil
    21  	default:
    22  		return nil, errors.New(fmt.Sprintf("cannot convert %T with value %v", v, v))
    23  	}
    24  }
    25  
    26  func ExampleExpectedExec() {
    27  	db, mock, _ := New()
    28  	result := NewErrorResult(fmt.Errorf("some error"))
    29  	mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
    30  	res, _ := db.Exec("INSERT something")
    31  	_, err := res.LastInsertId()
    32  	fmt.Println(err)
    33  	// Output: some error
    34  }
    35  
    36  func TestBuildQuery(t *testing.T) {
    37  	db, mock, _ := New()
    38  	query := `
    39  		SELECT
    40  			name,
    41  			email,
    42  			address,
    43  			anotherfield
    44  		FROM user
    45  		where
    46  			name    = 'John'
    47  			and
    48  			address = 'Jakarta'
    49  
    50  	`
    51  
    52  	mock.ExpectQuery(query)
    53  	mock.ExpectExec(query)
    54  	mock.ExpectPrepare(query)
    55  
    56  	db.QueryRow(query)
    57  	db.Exec(query)
    58  	db.Prepare(query)
    59  
    60  	if err := mock.ExpectationsWereMet(); err != nil {
    61  		t.Error(err)
    62  	}
    63  }
    64  
    65  func TestCustomValueConverterQueryScan(t *testing.T) {
    66  	db, mock, _ := New(ValueConverterOption(CustomConverter{}))
    67  	query := `
    68  		SELECT
    69  			name,
    70  			email,
    71  			address,
    72  			anotherfield
    73  		FROM user
    74  		where
    75  			name    = 'John'
    76  			and
    77  			address = 'Jakarta'
    78  
    79  	`
    80  	expectedStringValue := "ValueOne"
    81  	expectedIntValue := 2
    82  	expectedArrayValue := []string{"Three", "Four"}
    83  	mock.ExpectQuery(query).WillReturnRows(mock.NewRows([]string{"One", "Two", "Three"}).AddRow(expectedStringValue, expectedIntValue, []string{"Three", "Four"}))
    84  	row := db.QueryRow(query)
    85  	var stringValue string
    86  	var intValue int
    87  	var arrayValue []string
    88  	if e := row.Scan(&stringValue, &intValue, &arrayValue); e != nil {
    89  		t.Error(e)
    90  	}
    91  	if stringValue != expectedStringValue {
    92  		t.Errorf("Expectation %s does not met: %s", expectedStringValue, stringValue)
    93  	}
    94  	if intValue != expectedIntValue {
    95  		t.Errorf("Expectation %d does not met: %d", expectedIntValue, intValue)
    96  	}
    97  	if !reflect.DeepEqual(expectedArrayValue, arrayValue) {
    98  		t.Errorf("Expectation %v does not met: %v", expectedArrayValue, arrayValue)
    99  	}
   100  	if err := mock.ExpectationsWereMet(); err != nil {
   101  		t.Error(err)
   102  	}
   103  }
   104  
   105  func TestQueryWithNoArgsAndWithArgsPanic(t *testing.T) {
   106  	defer func() {
   107  		if r := recover(); r != nil {
   108  			return
   109  		}
   110  		t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
   111  	}()
   112  	mock := &sqlmock{}
   113  	mock.ExpectQuery("SELECT (.+) FROM user").WithArgs("John").WithoutArgs()
   114  }
   115  
   116  func TestExecWithNoArgsAndWithArgsPanic(t *testing.T) {
   117  	defer func() {
   118  		if r := recover(); r != nil {
   119  			return
   120  		}
   121  		t.Error("Expected panic for using WithArgs and ExpectNoArgs together")
   122  	}()
   123  	mock := &sqlmock{}
   124  	mock.ExpectExec("^INSERT INTO user").WithArgs("John").WithoutArgs()
   125  }
   126  

View as plain text