26 lines
889 B
Go
26 lines
889 B
Go
package main
|
|
|
|
import "testing"
|
|
|
|
func TestConvertCSV(t *testing.T) {
|
|
out, err := convert(conversionInput{FileName: "records.csv", Content: "name,amount\nalpha,2\nbeta,3\n", TargetFormat: "json"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if out.FileName != "records.json" || out.Format != "json" || out.Rows != 2 || out.Content != `[{"amount":"2","name":"alpha"},{"amount":"3","name":"beta"}]` {
|
|
t.Fatalf("unexpected output: %+v", out)
|
|
}
|
|
}
|
|
|
|
func TestConvertRejectsMalformedCSV(t *testing.T) {
|
|
for _, in := range []conversionInput{
|
|
{FileName: "records.csv", Content: "name,amount\nalpha\n", TargetFormat: "json"},
|
|
{FileName: "records.csv", Content: "name,name\nalpha,beta\n", TargetFormat: "json"},
|
|
{FileName: "records.csv", Content: "name,amount\nalpha,2\n", TargetFormat: "xml"},
|
|
} {
|
|
if _, err := convert(in); err == nil {
|
|
t.Fatalf("malformed input accepted: %+v", in)
|
|
}
|
|
}
|
|
}
|