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

feat: port v2 branch optimize to main branch #313

Closed
wants to merge 1 commit into from
Closed
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 @@ -14,7 +14,7 @@ const invalidate = "☠☠☠ MEMORY OVERWRITTEN ☠☠☠ "
// 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 @@ -753,3 +753,53 @@ func ExampleRows_AddRows() {
// Output: scanned id: 1 and title: one
// scanned id: 2 and title: two
}

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)
}
}