Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
fe9568d7ae | |||
454c632462 | |||
858cae9547 |
29
input_csv_test.go
Normal file
29
input_csv_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package script_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"code.uint32.ru/dmitry/script"
|
||||
)
|
||||
|
||||
func TestCSVReader(t *testing.T) {
|
||||
t.Parallel()
|
||||
r, err := script.NewCSVReader("testdata/sample_csv.csv")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
want := []string{"one", "two", "three"}
|
||||
|
||||
for range 2 {
|
||||
row, err := r.Read()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !slices.Equal(row, want) {
|
||||
t.Fatalf("rows not equal, want: %v, have: %v", want, row)
|
||||
}
|
||||
}
|
||||
}
|
42
output_csv_test.go
Normal file
42
output_csv_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package script
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCSVWriter(t *testing.T) {
|
||||
path := "testdata/output_csv.csv"
|
||||
|
||||
os.Remove(path)
|
||||
defer os.Remove(path)
|
||||
|
||||
w, err := newCSVwriter(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
want := []byte("one,two,three\none,two,three\n")
|
||||
|
||||
row := []string{"one", "two", "three"}
|
||||
|
||||
for range 2 {
|
||||
if err := w.Write(row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("err reading output file: %s", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(want, b) {
|
||||
t.Errorf("incorrect result, want: %s, have: %s", string(want), string(b))
|
||||
}
|
||||
}
|
2
testdata/sample_csv.csv
vendored
Normal file
2
testdata/sample_csv.csv
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
one,two,three
|
||||
one,two,three
|
|
Reference in New Issue
Block a user