diff --git a/rows.go b/rows.go index 01ea811..497b07f 100644 --- a/rows.go +++ b/rows.go @@ -215,6 +215,9 @@ func (r *Rows) FromCSVString(s string) *Rows { } panic(fmt.Sprintf("Parsing CSV string failed: %s", err.Error())) } + if len(res) != len(r.cols) { + panic(fmt.Sprintf("Expected number of values to match number of columns: expected %d, actual %d", len(r.cols), len(res))) + } row := make([]driver.Value, len(r.cols)) for i, v := range res { diff --git a/rows_test.go b/rows_test.go index c2d921a..34df4d3 100644 --- a/rows_test.go +++ b/rows_test.go @@ -474,6 +474,17 @@ func TestCSVParserInvalidInput(t *testing.T) { t.Error("expected panic from parsing invalid CSV") } +func TestCSVParserRejectsShortRecord(t *testing.T) { + const expectedPanic = "Expected number of values to match number of columns: expected 3, actual 2" + defer func() { + if r := recover(); r != expectedPanic { + t.Fatalf("expected panic %q, got %v", expectedPanic, r) + } + }() + + NewRows([]string{"order_id", "status", "created_at"}).FromCSVString("INV-2026-0908,paid") +} + func TestWrongNumberOfValues(t *testing.T) { // Open new mock database db, mock, err := New()