2020-08-06 20:58:47 +03:00
|
|
|
package htmlw_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-15 04:45:34 +03:00
|
|
|
"context"
|
2022-12-18 02:11:33 +03:00
|
|
|
"os"
|
2020-08-06 20:58:47 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-11-20 04:06:36 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output"
|
2020-08-06 20:58:47 +03:00
|
|
|
"github.com/neilotoole/sq/cli/output/htmlw"
|
|
|
|
"github.com/neilotoole/sq/testh"
|
|
|
|
"github.com/neilotoole/sq/testh/sakila"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRecordWriter(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
numRecs int
|
|
|
|
fixtPath string
|
|
|
|
}{
|
|
|
|
{name: "actor_0", numRecs: 0, fixtPath: "testdata/actor_0_rows.html"},
|
|
|
|
{name: "actor_3", numRecs: 3, fixtPath: "testdata/actor_3_rows.html"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-01-15 04:45:34 +03:00
|
|
|
ctx := context.Background()
|
2020-08-06 20:58:47 +03:00
|
|
|
recMeta, recs := testh.RecordsFromTbl(t, sakila.SL3, sakila.TblActor)
|
|
|
|
recs = recs[0:tc.numRecs]
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
2023-05-07 05:36:34 +03:00
|
|
|
pr := output.NewPrinting()
|
|
|
|
w := htmlw.NewRecordWriter(buf, pr)
|
2024-01-15 04:45:34 +03:00
|
|
|
require.NoError(t, w.Open(ctx, recMeta))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2024-01-15 04:45:34 +03:00
|
|
|
require.NoError(t, w.WriteRecords(ctx, recs))
|
|
|
|
require.NoError(t, w.Close(ctx))
|
2020-08-06 20:58:47 +03:00
|
|
|
|
2022-12-18 02:11:33 +03:00
|
|
|
want, err := os.ReadFile(tc.fixtPath)
|
2020-08-06 20:58:47 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, string(want), buf.String())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|