bulk_csv_test.go
package imp
import (
"strings"
"testing"
)
const validLocID = "IBv_KxDGsNQs"
func validRow() []string {
return []string{"Site A", validLocID, "/data/site_a", "2024-01-01_2024-01-31", "48000", "1200"}
}
func TestParseBulkCSVRow_Valid(t *testing.T) {
got, err := parseBulkCSVRow(validRow())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
want := bulkLocationData{
LocationName: "Site A",
LocationID: validLocID,
DirectoryPath: "/data/site_a",
DateRange: "2024-01-01_2024-01-31",
SampleRate: 48000,
FileCount: 1200,
}
if got != want {
t.Errorf("got %+v want %+v", got, want)
}
}
func TestParseBulkCSVRow_TrimsWhitespace(t *testing.T) {
row := []string{" Site A ", validLocID, " /data/site_a ", " 2024-01-01_2024-01-31 ", "48000", "1200"}
got, err := parseBulkCSVRow(row)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.LocationName != "Site A" || got.DirectoryPath != "/data/site_a" || got.DateRange != "2024-01-01_2024-01-31" {
t.Errorf("whitespace not trimmed: %+v", got)
}
}
func TestParseBulkCSVRow_Errors(t *testing.T) {
mutate := func(idx int, val string) []string {
r := validRow()
r[idx] = val
return r
}
tests := []struct {
name string
row []string
errSubstr string
}{
{"too few columns", []string{"a", "b", "c"}, "insufficient columns"},
{"empty location_name", mutate(0, ""), "empty location_name"},
{"empty location_name whitespace", mutate(0, " "), "empty location_name"},
{"empty directory_path", mutate(2, ""), "empty directory_path"},
{"empty date_range", mutate(3, ""), "empty date_range"},
{"bad location_id (short)", mutate(1, "abc"), "invalid location_id"},
{"bad location_id (bad chars)", mutate(1, "IBv KxDGsNQs"), "invalid location_id"},
{"non-numeric sample_rate", mutate(4, "fast"), "invalid sample_rate"},
{"out-of-range sample_rate", mutate(4, "10"), "invalid sample_rate"},
{"non-numeric file_count", mutate(5, "many"), "invalid file_count"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := parseBulkCSVRow(tt.row)
if err == nil {
t.Fatalf("expected error containing %q, got nil", tt.errSubstr)
}
if !strings.Contains(err.Error(), tt.errSubstr) {
t.Errorf("error %q does not contain %q", err.Error(), tt.errSubstr)
}
})
}
}