DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.05k stars 406 forks source link

How can I enable rows.Scan() to support []string? #193

Closed chengguangyu closed 4 years ago

chengguangyu commented 4 years ago

Hello,

I've been using a lot of go-sqlmock. However, I didn't find a way to mock the returned type from rows.Scan() when called database.Query (a rows object).

I have already successfully inplement the ConvertValue() func for my own custom type type StringSliceType []string which will make mock.NewRows([]string{"myarray"}).AddRow(StringSliceType {"hello", "world"}). enabled. I can input custom type to mock the Query result.

My 3rd party library (https://github.com/kshvakov/clickhouse) supports []string as supported data types so I can write:

var myArray []string
    if err := rows.Scan(&myArray); err != nil {
        e = err
        return
    }

However, there will be a type mismatch.

How could I extend go-sqlmock to allow mocking custom data types with the Scan function?

Thank you very much.

l3pp4rd commented 4 years ago

the only option is to use custom converters, which can be provided as an sqlmock option. but that will be messy, unless the db driver you use exports them

l3pp4rd commented 4 years ago

is now supported with custom converter, see the pull request above