DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to mock results with datetime columns

Hebo opened this issue · comments

Hey,

How can I mock columns that are stored as datetimes? It seems that RowsFromCSVString sets driver.Values that are always []byte, and Scan is unable to parse time.time objects from them:

Scan error on column index 4: unsupported driver -> Scan pair: []uint8 -> *time.Time

I'd guess this would work if either RowsFromCSVString was able to directly create time.time objects when it encounters them or there was another way to assemble sqlmock.rows objects.

Example:

sqlmock.ExpectQuery("SELECT now()").
    WillReturnRows(sqlmock.RowsFromCSVString([]string{"now"}, "2014-02-12 16:04:15.879588-08"))

# sql: Scan error on column index 0: unsupported driver -> Scan pair: []uint8 -> *time.Time

Hacked together an example in my fork of how giving native types to Scan will let the parsing handle more types (for the ones in this list, at least). I couldn't work out a good API for multiple rows though, or I'd clean it up and submit a PR.

Current example use:

WillReturnRows(sqlmock.RowFromInterface([]string{"now"}, time.Now()))

Thoughts?

Hi, yes there could be more ways to build sql driver rows. one for example, could be used to read struct data, like sqlstruct uses tags. another from an array of interface{}. I would really appreciate any contributions regarding it. So if you come up with a nice way of doing it, I would be happy to include it in the library. database sql rows interface gives us a flexible way to create them differently. I will take a look at your fork, this week, busy days recently.