Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scanning of sql.NullInt64 when row is created from CSV input #142

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}