|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/flarco/g" |
| 11 | + "github.com/flarco/g/process" |
| 12 | +) |
| 13 | + |
| 14 | +// TestOrderedFileMergeStdout verifies that when streaming from a folder of CSVs |
| 15 | +// using a wildcard, rows are emitted in strict filename order. |
| 16 | +func TestOrderedFileMergeStdout(t *testing.T) { |
| 17 | + bin := os.Getenv("SLING_BIN") |
| 18 | + if bin == "" { |
| 19 | + t.Fatalf("SLING_BIN environment variable is not set") |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + // Prepare temp folder with 50 CSV files named tablename_YYYYMMDD.csv |
| 24 | + dir, err := os.MkdirTemp("", "sling_order_*") |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("failed to create temp dir: %v", err) |
| 27 | + } |
| 28 | + defer os.RemoveAll(dir) |
| 29 | + |
| 30 | + // Create files tablename_20240101.csv .. tablename_20240150.csv |
| 31 | + for i := 1; i <= 50; i++ { |
| 32 | + fname := fmt.Sprintf("tablename_202401%02d.csv", i) |
| 33 | + path := filepath.Join(dir, fname) |
| 34 | + content := fmt.Sprintf("id,name\n%d,row_%02d\n", i, i) |
| 35 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 36 | + t.Fatalf("failed writing %s: %v", path, err) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Run sling to merge in order and print to stdout |
| 41 | + p, err := process.NewProc("bash") |
| 42 | + if !g.AssertNoError(t, err) { |
| 43 | + return |
| 44 | + } |
| 45 | + p.Capture = true |
| 46 | + p.WorkDir = "../.." |
| 47 | + bin = "cmd/sling/" + bin |
| 48 | + p.Print = true |
| 49 | + |
| 50 | + cmd := strings.Join([]string{ |
| 51 | + "set -e", |
| 52 | + "shopt -s expand_aliases", |
| 53 | + fmt.Sprintf("alias sling=%s", bin), |
| 54 | + fmt.Sprintf("sling run --src-stream 'file://%s' --src-options '{ format: \"csv\" }' --stdout", dir), |
| 55 | + }, "\n") |
| 56 | + |
| 57 | + err = p.Run("-lc", cmd) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("command failed: %v\nstdout: %s\nstderr: %s", err, p.Stdout.String(), p.Stderr.String()) |
| 60 | + } |
| 61 | + |
| 62 | + // Parse stdout and ensure rows 1..50 are in order (header first) |
| 63 | + lines := strings.Split(strings.TrimSpace(p.Stdout.String()), "\n") |
| 64 | + if len(lines) < 51 { |
| 65 | + t.Fatalf("expected at least 51 lines (1 header + 50 rows), got %d", len(lines)) |
| 66 | + } |
| 67 | + |
| 68 | + // Verify header |
| 69 | + if lines[0] != "id,name" { |
| 70 | + t.Fatalf("unexpected header: %q", lines[0]) |
| 71 | + } |
| 72 | + |
| 73 | + // Verify each subsequent line has ascending id and matching name |
| 74 | + for i := 1; i <= 50; i++ { |
| 75 | + expected := fmt.Sprintf("%d,row_%02d", i, i) |
| 76 | + if lines[i] != expected { |
| 77 | + t.Fatalf("unexpected line %d: got %q, want %q", i, lines[i], expected) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
0 commit comments