|
| 1 | +package csvops |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func writeCSV(t *testing.T, path, body string) { |
| 12 | + t.Helper() |
| 13 | + if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 14 | + t.Fatal(err) |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func readFile(t *testing.T, path string) string { |
| 19 | + t.Helper() |
| 20 | + b, err := os.ReadFile(path) |
| 21 | + if err != nil { |
| 22 | + t.Fatal(err) |
| 23 | + } |
| 24 | + return string(b) |
| 25 | +} |
| 26 | + |
| 27 | +func TestSplit_BasicWithHeader(t *testing.T) { |
| 28 | + dir := t.TempDir() |
| 29 | + in := filepath.Join(dir, "in.csv") |
| 30 | + out := filepath.Join(dir, "parts") |
| 31 | + writeCSV(t, in, "id,name\n1,a\n2,b\n3,c\n4,d\n5,e\n") |
| 32 | + |
| 33 | + res, err := Split(context.Background(), SplitOptions{ |
| 34 | + Input: in, |
| 35 | + OutputDir: out, |
| 36 | + RowsPerFile: 2, |
| 37 | + WithHeader: true, |
| 38 | + Delimiter: ',', |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + if res.RowsProcessed != 5 { |
| 44 | + t.Errorf("RowsProcessed = %d, want 5", res.RowsProcessed) |
| 45 | + } |
| 46 | + if res.FilesCreated != 3 { |
| 47 | + t.Errorf("FilesCreated = %d, want 3", res.FilesCreated) |
| 48 | + } |
| 49 | + |
| 50 | + got := readFile(t, filepath.Join(out, "part_1.csv")) |
| 51 | + if !strings.HasPrefix(got, "id,name\n") { |
| 52 | + t.Errorf("part_1.csv missing header: %q", got) |
| 53 | + } |
| 54 | + |
| 55 | + got3 := readFile(t, filepath.Join(out, "part_3.csv")) |
| 56 | + if !strings.Contains(got3, "5,e") { |
| 57 | + t.Errorf("part_3.csv missing trailing row: %q", got3) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestSplit_ProgressCallbackInvoked(t *testing.T) { |
| 62 | + dir := t.TempDir() |
| 63 | + in := filepath.Join(dir, "in.csv") |
| 64 | + writeCSV(t, in, "h\n1\n2\n3\n") |
| 65 | + |
| 66 | + var calls int |
| 67 | + var lastDone, lastTotal int64 |
| 68 | + _, err := Split(context.Background(), SplitOptions{ |
| 69 | + Input: in, |
| 70 | + OutputDir: filepath.Join(dir, "out"), |
| 71 | + RowsPerFile: 1, |
| 72 | + WithHeader: true, |
| 73 | + Delimiter: ',', |
| 74 | + Progress: func(done, total int64) { |
| 75 | + calls++ |
| 76 | + lastDone, lastTotal = done, total |
| 77 | + }, |
| 78 | + }) |
| 79 | + if err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if calls != 3 { |
| 83 | + t.Errorf("progress called %d times, want 3", calls) |
| 84 | + } |
| 85 | + if lastDone != 3 || lastTotal != 3 { |
| 86 | + t.Errorf("final progress = (%d,%d), want (3,3)", lastDone, lastTotal) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestSplit_CancelledContext(t *testing.T) { |
| 91 | + dir := t.TempDir() |
| 92 | + in := filepath.Join(dir, "in.csv") |
| 93 | + writeCSV(t, in, "h\n1\n2\n3\n") |
| 94 | + |
| 95 | + ctx, cancel := context.WithCancel(context.Background()) |
| 96 | + cancel() |
| 97 | + |
| 98 | + _, err := Split(ctx, SplitOptions{ |
| 99 | + Input: in, |
| 100 | + OutputDir: filepath.Join(dir, "out"), |
| 101 | + RowsPerFile: 1, |
| 102 | + WithHeader: true, |
| 103 | + Delimiter: ',', |
| 104 | + }) |
| 105 | + if err != context.Canceled { |
| 106 | + t.Errorf("err = %v, want context.Canceled", err) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestSplit_ValidatesInputs(t *testing.T) { |
| 111 | + _, err := Split(context.Background(), SplitOptions{RowsPerFile: 10}) |
| 112 | + if err == nil { |
| 113 | + t.Error("expected error for missing input") |
| 114 | + } |
| 115 | + _, err = Split(context.Background(), SplitOptions{Input: "x", RowsPerFile: 0}) |
| 116 | + if err == nil { |
| 117 | + t.Error("expected error for RowsPerFile=0") |
| 118 | + } |
| 119 | +} |
0 commit comments