1
0
mirror of https://github.com/DATA-DOG/go-sqlmock.git synced 2025-04-02 21:45:24 +02:00

Merge pull request #142 from craigfitzpatrick/master

Fix scanning of sql.NullInt64 when row is created from CSV input
This commit is contained in:
Gediminas Morkevicius 2018-09-14 08:42:22 +03:00 committed by GitHub
commit c19298f520
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 1 deletions

View File

@ -11,7 +11,7 @@ import (
// CSVColumnParser is a function which converts trimmed csv
// column string to a []byte representation. currently
// transforms NULL to nil
var CSVColumnParser = func(s string) []byte {
var CSVColumnParser = func(s string) driver.Value {
switch {
case strings.ToLower(s) == "null":
return nil

View File

@ -315,3 +315,53 @@ func TestEmptyRowSets(t *testing.T) {
t.Fatalf("expected rowset 3, to be empty, but it was not")
}
}
func TestNullScanFromCSV(t *testing.T) {
const query string = "select nullable from TableWithNulls"
db, mock, err := New()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
rs := NewRows([]string{"nullable"}).AddRow(1).AddRow(nil).FromCSVString("null")
mock.ExpectQuery(query).WillReturnRows(rs)
var expectedResults = []sql.NullInt64{
{Valid: true, Int64: 1},
{Valid: false, Int64: 0},
{Valid: false, Int64: 0},
}
rows, err := db.Query(query)
if err != nil {
t.Fatalf("an error '%s' was not expected when querying a stub database connection", err)
}
defer rows.Close()
var actualResults []sql.NullInt64
for rows.Next() {
var ni sql.NullInt64
if err = rows.Scan(&ni); err != nil {
t.Fatalf("an error '%s' was not expected while scanning row from mocked recordset", err)
}
actualResults = append(actualResults, ni)
}
if len(expectedResults) != len(actualResults) {
t.Fatalf("unexpected row count: wanted %d, got %d", len(expectedResults), len(actualResults))
}
for i, v := range actualResults {
if v != expectedResults[i] {
t.Fatalf("unexpected value in row %d: wanted %v, got %v", i, expectedResults[i], v)
}
}
if err = mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
}