blob: c479b186c506ddf3034ad1c0812db322554cc45c [file] [log] [blame]
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"io/ioutil"
"path/filepath"
"testing"
"cloud.google.com/go/bigquery"
"github.com/google/go-cmp/cmp"
)
func TestWriteOutput(t *testing.T) {
t.Run("writes to json output path", func(t *testing.T) {
rows := []map[string]bigquery.Value{
{
"number": float64(1), // All numbers are coerced to floats by round-tripping through JSON.
"bool": true,
"string": "foo",
},
}
outputPath := filepath.Join(t.TempDir(), "output.json")
if err := writeOutput(rows, outputPath, nil); err != nil {
t.Fatal(err)
}
b, err := ioutil.ReadFile(outputPath)
if err != nil {
t.Fatal(err)
}
var outputRows []map[string]bigquery.Value
if err := json.Unmarshal(b, &outputRows); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(rows, outputRows); diff != "" {
t.Fatalf("got wrong output rows (-want +got)\n%s", diff)
}
})
t.Run("writes to stdout if json output path is unset", func(t *testing.T) {
rows := []map[string]bigquery.Value{
{"a": "foo", "b": "bar"},
{"c": "foo", "d": "bar"},
}
stdout := bytes.NewBuffer([]byte{})
if err := writeOutput(rows, "", stdout); err != nil {
t.Fatal(err)
}
var outputRows []map[string]bigquery.Value
if err := json.Unmarshal(stdout.Bytes(), &outputRows); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(rows, outputRows); diff != "" {
t.Fatalf("got wrong output rows (-want +got)\n%s", diff)
}
})
}